Hawk-and-Chicken HDU - 3639(强联通缩点)

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

题意:n个小朋友一起玩老鹰捉小鸡的游戏,有很多小朋友想扮演老鹰,因此老师让他们投票,获得票数最多的小朋友可以扮演老鹰,投票具有传递性,如果A赞成B,B赞成C,则A赞成C,(则C获得两票A,B),求出最大获的票数,和获得最大票数同学的标号

思路:原思路对每个点跑一边DFS,必然会超时,可以将一个强联通分量看成一点(内部任意两点可以互相到达),缩点后进行DFS,新图存图要反向存,最大值只可能在入度为0的点出现

代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
const int N = 5010;
int LOW[N],DFN[N],index;
int vis[N],include[N],cnt;
int dp[N],num[N],in[N],sum;
int n,m;
stack<int>p;
vector<int>G[N],mp[N];
void init(){
	index=cnt=0;
	memset(in,0,sizeof(in));
	memset(vis,0,sizeof(vis));
	memset(LOW,0,sizeof(LOW));
	memset(DFN,0,sizeof(DFN));
	memset(include,0,sizeof(include));
	memset(num,0,sizeof(num));
	for(int i=0;i<=n;i++)
	   G[i].clear(),mp[i].clear();
	while(!p.empty()) p.pop();
}
void tarjan(int x){
	LOW[x]=DFN[x]=++index;
	vis[x]=1;
	p.push(x);
	for(int i=0;i<G[x].size();i++){
		int v=G[x][i];
		if(!DFN[v]){
			tarjan(v);
			LOW[x]=min(LOW[x],LOW[v]);
		}
		else if(vis[v]) LOW[x]=min(LOW[x],DFN[v]);
	}
	
	if(LOW[x]==DFN[x]){
	    int v;
		include[x]=++cnt;
		do{
			v=p.top();
			p.pop();
			vis[v]=0;
			include[v]=cnt;//记录每个点属于的强联通块的标号 
			num[cnt]++; //记录每个强联通块的大小 
		}while(v!=x);
	}
}
void DFS(int x){
	vis[x]=1;
	sum+=num[x];
	for(int i=0;i<mp[x].size();i++){
		int v=mp[x][i];
		if(!vis[v]) DFS(v);
	}
}
int main(){
	int T,cas=0;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&m);
		init();
		for(int i=0;i<m;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			G[x].push_back(y);
		}
		for(int i=0;i<n;i++)
		   if(!DFN[i]) tarjan(i);
		
		for(int i=0;i<n;i++)
		for(int j=0;j<G[i].size();j++){
			int v=G[i][j];
			int x=include[i],y=include[v];
			if(x!=y){ //反向建图 
				mp[y].push_back(x);
				in[x]++;//入度 
			} 
		}
		
		memset(dp,0,sizeof(dp)); 
		int maxt=0;
		for(int i=1;i<=cnt;i++){
			if(!in[i]){
				memset(vis,0,sizeof(vis));
			    sum=0;
			    DFS(i);
			    dp[i]=sum;
			    if(sum>maxt) maxt=sum;
			}
		}
		
	    printf("Case %d: %d\n",++cas,maxt-1);
	    int first=1;
		for(int i=0;i<n;i++){
			if(dp[include[i]]==maxt){
				if(first) printf("%d",i);
				else printf(" %d",i);
				first=0;
			}
		}
		puts("");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值