hdu3639之缩点+逆向建图

Hawk-and-Chicken

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1739    Accepted Submission(s): 491


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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=5000+10;
int n,m,size,top,index;
int uu[MAX*6],vv[MAX*6];
int head[MAX],dfn[MAX],low[MAX],stack[MAX];
int mark[MAX],s[MAX],ind[MAX];
bool flag[MAX];
vector<int>a[MAX]; 

struct Edge{
	int v,next;
	Edge(){}
	Edge(int V,int NEXT):v(V),next(NEXT){}
}edge[MAX*6];

void Init(int num){
	for(int i=0;i<=num;++i)head[i]=-1,mark[i]=0;
	size=top=index=0;
}

void InsertEdge(int u,int v){
	edge[size]=Edge(v,head[u]);
	head[u]=size++;
}

void tarjan(int u){
	if(mark[u])return;
	dfn[u]=low[u]=++index;
	stack[++top]=u;
	mark[u]=1;
	for(int i=head[u];i != -1;i=edge[i].next){
		int v=edge[i].v;
		tarjan(v);
		if(mark[v] == 1)low[u]=min(low[u],low[v]);
	}
	if(dfn[u] == low[u]){
		while(stack[top] != u){
			mark[stack[top]]=-1;
			low[stack[top--]]=low[u];
		}
		mark[u]=-1;
		--top;
	}
}

int dfs(int u){
	if(flag[u])return 0;
	flag[u]=1;
	int sum=ind[u];
	for(int i=head[u];i != -1;i=edge[i].next){
		int v=edge[i].v;
		sum+=dfs(v);
	}
	return sum;
}

int main(){
	int t,num=0;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		Init(n);
		for(int i=0;i<m;++i){
			scanf("%d%d",&uu[i],&vv[i]);
			InsertEdge(uu[i],vv[i]);
		}
		for(int i=0;i<n;++i){
			if(mark[i])continue;
			tarjan(i);
		}
		for(int i=0;i<=n;++i)mark[i]=ind[i]=0,head[i]=-1,a[i].clear();
		for(int i=0;i<n;++i)++ind[low[i]],a[low[i]].push_back(i);
	 	size=0;
	 	for(int i=0;i<m;++i){//缩点逆向重建图 
	 		if(low[uu[i]] == low[vv[i]])continue;
	 		InsertEdge(low[vv[i]],low[uu[i]]);
	 		++mark[low[uu[i]]];
	 	}
	 	int ans=0,sum=0,k=0,t,v; 
	 	for(int i=0;i<=n;++i){
	 		t=a[i].size();
	 		if(t == 0)continue;
	 		v=a[i][0];
	 		if(mark[low[v]])continue;//入度不为0的一定不可能最大(因为该点的父节点就比该点大) 
	 		for(int j=0;j<=index;++j)flag[j]=false;
	 		ans=dfs(low[v]);
	 		if(ans<sum)continue;
	 		if(ans>sum)sum=ans,k=0;
	 		for(int j=0;j<t;++j)s[k++]=a[i][j];
	 	}
	 	sort(s,s+k);
		printf("Case %d: %d\n",++num,sum-1);
		for(int i=0;i<k;++i)printf("%d%c",s[i],i+1 == k?'\n':' ');
	}
	return 0;
}



你可能感兴趣的:(hdu3639之缩点+逆向建图)