HDU 3969(强连通分量+缩点)

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 totalsupports 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

 

题意:一个图,设res[i] 为所有能到达i点的点的个数,求最大个数以及这些点,点按升序输出

思路:有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly connected)。如果有向图G的每两个顶点都强连通,称G是一个强连通图。有向图的极大强连通子图,称为强连通分量(strongly connected components)。

           冷静分析:容易看出,设某个强连通分量里有a个点,则该强连通分量中的所有点都至少有a - 1 个人支持,我们把每个强连通分量看成一个点(单个点也算一个强连通分量), 其余边不变,容易看出最后得到的图为有向无环图,然后再对每个强连通分量dfs一次,将他们能访问到的点的值都加上该强连通分量的点的个数,最后找一下最大值和对应的点,排序输出就好了

 

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL INF = 1000000000;
const LL MAX = 5050;
int n, m;

struct Edge // 前向星建图
{
	int to, next;
} edge[MAX * 6], edge2[MAX * 6];

int k, head[MAX];

void add(int a, int b){
	edge[k].to = b;
	edge[k].next = head[a];
	head[a] = k++;
}

int k2, head2[MAX];

void add2(int a, int b){
	edge2[k2].to = b;
	edge2[k2].next = head2[a];
	head2[a] = k2++;
}

int dfn[MAX], low[MAX]; // dfn记录时间戳, low记录能访问到的最小时间戳
int s[MAX], top, tim; // s手写栈,
int nodeno[MAX], bcc_cnt; // nodeno记录每个点所在的强连通分量的编号
int num[MAX]; // 记录每个强连通分量的点的个数
int res[MAX], ans[MAX]; // res[u]记录能到达点u的点的个数, ans用于存点
int vis[MAX]; // vis的具体作用看下面
void init(){
	memset(dfn, 0, sizeof(dfn));
	memset(low, 0, sizeof(low));
	memset(head, -1, sizeof(head));
	memset(head2, -1, sizeof(head2));
	memset(num, 0, sizeof(num));
	memset(res, 0, sizeof(res));
	memset(nodeno, 0, sizeof(nodeno));
	bcc_cnt = 0;
	top = tim = k = k2 = 0;
}


void dfs(int u, int pre){ // 求强连通分量模板
	dfn[u] = low[u] = ++tim;
	s[++top] = u;
	for(int i= head[u]; i != -1; i = edge[i].next){
		int to = edge[i].to;
		if(!dfn[to]){
			dfs(to, u);
			low[u] = min(low[to], low[u]);
		} else if(!nodeno[to]){
			low[u] = min(low[u], dfn[to]);
		}	
	}
	if(low[u] == dfn[u]){
		int x;
		bcc_cnt++;
		do{
			x = s[top--];
			nodeno[x] = bcc_cnt; 
			num[bcc_cnt]++;
		} while(x != u);
	}
}

void dfs2(int u, int pre, int sum){ //找出该强连通分量能到达的所有点, sum为强连通分量所包含的点
	vis[u] = 1; // 记录访问过的点
	for(int i = head2[u]; i != -1; i = edge2[i].next){
		int to = edge2[i].to;
		if(vis[to]){
			continue;
		}
		res[to] += sum; 
		dfs2(to, u, sum);
	}
}

int main()
{
	int t;
	scanf("%d", &t);
	int ca = 1;
	while(t--){
		init();
		scanf("%d%d", &n, &m);
		for(int i = 0; i < m; i++){
			int a, b;
			scanf("%d%d", &a, &b);
			add(a, b);
		}

		for(int i = 0; i < n; i++){
			if(!dfn[i]){
				dfs(i, -1);
			}
		}
		for(int i = 0; i < n; i++){
			for(int j = head[i]; j != -1; j = edge[j].next){
				int to = edge[j].to;
				if(nodeno[to] != nodeno[i]){
					add2(nodeno[i], nodeno[to]);
				}
			}
		}

		for(int i = 1; i <= bcc_cnt; i++){
			memset(vis, 0, sizeof(vis));
			dfs2(i, -1, num[i]);
		}

		int ma = 0;
		for(int i = 1; i <= bcc_cnt; i++){
			ma = max(ma, res[i] + num[i]); // 结果为,可达人数加该分量中点的个数 - 1, 这里为了方便,先不减一
		}

		k = 0;
		for(int i = 0; i < n; i++){
			if(num[nodeno[i]] + res[nodeno[i]] == ma){
				ans[k++] = i;
			}
		}

		sort(ans, ans + k);
		printf("Case %d: %d\n", ca++, ma - 1);
		for(int i = 0; i < k; i++){
			printf("%d", ans[i]);
			if(i != k - 1){
				printf(" ");
			}
		}
		printf("\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值