Hawk-and-Chicken
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4508 Accepted Submission(s): 1387
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 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
题意:一群幼儿园小朋友想玩老鹰抓小鸡,但是谁都想当老鹰。老师发现每个人都有一些手绢,于是提议如果x支持y就可以把手绢丢给y,最后选出手绢最多的人。一个人有多个手绢,可以丢给多个人,但是一个人丢给另一个人的数量只能是1个。因为手绢具有传递性,如果一个人得到其他 x 个人的手绢,他可以把这x个手绢丢给自己支持的人,自己还有x个手绢。
思路:大概就是强连通缩点然后dfs找最长的一条。但是这样找有点麻烦,于是我们可以反向建图从终点开始找,最长的终点就输出。
坑点:一开始超时了,原因是找出度和入度的时候时间有点长
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=5000+50;
const int INF=0x7f7f7f7f;
typedef long long int ll;
struct Node
{
int end,next;
}map[maxn*6];
int pre[maxn],dfn[maxn],head[maxn],sta[maxn],dis[maxn];
int n,m,sum,top,index,ans,pos;
bool vis[maxn];
int arr[maxn],qin[maxn];
int ANS[maxn];
vector<int> v[maxn];
void Init()
{
for (int i=1;i<=n;i++)
v[i].clear();
sum=0;ans=top=index=0;
memset(map,0,sizeof(map));
memset(pre,0,sizeof(pre));
memset(dfn,0,sizeof(dfn));
memset(head,-1,sizeof(head));
memset(sta,0,sizeof(sta));
memset(vis,false,sizeof(vis));
memset(qin,0,sizeof(qin));
memset(arr,0,sizeof(arr));
memset(dis,0,sizeof(dis));
return ;
}
void addedge(int start,int end)
{
map[sum].end=end;
map[sum].next=head[start];
head[start]=sum++;
return ;
}
void tarjan(int start)
{
dfn[start]=pre[start]=++index;
sta[++top]=start;
vis[start]=true;
for (int i=head[start];i!=-1;i=map[i].next)
{
int end=map[i].end;
if (dfn[end]==0)
{
tarjan(end);
pre[start]=min(pre[start],pre[end]);
}
else if (vis[end]==true) pre[start]=min(pre[start],dfn[end]);
if (arr[start]!=arr[end]) qin[arr[end]]++; //在这里找入度为0的,就是起点
}
if (pre[start]==dfn[start])
{
ans++;
int j=-1;
while (j!=start)
{
j=sta[top--];
vis[j]=false;
arr[j]=ans;
v[ans].push_back(j); //缩点之后新建点,把原的点集合放进vector中
// cout<<j<<" "<<ans<<"------"<<endl;
}
// arr[j]=ans;vis[j]=false;v[ans].push_back(j);
}
}
void dfs(int now)
{
for (int i=head[now];i!=-1;i=map[i].next)
{
int end=map[i].end;
if (vis[end]==true) continue;
vis[end]=true;
pos++;
dfs(end);
}
return ;
}
int main()
{
int t,Max;
cin>>t;
int start,end;
for (int k=1;k<=t;k++)
{
cin>>n>>m;
Init();
for (int i=1;i<=m;i++)
{
scanf("%d%d",&start,&end);
addedge(end,start);
}
for (int i=0;i<n;i++)
if (dfn[i]==0) tarjan(i);
Max=0;
for (int i=1;i<=ans;i++)
if (qin[i]==0)
for (int j=0;j<v[i].size();j++)
{
memset(vis,false,sizeof(vis));
pos=0;
vis[v[i][j]]=true;
dfs(v[i][j]); //找最长的
dis[i]=pos;
Max=max(Max,dis[i]);
}
printf("Case %d: ",k);
cout<<Max<<endl;
int aaa=0;
for (int i=1;i<=ans;i++)
if (dis[i]==Max)
for (int j=0;j<v[i].size();j++)
ANS[aaa++]=v[i][j];
sort(ANS,ANS+aaa);
for (int i=0;i<aaa;i++)
if (i==aaa-1) cout<<ANS[i]<<endl;
else cout<<ANS[i]<<" ";
}
return 0;
}