【题解】HDU-3639 Hawk-and-Chicken

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.

样例输入

1
2
3
4
5
6
7
8
9
10
2
4 3
3 2
2 0
2 1

3 3
1 0
2 1
0 2

样例输出

1
2
3
4
Case 1: 2
0 1
Case 2: 2
0 1 2

提示

思路

代码

查看代码
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
113
114
115
116
117
118
119
120
121
122
123
124
125
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 5e3+5;
const int M = 3e4+5;

struct E {
int to, next;
} e[M];
int H[N], tot, R[N];

int S[N], top;
int dfn[N], low[N], bel[N], idx, scc;

int T, n, m;
int u, v;

int od[N], num[N], sum[N];
bool vis[N];

void add(int *H, int from, int to) {
e[tot] = {to, H[from]};
H[from] = tot++;
}

void dfs(int u) {
dfn[u] = low[u] = ++idx;
S[++top]=u;

for(int i=H[u]; ~i; i=e[i].next) {
int v = e[i].to;
if(!dfn[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
} else if(!bel[v])
low[u] = min(low[u], dfn[v]);
}
if(low[u]==dfn[u]) {
scc++;
int t;
do {
t=S[top--];
bel[t]=scc;
} while(t!=u);
}
}

void tarjan() {
memset(dfn, 0, sizeof(dfn));
memset(bel, 0, sizeof(bel));
idx = scc = top = 0;
for(int i=1; i<=n; i++) {
if(!dfn[i])
dfs(i);
}
}

int stat(int u) {
vis[u] = 1;
int t = num[u];
for(int i=R[u]; ~i; i=e[i].next) {
int v = e[i].to;
if(!vis[v]) {
t += stat(v);
}
}
return t;
}

int solve() {
memset(od, 0, sizeof(od));
memset(num, 0, sizeof(num));
memset(sum, 0, sizeof(sum));

for(int i=1; i<=n; i++) {
num[bel[i]]++;
}

for(int u=1; u<=n; u++) {
for(int i=H[u]; ~i; i=e[i].next) {
int v = e[i].to;
if(bel[u]!=bel[v]) {
add(R, bel[v], bel[u]);
od[bel[u]]++;
}
}
}
int ans=0;
for(int i=1; i<=scc; i++) {
if(!od[i]) {
memset(vis, 0, sizeof(vis));
sum[i] = stat(i)-1;
ans = max(ans, sum[i]);
}
}
return ans;
}

int main(void) {
scanf("%d", &T);
for(int cs=1; cs<=T; cs++) {
scanf("%d%d", &n, &m);
memset(H, -1, sizeof(H));
memset(R, -1, sizeof(R));
tot = 0;
for(int i=0; i<m; i++) {
scanf("%d%d", &u, &v);
add(H, u+1, v+1);
}
tarjan();
int ans = solve(), flag = 0;
printf("Case %d: %d\n", cs, ans);
for(int i=1; i<=n; i++) {
if(sum[bel[i]]==ans) {
if(flag)
printf(" ");
else
flag = 1;
printf("%d", i-1);
}
}
printf("\n");
}
return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/