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;
}

願你走出半生,歸來仍是少年~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章