# [tarjan] hdu 3639 Hawk-and-Chicken

http://acm.hdu.edu.cn/showproblem.php?pid=3639

# Hawk-and-Chicken

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1986    Accepted Submission(s): 575

Problem Description

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 totalsupports 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

Author

Dragon

Source

2010 ACM-ICPC Multi-University
Training Contest（19）——Host by HDU

Recommend

lcy   |   We have carefully selected several similar problems for you:  3634 3633 3636 3637 3638

tarjan缩点，反向建图，然后dfs统计入度为0的联通分量，求出能够到达的节点数a，比较记录a最大的那些连通分量。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
using namespace std;

#define Maxn 5500

int low[Maxn],dfn[Maxn],sta[Maxn],sc,bc,n,m,dep;
bool iss[Maxn];
int in[Maxn],nu[Maxn],dei[Maxn],ans,temp;
vector<vector<int> >myv;
vector<vector<int> >tree;
map<int,int>myp[Maxn];
bool vis[Maxn];

void tarjan(int cur)
{
int ne;

low[cur]=dfn[cur]=++dep;
sta[++sc]=cur;
iss[cur]=true;

for(int i=0;i<myv[cur].size();i++)
{
ne=myv[cur][i];
if(!dfn[ne])
{
tarjan(ne);
if(low[ne]<low[cur])
low[cur]=low[ne];
}
else if(iss[ne]&&dfn[ne]<low[cur])
low[cur]=dfn[ne];
}
if(low[cur]==dfn[cur])
{
bc++;
do
{
ne=sta[sc--];
iss[ne]=false;
in[ne]=bc;
nu[bc]++;
}while(ne!=cur);
}
}
void solve()
{
dep=sc=bc=0;
memset(iss,false,sizeof(iss));
memset(dfn,0,sizeof(dfn));
memset(nu,0,sizeof(nu));

for(int i=0;i<n;i++)
if(!dfn[i])
tarjan(i);
}
void dfs(int cur)
{
vis[cur]=true;
temp+=nu[cur];

for(int j=0;j<tree[cur].size();j++)
{
int ne=tree[cur][j];
if(!vis[ne])
dfs(ne);
}
}

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,kcas=0;

scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
myv.clear();
myv.resize(n+1);

for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
myv[a].push_back(b);
}
solve();

tree.clear();
tree.resize(bc+1);
for(int i=1;i<=bc;i++)
myp[i].clear();

memset(dei,0,sizeof(dei));
for(int i=0;i<n;i++)
{
for(int j=0;j<myv[i].size();j++)
{
int ne=myv[i][j];
if(in[ne]!=in[i])
{
if(myp[in[ne]][in[i]]==0)
{
//system("pause");
myp[in[ne]][in[i]]=1;
tree[in[ne]].push_back(in[i]);
dei[in[i]]++;
}
}
}
}
ans=0;
/*printf("bc:%d\n",bc);
for(int i=0;i<n;i++)
printf("i:%d bei:%d dei:%d\n",i,in[i],dei[in[i]]);*/
for(int i=1;i<=bc;i++)
{
if(!dei[i])
{
//printf("i:%d\n",i);
//system("pause");

for(int j=1;j<=bc;j++)
vis[j]=false;
temp=0;
dfs(i);
nu[i]=temp;
ans=max(ans,temp);
}
}
printf("Case %d: %d\n",++kcas,ans-1);
bool fi=true;

for(int i=0;i<n;i++)
{
if(nu[in[i]]==ans)
{
if(!fi)
putchar(' ');
else
fi=false;
printf("%d",i);
}

}
putchar('\n');
}

return 0;
}
/*
4 4
0 1
0 2
1 3
2 3
*/

## (tarjan/+DFS+反向建图) hdu 3639

G - Hawk-and-Chicken Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3639 Description Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But there always exists a big problem: ev

## HDU 3639 Hawk-and-Chicken（Tarjan缩点+反向DFS)

Problem Description 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

## HDU 3639 Hawk-and-Chicken

Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1979    Accepted Submission(s): 570 Problem Description Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But t

## LCA（最近公共祖先）--tarjan离线算法 hdu 2586

HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11320    Accepted Submission(s): 4119 Problem Description There are n houses in the village and some bidirectional roads c

## HDU 3207 Ikki&#39;s Story IV - Panda&#39;s Trick（图论-2SAT,图论-tarjan）

Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7821   Accepted: 2892 Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many tim