1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <stdio.h> #include <string.h> #include <stack> #include <algorithm> using namespace std;
int n,m, head[10005], cnt, timestamp[10005], low[10005], t, sccnum; bool ins[10005]; stack<int> s;
struct Arc { int from, to, nxt; }g[100005];
void addArc(int a, int b) { g[cnt].from = a, g[cnt].to = b, g[cnt].nxt = head[a]; head[a] = cnt++; }
void dfs(int i) { timestamp[i] = low[i] = ++t; s.push(i), ins[i] = true; for (int h = head[i],to; ~h; h = g[h].nxt) { to = g[h].to; if (!timestamp[to]) { dfs(to); low[i] = min(low[i], low[to]); } else if (ins[to]) { low[i] = min(low[i], low[to]); } } if (low[i] ==timestamp[i]) { if (++sccnum>1) { return; } int j = i; do { j = s.top(), s.pop(), ins[j] = false; } while (j!=i); } }
bool tarjan() { for (int i = 1; i<=n; i++) { if (!timestamp[i]) { dfs(i); if (sccnum>1) { return true; } } } return false; }
int main() {
#ifdef LOCAL freopen("d:\\data.in", "r", stdin); #endif while(scanf("%d%d", &n, &m), n||m) { memset(head, -1, sizeof(head)); memset(ins, 0, sizeof(ins)); memset(timestamp, 0, sizeof(timestamp)); memset(low, 0, sizeof(low)); while(!s.empty()) { s.pop(); } cnt = t = sccnum = 0; while(m--) { int a,b; scanf("%d%d", &a, &b); addArc(a,b); } tarjan()?puts("No"):puts("Yes"); } return 0; }
|