structEdge { int to, next; } e[maxm]; int c1, head[maxn], in[maxn], In[maxn]; inlinevoidadd_edge(int u, int v){ e[c1].to = v; e[c1].next = head[u]; head[u] = c1++; }
int id[maxn]; booltopsort(){ queue<int> Q; for (int i = 1; i <= n; ++i) if (!in[i]) Q.push(i); int cnt = 0; while (!Q.empty()) { int u = Q.front(); Q.pop(); ++cnt; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (--in[v] == 0) Q.push(v); } } return cnt == n; }
intmain(){ fill(head, head + maxn, -1); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; add_edge(x, y); ++in[y]; } for (int i = 1; i <= n; ++i) In[i] = in[i]; for (int i = 1; i <= n; ++i) { if (!In[i]) continue; for (int j = 1; j <= n; ++j) in[j] = In[j]; --in[i]; if (topsort()) returncout << "YES" << "\n", 0; } cout << "NO" << "\n"; return0; }