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 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| void dfs(int x) { v[x] = 1; for (int i = head[x]; i; i = next[i]) { int y = ver[i]; if (v[y]) continue; dfs(y); } }
void dfs(int x) { a[++m] = x; v[x] = 1; for (int i = head[x]; i; i = next[i]) { int y = ver[i]; if (v[y]) continue; dfs(y); } a[++m] = x; }
void dfs(int x) { v[x] = 1; for (int i = head[x]; i; i = next[i]) { int y = ver[i]; if (v[y]) continue; d[y] = d[x] + 1; dfs(y); } }
void dfs(int x) { v[x] = 1; size[x] = 1; int max_part = 0; for (int i = head[x]; i; i = next[i]) { int y = ver[i]; if (v[y]) continue; dfs(y); size[x] += size[y]; max_part = max(max_part, size[y]); } max_part = max(max_part, n - size[x]); if (max_part < ans) { ans = max_part; pos = x; } }
void dfs(int x) { v[x] = cnt; for (int i = head[x]; i; i = next[i]) { int y = ver[i]; if (v[y]) continue; dfs(y); } } for (int i = 1; i <= n; i++) if (!v[i]) { cnt++; dfs(i); }
void bfs() { memset(d, 0, sizeof(d)); queue<int> q; q.push(1); d[1] = 1; while (q.size()) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = next[i]) { int y = ver[i]; if (d[y]) continue; d[y] = d[x] + 1; q.push(y); } } }
void add(int x, int y) { ver[++tot] = y, next[tot] = head[x], head[x] = tot; deg[y]++; } void topsort() { queue<int> q; for (int i = 1; i <= n; i++) if (deg[i] == 0) q.push(i); while (q.size()) { int x = q.front(); q.pop(); a[++cnt] = x; for (int i = head[x]; i; i = next[i]) { int y = ver[i]; if (--deg[y] == 0) q.push(y); } } } int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); add(x, y); } topsort(); for (int i = 1; i <= cnt; i++) printf("%d ", a[i]); cout << endl; }
|