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


不难看出,同一个连通分量当中的值是一样的。所以我们可以缩点,记录块当中的个数。

然后答案肯定是缩点之后的图中,出度为0的点,故我们建立反向边,从出度为0的点开始计算答案。

要注意,同一个点可能计算多次,所以我们需要标记一下。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=5e3+10,M=3e4+10;
int T,n,m,mx,num,ts;
int dfn[N],low[N],col[N],sum[N],vis[N],co,cnt,du[N];
int head[N],nex[M],to[M],tot;
stack<int> st; vector<int> v[N],res,g[N],ans;
inline void add(int a,int b){to[++tot]=b; nex[tot]=head[a]; head[a]=tot;}
inline void init(){
	tot=cnt=co=mx=0; memset(head,0,sizeof head);
	memset(col,0,sizeof col); memset(dfn,0,sizeof dfn);
	memset(du,0,sizeof du); res.clear(); ans.clear();
	memset(sum,0,sizeof sum);
	for(int i=1;i<=n;i++)	v[i].clear(),g[i].clear();
}
void tarjan(int x){
	dfn[x]=low[x]=++cnt; vis[x]=1; st.push(x);
	for(int i=head[x];i;i=nex[i]){
		if(!dfn[to[i]]){
			tarjan(to[i]);	low[x]=min(low[x],low[to[i]]);
		}else if(vis[to[i]])	low[x]=min(low[x],dfn[to[i]]);
	}
	if(dfn[x]==low[x]){
		co++; int u;
		do{
			u=st.top(); st.pop(); vis[u]=0; col[u]=co; sum[co]++;
			g[co].push_back(u);
		}while(u!=x);
	}
}
void dfs(int x){
	for(int i=0;i<v[x].size();i++){
		if(!vis[v[x][i]])	num+=sum[v[x][i]]; 
		vis[v[x][i]]=1;	dfs(v[x][i]);
	}
}
signed main(){
	cin>>T;
	while(T--){
		cin>>n>>m;	init();
		for(int i=1,a,b;i<=m;i++){
			scanf("%d %d",&a,&b),a++,b++;if(a==b)	continue;add(a,b);	
		}
		for(int i=1;i<=n;i++)	if(!dfn[i])	tarjan(i);
		for(int i=1;i<=n;i++){
			for(int j=head[i];j;j=nex[j]){
				if(col[to[j]]!=col[i]){
					du[col[i]]++; v[col[to[j]]].push_back(col[i]);
				}
			}
		}
		for(int i=1;i<=co;i++){
			if(du[i])	continue; memset(vis,0,sizeof vis);
			num=sum[i]-1; 	dfs(i);
			if(num>mx){
				mx=num; res.clear(); res.push_back(i);
			}else if(num==mx)	res.push_back(i);
		}
		printf("Case %d: %d\n",++ts,mx);
		for(int i=0;i<res.size();i++){
			for(int j=0;j<g[res[i]].size();j++)	ans.push_back(g[res[i]][j]);
		}
		sort(ans.begin(),ans.end());
		for(int i=0;i<ans.size();i++)	
			printf("%d%c",ans[i]-1,i==ans.size()-1?'\n':' ');
	}
	return 0;
}
/* 1 6 6 1 5 4 1 2 4 2 0 0 2 4 5 */