Welcome!
复习一下拓扑排序
O(N + M) 顶点数 + 边数
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 100000;
const int MAXM = 5000000;
int head[MAXN + 1] = { 0 }; // 表示头指针,初始化为0
int p[MAXM + 1]; // 表示指向的节点
int nexte[MAXM + 1] = { 0 }; // 模拟指针,初始化为0
int edgecnt; // 记录边的数量
void addedge(int u, int v) { // 添加边(u,v)
++edgecnt;
p[edgecnt] = v;
nexte[edgecnt] = head[u];
head[u] = edgecnt;
}
// 枚举边的过程,u为起始点
/*for (int i = head[u]; i; i = next[i]) {
v = p[i];
...
}*/
int N, M;
void calcin(int degin[])
{
for (int k = 1; k <= N; k++)
{
for (int i = head[k]; i; i = nexte[i]) {
int v = p[i];
degin[v]++;
}
}
}
void checkring(queue<int> Q, int degin[])
{
while (!Q.empty())
{
int s = Q.front();
Q.pop();
N--;
for (int i = head[s]; i; i = nexte[i]) {
int v = p[i];
degin[v]--;
if (degin[v] == 0)
Q.push(v);
}
}
}
int main()
{
//freopen("t.txt","r", stdin);
int T;
cin >> T;
for (int ti = 0; ti < T; ti++)
{
memset(head, 0, sizeof(head));
memset(p, 0, sizeof(p));
memset(nexte, 0, sizeof(nexte));
edgecnt = 0;
cin >> N >> M;
for (int i = 0; i < M; i++)
{
int u, v;
scanf("%d %d", &u, &v);
addedge(u, v);
}
int degin[MAXN + 1] = { 0 };
calcin(degin);
queue<int> Q;
for (int i = 1; i <= N; i++)
{
if (degin[i] == 0)
Q.push(i);
}
if (Q.empty())
cout << "Wrong" << endl;
else
{
checkring(Q, degin);
cout << (N > 0 ? "Wrong" : "Correct") << endl;
}
}
return 0;
}