You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.2 KiB
53 lines
1.2 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
|
|
const int N = 110, M = N << 1;
|
|
int in[N]; // 入度
|
|
// 链式前向星
|
|
int e[M], h[N], idx, w[M], ne[M];
|
|
void add(int a, int b, int c = 0) {
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
}
|
|
|
|
bool solve(int n) {
|
|
queue<int> q;
|
|
for (int i = 0; i < n; i++)
|
|
if (in[i] == 0) q.push(i);
|
|
|
|
int cnt = 0;
|
|
while (q.size()) {
|
|
int u = q.front();
|
|
q.pop();
|
|
|
|
++cnt;
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int j = e[i];
|
|
in[j]--;
|
|
if (in[j] == 0) q.push(j);
|
|
}
|
|
}
|
|
return cnt == n;
|
|
}
|
|
|
|
int main() {
|
|
// 加快读入
|
|
ios::sync_with_stdio(false), cin.tie(0);
|
|
int n, m;
|
|
while (cin >> n >> m && n) {
|
|
// 多组测试数据,一定要注意两个初始语句!
|
|
memset(h, -1, sizeof h);
|
|
idx = 0;
|
|
memset(in, 0, sizeof in);
|
|
|
|
while (m--) {
|
|
int u, v;
|
|
cin >> u >> v;
|
|
in[v]++;
|
|
add(u, v); // 建正图
|
|
}
|
|
bool ans = solve(n);
|
|
puts(ans ? "YES" : "NO");
|
|
}
|
|
return 0;
|
|
}
|