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.
输入
There 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.
输出
For 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.
样例输入
12
24 3
33 2
42 0
52 1
6
73 3
81 0
92 1
100 2
样例输出
1Case 1: 2
20 1
3Case 2: 2
40 1 2
提示
无
思路
代码
1using namespace std;
2typedef long long ll;
3const int inf = 0x3f3f3f3f;
4const int N = 5e3+5;
5const int M = 3e4+5;
6
7struct E {
8 int to, next;
9} e[M];
10int H[N], tot, R[N];
11
12int S[N], top;
13int dfn[N], low[N], bel[N], idx, scc;
14
15int T, n, m;
16int u, v;
17
18int od[N], num[N], sum[N];
19bool vis[N];
20
21void add(int *H, int from, int to) {
22 e[tot] = {to, H[from]};
23 H[from] = tot++;
24}
25
26void dfs(int u) {
27 dfn[u] = low[u] = ++idx;
28 S[++top]=u;
29
30 for(int i=H[u]; ~i; i=e[i].next) {
31 int v = e[i].to;
32 if(!dfn[v]) {
33 dfs(v);
34 low[u] = min(low[u], low[v]);
35 } else if(!bel[v])
36 low[u] = min(low[u], dfn[v]);
37 }
38 if(low[u]==dfn[u]) {
39 scc++;
40 int t;
41 do {
42 t=S[top--];
43 bel[t]=scc;
44 } while(t!=u);
45 }
46}
47
48void tarjan() {
49 memset(dfn, 0, sizeof(dfn));
50 memset(bel, 0, sizeof(bel));
51 idx = scc = top = 0;
52 for(int i=1; i<=n; i++) {
53 if(!dfn[i])
54 dfs(i);
55 }
56}
57
58int stat(int u) {
59 vis[u] = 1;
60 int t = num[u];
61 for(int i=R[u]; ~i; i=e[i].next) {
62 int v = e[i].to;
63 if(!vis[v]) {
64 t += stat(v);
65 }
66 }
67 return t;
68}
69
70int solve() {
71 memset(od, 0, sizeof(od));
72 memset(num, 0, sizeof(num));
73 memset(sum, 0, sizeof(sum));
74
75 for(int i=1; i<=n; i++) {
76 num[bel[i]]++;
77 }
78
79 for(int u=1; u<=n; u++) {
80 for(int i=H[u]; ~i; i=e[i].next) {
81 int v = e[i].to;
82 if(bel[u]!=bel[v]) {
83 add(R, bel[v], bel[u]);
84 od[bel[u]]++;
85 }
86 }
87 }
88 int ans=0;
89 for(int i=1; i<=scc; i++) {
90 if(!od[i]) {
91 memset(vis, 0, sizeof(vis));
92 sum[i] = stat(i)-1;
93 ans = max(ans, sum[i]);
94 }
95 }
96 return ans;
97}
98
99int main(void) {
100 scanf("%d", &T);
101 for(int cs=1; cs<=T; cs++) {
102 scanf("%d%d", &n, &m);
103 memset(H, -1, sizeof(H));
104 memset(R, -1, sizeof(R));
105 tot = 0;
106 for(int i=0; i<m; i++) {
107 scanf("%d%d", &u, &v);
108 add(H, u+1, v+1);
109 }
110 tarjan();
111 int ans = solve(), flag = 0;
112 printf("Case %d: %d\n", cs, ans);
113 for(int i=1; i<=n; i++) {
114 if(sum[bel[i]]==ans) {
115 if(flag)
116 printf(" ");
117 else
118 flag = 1;
119 printf("%d", i-1);
120 }
121 }
122 printf("\n");
123 }
124 return 0;
125}