HDU3639(Hawk-and-Chicken)

Hawk-and-Chicken

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

题意

大概就是一个关系的传递,有n个小朋友在一个班级中,现在要选择班长。收集了小朋友们的意见,一条意见表示为A认为B合适。这个是具备传递性的,A认为B合适,B认为C合适。那么A也会认为C合适。
现在需要提供一份候选人名单,这里面的人是被最多的人,认为合适的。

思路

Tarjan + 缩点,首先要明白最基础的两点:

  1. 票数最多的肯定是缩点完后出度为0的点。
  2. 一个强连通分量子图中每个人的票数是这个强连通分量人数 - 1,自己不能投自己。
    光会这两点一测数据是对的,一交就WA,因为存在重复统计了。

6 6
0 1
0 2
1 5
1 3
2 3
2 4
答案3 3
错误答案4 3,因为0号对3号贡献了两票。直接num[y] += num[x]导致这个错误。

在这里插入图片描述
所以为了0号点不能重复算,那么就反向建图在对出度为0的点进行反向搜索统计,这样就能去除重复的情况,这组测试数据给的很好,一下就发现了问题。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <vector>
#include <stack>
#pragma warning(disable:4996)
using namespace std;
const int maxn = 5005;
struct edge{
	int from;
	int to;
	int next;
}e[maxn*6];
stack<int>s;
vector<int>a;
vector<int>map[maxn];
int head[maxn];				//链式前向星
int dfn[maxn];				//时间戳
int low[maxn];				//返祖时间序
int out[maxn];				//出度
int num[maxn];				//同一个强连通分量人数
bool vis[maxn];				//标记
int sum[maxn];				//票数
int scc[maxn];				//点所在强连通分量编号
int tot,cnt,res;			
inline void addedge(int x,int y)
{
	e[tot].from = x;
	e[tot].to = y;
	e[tot].next = head[x];
	head[x] = tot++;
}
inline void clear_set()
{
	cnt = tot = res = 0;
	memset(head,-1,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(num,0,sizeof(num));
	memset(sum,0,sizeof(sum));
	memset(out,0,sizeof(out));
	memset(scc,0,sizeof(scc));
	while(!s.empty())	s.pop();
	a.clear();
	for(int i = 0;i < maxn;i++){
		map[i].clear();
	}
}
inline void tarjan(int x)
{
	dfn[x] = low[x] = ++cnt;
	s.push(x);
	for(int i = head[x];~i;i = e[i].next){
		int y = e[i].to;
		if(!dfn[y]){
			tarjan(y);
			low[x] = min(low[x],low[y]);
		}
		else if(!scc[y]){
			low[x] = min(low[x],dfn[y]);
		}
	}
	if(low[x] == dfn[x]){
		res++;
		while(true){
			int p = s.top();
			s.pop();
			scc[p] = res;
			num[scc[p]]++;
			if(p == x){
				break;
			}
		}
	}
}
inline void dfs(int x)
{
	sum[x] = num[x];
	vis[x] = true;
	for(int i = 0;i < map[x].size();i++){
		int y = map[x][i];
		if(!vis[y]){
			dfs(y);
			sum[x] += sum[y];
		}
	}
}
int main()
{
	int t,n,m;
	scanf("%d",&t);
	for(int k = 1;k <= t;k++){
		clear_set();
		scanf("%d%d",&n,&m);
		for(int i = 0;i < m;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			addedge(x,y);
		}
		for(int i = 0;i < n;i++){
			if(!dfn[i]){
				tarjan(i);
			}
		}
		for(int i = 0;i < m;i++){
			int x = e[i].from;
			int y = e[i].to;
			if(scc[x] != scc[y]){			//scc[x] -> scc[y]有一条边
				out[scc[x]]++;				//scc[x]的出度+1
				map[scc[y]].push_back(scc[x]);
			}
		}
		for(int i = 1;i <= res;i++){
			if(!out[i]){
				memset(vis,false,sizeof(vis));
				dfs(i);
			}
		}
		int ans = *max_element(sum+1,sum+1+res);		//找到最大票数
		for(int i = 0;i < n;i++){
			if(sum[scc[i]] == ans){				
				a.push_back(i);
			}
		}
		printf("Case %d: %d\n%d",k,ans-1,a[0]);
		for(int i = 1;i < a.size();i++){
			printf(" %d",a[i]);
		}
		printf("\n");
	}
	return 0;
}

愿你走出半生,归来仍是少年~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值