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
AC的C++程序如下:
#include<bits/stdc++.h>
using namespace std;
const int N=5005;
int cnt;
int low[N],num[N],dfn,scc_count[N];
int sccno[N],stack1[N],sccnum[N],degree[N],top;
vector<int> G[N],newg[N];//缩点后建立的新图
int visit[N],scc_visit[N];
void dfs(int u)
{
stack1[top++]=u;
low[u]=num[u]=++dfn;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!num[v])
{
dfs(v);
low[u]=min(low[v],low[u]);
}
else if(!sccno[v])
{
low[u]=min(low[u],num[v]);
}
}
if(low[u]==num[u])
{
cnt++;
while(1)
{
int v=stack1[--top];
sccno[v]=cnt;
sccnum[cnt]++;
if(u==v) break;
}
}
}
void Tarjan(int n)
{
cnt=top=dfn=0;
memset(sccno,0,sizeof(sccno));
memset(sccnum,0,sizeof(sccnum));
memset(degree,0,sizeof(degree));
memset(num,0,sizeof(num));
memset(low,0,sizeof(low));
memset(scc_count,0,sizeof(scc_count));
memset(scc_visit,0,sizeof(scc_visit));
for(int i=0;i<n;i++)
{
if(!num[i]) dfs(i);
}
}
void dfs_scc(int u,int fa)
{
scc_count[fa]+=sccnum[u];
visit[u]=1;
for(int i=0;i<newg[u].size();i++)
{
if(!visit[newg[u][i]]) dfs_scc(newg[u][i],fa);
}
}
int main()
{
int t,kase=1;
scanf("%d",&t);
while(t--)
{
int n,m,u,v;
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++) G[i].clear();
for(int i=0;i<=n;i++) newg[i].clear();
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
}
Tarjan(n);
for(int i=0;i<n;i++)
{
for(int j=0;j<G[i].size();j++)
{
if(sccno[i]!=sccno[G[i][j]]){
newg[sccno[G[i][j]]].push_back(sccno[i]);//反向建图
degree[sccno[i]]++;
}
}
}
int max1=0;
for(int i=0;i<n;i++)
{
if(degree[sccno[i]]==0&&!scc_visit[sccno[i]]){
memset(visit,0,sizeof(visit));
dfs_scc(sccno[i],sccno[i]);
max1=max(max1,scc_count[sccno[i]]);
scc_visit[sccno[i]]=1;
}
}
printf("Case %d: %d\n",kase++,max1-1);
int flag=0;//标记是否输出空格
for(int i=0;i<n;i++)
{
if(degree[sccno[i]]==0&&scc_count[sccno[i]]==max1)
{
if(flag) printf(" ");
printf("%d",i);
flag=1;
}
}
printf("\n");
}
return 0;
}