拓扑排序
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxv = 100010; 4 const int maxe = 500010; 5 struct Edge{ 6 int v, nxt; 7 Edge(){} 8 Edge(int v, int nxt):v(v), nxt(nxt){} 9 }e[maxe<<1]; 10 int head[maxv], cnt; 11 void init(){ 12 memset(head, -1, sizeof head); 13 cnt = 0; 14 } 15 void add(int u, int v){ 16 e[cnt] = Edge(v, head[u]); 17 head[u] = cnt++; 18 } 19 20 int ind[maxv]; 21 int n, m; 22 int res; 23 int top(){ 24 queue<int> q; 25 for(int i = 1; i <= n; i++) if(ind[i] == 0) q.push(i); 26 while(!q.empty()){ 27 int u = q.front(); q.pop(); 28 res++; 29 for(int i = head[u]; ~i; i = e[i].nxt){ 30 int v = e[i].v; 31 ind[v]--; 32 if(ind[v] == 0) q.push(v); 33 } 34 } 35 return res == n; 36 } 37 int main(){ 38 int t; 39 scanf("%d", &t); 40 while(t--){ 41 scanf("%d %d", &n, &m); 42 memset(ind, 0, sizeof ind); 43 res = 0; 44 init(); 45 int u, v; 46 for(int i = 0; i < m; i++){ 47 scanf("%d %d", &u, &v); 48 add(u, v); 49 ind[v]++; 50 } 51 if(top()) puts("Correct"); 52 else puts("Wrong"); 53 } 54 }
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxv = 100010; 4 const int maxe = 500010; 5 const int mod = 142857; 6 struct Edge{ 7 int v, nxt; 8 Edge(){} 9 Edge(int v, int nxt):v(v), nxt(nxt){} 10 }e[maxe<<1]; 11 int head[maxv], cnt; 12 void init(){ 13 memset(head, -1, sizeof head); 14 cnt = 0; 15 } 16 void add(int u, int v){ 17 e[cnt] = Edge(v, head[u]); 18 head[u] = cnt++; 19 } 20 21 int ind[maxv]; 22 int n, m; 23 int ill[maxv]; 24 void top(){ 25 queue<int> q; 26 for(int i = 1; i <= n; i++) if(ind[i] == 0) q.push(i); 27 while(!q.empty()){ 28 int u = q.front(); 29 q.pop(); 30 for(int i = head[u]; ~i; i = e[i].nxt){ 31 int v = e[i].v; 32 ill[v] = (ill[v] + ill[u]) % mod; 33 ind[v]--; 34 if(!ind[v]) q.push(v); 35 } 36 } 37 } 38 int main(){ 39 int k, x; 40 while(scanf("%d %d %d", &n, &m, &k) != EOF){ 41 memset(ill, 0, sizeof ill); 42 memset(ind, 0, sizeof ind); 43 init(); 44 for(int i = 0; i < k; i++) scanf("%d", &x), ill[x] = 1; 45 for(int i = 0; i < m; i++){ 46 int u, v; 47 scanf("%d %d", &u, &v); 48 add(u, v); 49 ind[v]++; 50 } 51 top(); 52 int ans = 0; 53 for(int i = 1; i <= n; i++){ 54 ans = (ans + ill[i]) % mod; 55 } 56 printf("%d\n", ans); 57 } 58 }
拓扑排序 + 支配树
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxv = 100010; 4 const int maxe = maxv * 10; 5 int temp[11], dep[maxv], par[maxv]; 6 struct Edge{ 7 int v, nxt; 8 Edge(){} 9 Edge(int v, int nxt):v(v), nxt(nxt){} 10 }e[maxe]; 11 int head[maxv], cnt; 12 void init(){ 13 memset(head, -1, sizeof head); 14 cnt = 0 ; 15 } 16 void add(int u, int v){ 17 e[cnt] = Edge(v, head[u]); 18 head[u] = cnt++; 19 } 20 vector<int> g[maxv]; 21 int ind[maxv]; 22 int n, ans; 23 24 int lca(int u){ 25 int md = -1; 26 for(int i = 0; i < g[u].size(); i++){ 27 temp[i] = g[u][i]; 28 if(md == -1 || dep[temp[i]] < md){ 29 md = dep[temp[i]]; 30 } 31 } 32 for(int i = 0; i < g[u].size(); i++){ 33 while(dep[temp[i]] > md) temp[i] = par[temp[i]]; 34 } 35 while(1){ 36 int i = 1; 37 for(; i < g[u].size(); i++){ 38 if(temp[i] != temp[0]) break; 39 } 40 if(i == g[u].size()) break; 41 for(int i = 0; i < g[u].size(); i++){ 42 temp[i] = par[temp[i]]; 43 } 44 } 45 return temp[0]; 46 } 47 void top(){ 48 queue<int> q; 49 for(int i = 0; i <= n; i++) if(ind[i] == 0) q.push(i); 50 while(!q.empty()){ 51 int u = q.front(); q.pop(); 52 for(int i = head[u]; ~i; i = e[i].nxt){ 53 int v = e[i].v; 54 ind[v]--; 55 if(ind[v] == 0){ 56 par[v] = lca(v); 57 dep[v] = dep[par[v]] + 1; 58 if(par[v] == 0) ans++; 59 q.push(v); 60 } 61 } 62 } 63 } 64 int main(){ 65 while(scanf("%d", &n) != EOF){ 66 init(); 67 memset(ind, 0, sizeof ind); 68 for(int j = 1; j <= n; j++) g[j].clear(); 69 for(int i = 1; i <= n; i++){ 70 int k, x; 71 scanf("%d", &k); 72 for(int j = 0; j < k; j++){ 73 scanf("%d", &x); 74 add(x, i); 75 ind[i]++; 76 g[i].push_back(x); 77 } 78 } 79 ans = 0; 80 top(); 81 printf("%d\n", ans); 82 } 83 84 }