您的位置:首页 > 其它

HDU 3639 Hawk-and-Chicken

2015-10-02 15:39 363 查看

Hawk-and-Chicken

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3639
64-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.

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.

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.

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

2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU

解题:强连通分量后建反图直接乱搞

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5100;
struct arc {
int to,next;
arc(int x = 0,int y = -1) {
to = x;
next = y;
}
} e[100010];
int head[maxn],dfn[maxn],low[maxn],cnt[maxn],clk,scc,tot;
int belong[maxn],du[maxn],fan[maxn],n,m;
bool instack[maxn];
stack<int>stk;
vector<int>g[maxn];
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void tarjan(int u) {
dfn[u] = low[u] = ++clk;
instack[u] = true;
stk.push(u);
for(int i = head[u]; ~i; i = e[i].next) {
if(!dfn[e[i].to]) {
tarjan(e[i].to);
low[u] = min(low[u],low[e[i].to]);
} else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
}
if(low[u] == dfn[u]) {
int v;
cnt[++scc] = 0;
do {
instack[v = stk.top()] = false;
stk.pop();
belong[v] = scc;
cnt[scc]++;
} while(v != u);
}
}
bool vis[maxn];
void init() {
for(int i = tot = clk = scc = 0; i < maxn; ++i) {
head[i] = -1;
dfn[i] = low[i] = belong[i] = 0;
instack[i] = false;
g[i].clear();
fan[i] = du[i] = 0;
}
}
int dfs(int u) {
vis[u] = true;
int ret = cnt[u];
for(int i = g[u].size()-1; i >= 0; --i) {
if(vis[g[u][i]]) continue;
ret += dfs(g[u][i]);
}
return ret;
}
int main() {
int kase,u,v,cs = 1;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
init();
for(int i = 0; i < m; ++i) {
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i = 0; i < n; ++i)
if(!dfn[i]) tarjan(i);
for(int i = 0; i < n; ++i) {
for(int j = head[i]; ~j; j = e[j].next) {
if(belong[i] == belong[e[j].to]) continue;
g[belong[e[j].to]].push_back(belong[i]);
du[belong[i]]++;
}
}
int mx = -1;
for(int i = 1; i <= scc; ++i)
if(!du[i]) {
memset(vis,false,sizeof vis);
fan[i] = dfs(i) - 1;
mx = max(mx,fan[i]);
}
bool flag = true;
printf("Case %d: %d\n",cs++,mx);
for(int i = 0; i < n; ++i)
if(fan[belong[i]] == mx) {
if(flag) {
printf("%d",i);
flag = false;
} else printf(" %d",i);
}
puts("");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: