Hawk-and-Chicken
Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 363964-bit integer IO format: %I64d Java class name: Main
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.
Input
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.
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.
Output
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.
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
Source
解题:强连通分量后建反图直接乱搞
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 5100; 4 struct arc { 5 int to,next; 6 arc(int x = 0,int y = -1) { 7 to = x; 8 next = y; 9 } 10 } e[100010]; 11 int head[maxn],dfn[maxn],low[maxn],cnt[maxn],clk,scc,tot; 12 int belong[maxn],du[maxn],fan[maxn],n,m; 13 bool instack[maxn]; 14 stack<int>stk; 15 vector<int>g[maxn]; 16 void add(int u,int v) { 17 e[tot] = arc(v,head[u]); 18 head[u] = tot++; 19 } 20 void tarjan(int u) { 21 dfn[u] = low[u] = ++clk; 22 instack[u] = true; 23 stk.push(u); 24 for(int i = head[u]; ~i; i = e[i].next) { 25 if(!dfn[e[i].to]) { 26 tarjan(e[i].to); 27 low[u] = min(low[u],low[e[i].to]); 28 } else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]); 29 } 30 if(low[u] == dfn[u]) { 31 int v; 32 cnt[++scc] = 0; 33 do { 34 instack[v = stk.top()] = false; 35 stk.pop(); 36 belong[v] = scc; 37 cnt[scc]++; 38 } while(v != u); 39 } 40 } 41 bool vis[maxn]; 42 void init() { 43 for(int i = tot = clk = scc = 0; i < maxn; ++i) { 44 head[i] = -1; 45 dfn[i] = low[i] = belong[i] = 0; 46 instack[i] = false; 47 g[i].clear(); 48 fan[i] = du[i] = 0; 49 } 50 } 51 int dfs(int u) { 52 vis[u] = true; 53 int ret = cnt[u]; 54 for(int i = g[u].size()-1; i >= 0; --i) { 55 if(vis[g[u][i]]) continue; 56 ret += dfs(g[u][i]); 57 } 58 return ret; 59 } 60 int main() { 61 int kase,u,v,cs = 1; 62 scanf("%d",&kase); 63 while(kase--) { 64 scanf("%d%d",&n,&m); 65 init(); 66 for(int i = 0; i < m; ++i) { 67 scanf("%d%d",&u,&v); 68 add(u,v); 69 } 70 for(int i = 0; i < n; ++i) 71 if(!dfn[i]) tarjan(i); 72 for(int i = 0; i < n; ++i) { 73 for(int j = head[i]; ~j; j = e[j].next) { 74 if(belong[i] == belong[e[j].to]) continue; 75 g[belong[e[j].to]].push_back(belong[i]); 76 du[belong[i]]++; 77 } 78 } 79 int mx = -1; 80 for(int i = 1; i <= scc; ++i) 81 if(!du[i]) { 82 memset(vis,false,sizeof vis); 83 fan[i] = dfs(i) - 1; 84 mx = max(mx,fan[i]); 85 } 86 bool flag = true; 87 printf("Case %d: %d ",cs++,mx); 88 for(int i = 0; i < n; ++i) 89 if(fan[belong[i]] == mx) { 90 if(flag) { 91 printf("%d",i); 92 flag = false; 93 } else printf(" %d",i); 94 } 95 puts(""); 96 } 97 return 0; 98 }