Hawk-and-Chicken HDU - 3639
Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But there always exists a big problem: every kid in this game want to play the role of Hawk.
So the teacher came up with an idea: Vote. Every child have some nice handkerchiefs, and if he/she think someone is suitable for the role of Hawk, he/she gives a handkerchief to this kid, which means this kid who is given the handkerchief win the support. Note the support can be transmitted. Kids who get the most supports win in the vote and able to play the role of Hawk.(A note:if A can win
support from B(A != B) A can win only one support from B in any case the number of the supports transmitted from B to A are many. And A can't win the support from himself in any case.
If two or more kids own the same number of support from others, we treat all of them as winner.
Here's a sample: 3 kids A, B and C, A gives a handkerchief to B, B gives a handkerchief to C, so C wins 2 supports and he is choosen to be the Hawk.
So the teacher came up with an idea: Vote. Every child have some nice handkerchiefs, and if he/she think someone is suitable for the role of Hawk, he/she gives a handkerchief to this kid, which means this kid who is given the handkerchief win the support. Note the support can be transmitted. Kids who get the most supports win in the vote and able to play the role of Hawk.(A note:if A can win
support from B(A != B) A can win only one support from B in any case the number of the supports transmitted from B to A are many. And A can't win the support from himself in any case.
If two or more kids own the same number of support from others, we treat all of them as winner.
Here's a sample: 3 kids A, B and C, A gives a handkerchief to B, B gives a handkerchief to C, so C wins 2 supports and he is choosen to be the Hawk.
InputThere are several test cases. First is a integer T(T <= 50), means the number of test cases.
Each test case start with two integer n, m in a line (2 <= n <= 5000, 0 <m <= 30000). n means there are n children(numbered from 0 to n - 1). Each of the following m lines contains two integers A and B(A != B) denoting that the child numbered A give a handkerchief to B.OutputFor each test case, the output should first contain one line with "Case x:", here x means the case number start from 1. Followed by one number which is the total
supports the winner(s) get.
Then follow a line contain all the Hawks' number. The numbers must be listed in increasing order and separated by single spaces.Sample Input
2 4 3 3 2 2 0 2 1 3 3 1 0 2 1 0 2
Sample Output
Case 1: 2 0 1 Case 2: 2 0 1 2
题解:注意该有向图是可以存在环的,那么缩点之后的DAG。怎么求每个人的被喜爱度呢?一种方法是用DAG的反向图,通过DFS的回溯求。为什么取DAG中入度为0的顶点?可以用反证法,如果该顶点入度不为0,那么它的父亲结点一定更优
1 // A.cpp: 定义控制台应用程序的入口点。 2 // 3 4 //#include "stdafx.h" 5 #include<cstdio> 6 #include<vector> 7 #include<cstring> 8 #include<iostream> 9 #include<algorithm> 10 using namespace std; 11 12 const int maxm = 30003 ; 13 const int maxn = 5005; 14 15 int n, m,tot; 16 int cmp[maxn],head[maxn],ind[maxn],cnt[maxn],tem[maxn]; 17 bool use[maxn]; 18 19 vector<int> G1[maxn]; 20 vector<int> G2[maxn]; 21 vector<int> vs; 22 23 struct node { 24 int to, next; 25 }e[maxm]; 26 27 void Inite() { 28 tot = 0; 29 memset(head, -1, sizeof(head)); 30 for (int i = 0;i < n;i++) { 31 cmp[i] = 0; 32 ind[i] = 0; 33 cnt[i] = 0; 34 tem[i] = 0; 35 G1[i].clear(); 36 G2[i].clear(); 37 } 38 } 39 40 void addedge(int u, int v) { 41 e[tot].to = v; 42 e[tot].next = head[u]; 43 head[u] = tot++; 44 } 45 46 void add_edge(int u, int v) { 47 G1[u].push_back(v); 48 G2[v].push_back(u); 49 } 50 51 void DFS1(int v) { 52 use[v] = true; 53 for (int i = 0;i < G1[v].size();i++) if (!use[G1[v][i]]) DFS1(G1[v][i]); 54 vs.push_back(v); 55 } 56 57 void DFS2(int v, int k) { 58 use[v] = true; 59 cmp[v] = k; 60 for (int i = 0;i < G2[v].size();i++) if (!use[G2[v][i]]) DFS2(G2[v][i], k); 61 } 62 63 int DFS3(int v) { 64 use[v] = true; 65 int sum = cnt[v]; 66 for (int i = head[v];i != -1;i = e[i].next) if (!use[e[i].to]) sum+=DFS3(e[i].to); 67 return sum; 68 } 69 70 int Scc() { 71 vs.clear(); 72 memset(use, 0, sizeof(use)); 73 for (int i = 0;i < n;i++) if (!use[i]) DFS1(i); 74 memset(use, 0, sizeof(use)); 75 76 int k = 0, b = vs.size() - 1; 77 for (int i = b;i >= 0;i--) if (!use[vs[i]]) DFS2(vs[i], k++); 78 return k; 79 } 80 81 void Solve() { 82 int k = Scc(); 83 if (k == 1) { 84 printf("%d\n", n - 1); 85 for (int i = 0;i < n;i++) printf("%d%c", i, i == n - 1 ? '\n' : ' '); 86 return; 87 } 88 for (int u = 0;u < n;u++) { 89 cnt[cmp[u]]++; 90 for (int i = 0;i < G1[u].size();i++) { 91 int v = G1[u][i]; 92 if (cmp[v] == cmp[u]) continue; 93 addedge(cmp[v], cmp[u]); 94 ind[cmp[u]]++; 95 } 96 } 97 /* 98 for (int i = 0;i < n;i++) cout << cmp[i] << endl; 99 cout << "????" << endl; 100 for (int i = 0;i < k;i++) cout << ind[i] << endl; 101 cout << "????" << endl; 102 for (int i = 0;i < k;i++) cout << cnt[i] << endl; 103 */ 104 int ans = 0; 105 for (int i = 0;i < k;i++) { 106 if (ind[i] != 0) continue; 107 memset(use, 0, sizeof(use)); 108 tem[i] = DFS3(i); 109 ans = max(ans, tem[i]); 110 } 111 printf("%d\n", ans-1); 112 bool flag = true; 113 for (int i = 0;i < n;i++) { 114 if (tem[cmp[i]] == ans) { 115 if (flag) { 116 printf("%d", i); 117 flag = false; 118 } 119 else printf(" %d", i); 120 } 121 } 122 printf("\n"); 123 } 124 125 126 int main() 127 { 128 int kase; 129 cin >> kase; 130 for (int t = 1;t <= kase;t++) { 131 scanf("%d%d", &n, &m); 132 Inite(); 133 for (int i = 0;i < m;i++) { 134 int a, b; 135 scanf("%d%d", &a, &b); 136 add_edge(a, b); 137 } 138 printf("Case %d: ", t); 139 Solve(); 140 } 141 return 0; 142 }

浙公网安备 33010602011771号