文章
99
粉丝
120
获赞
8
访问
96.9k
#include<bits/stdc++.h>
using namespace std;
#include<queue>
const int maxn = 505;
bool mpt[maxn][maxn];
int lev[maxn];//点的入度
vector<int>v[maxn];//邻接表存储边
priority_queue<int, vector<int>,greater<int>>q;//最小元素最优先
void topo(int n)
{
for (int i = 1;i <= n;i++)
{
if (!lev[i])q.push(i);
}
int flag = 0;
while (!q.empty())
{
int now = q.top();
q.pop();
if (flag)printf(" %d", now);
else printf("%d", now);
flag++;
for (int i = 0;i < v[now].size();i++)
{
int next = v[now][i];
lev[next]--;
if (!lev[next])q.push(next);
}
}if (flag != n)
printf("有环,无拓扑排序");
}
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
memset(mpt, 0, sizeof(mpt));
for (int i = 1;i <= m;i++)
{
int a, b;
scanf("%d%d", &a, &b);
mpt[a][b] = 1;
}
for (int i = 1;i <= n;i++)
{
v[i].clear();
for (int j = 1;j <= n;j++)
{
if (mpt[...
登录后发布评论
暂无评论,来抢沙发