Hawk-and-Chicken(强连通-tarjan算法)

Hawk-and-Chicken(强连通-tarjan算法)

描述

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 &lt; = 50 ) T(T &lt;= 50) T(T<=50), means the number of test cases.
Each test case start with two integer n, m in a line ( 2 &lt; = n &lt; = 5000 , 0 &lt; m &lt; = 30000 ) (2 &lt;= n &lt;= 5000, 0 &lt;m &lt;= 30000) (2<=n<=5000,0<m<=30000). n n n means there are n n n children(numbered from 0 0 0 to n − 1 n - 1 n1). 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 x 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

题意

每个孩子手里有一些手帕可以给别的孩子来投票,然后给出孩子门投票的结果,问最后得到最多的选票是多少,以及都有谁得到了这个选票数。有一点需注意,加入A投给了B,B又投给了C,那么此时C是得到了两个选票的,也就是说选票在传递过程中是累加的。

先用Tarjin算法缩点,然后求出每个强连通块的人数,找出传递链开始的地方深搜,找出获得的最大票数(深搜前要把所有的边逆过来建一个新的图)。

代码
#include <bits/stdc++.h>
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
#define mem(a, b) memset(a, b, sizeof(a))
#define maxn 5010
#define inf 0x3f3f3f3f
using namespace std;
struct edge {//链式向前星
	int u, v, next, id;
}G[30005], g[30005];
int headG[maxn], cntG;//原链表
int headg[maxn], cntg;//逆链表
int dfn[maxn], low[maxn];
stack<int> q;
int n, m;
int scccnt;//强连通块的数量
int sccno[maxn];//强连通块的序号
int num[maxn];//每个强连通块的人数-以强连通块的序号为索引
int num1[maxn];//目前为止连起来的人数-以强连通块的序号为索引
int tclock;
int indegree[maxn];//强连通块的入度
int vis[maxn];
void add_edge(edge G[], int head[], int &cnt, int u, int v) {
	G[cnt].u = u;
	G[cnt].v = v;
	G[cnt].id = cnt;
	G[cnt].next = head[u];
	head[u] = cnt++;
}
void init() {
	tclock = scccnt = 0;
	mem(headG, -1);
	mem(headg, -1);
	cntg = cntG = 0;
	mem(dfn, 0);
	mem(low, 0);
	mem(num, 0);
	mem(num1, 0);
	mem(sccno, 0);
	mem(indegree, 0);
	mem(G, 0);
	mem(g, 0);
}
void tarjin(int u) {//缩点
	dfn[u] = low[u] = ++tclock;
	q.push(u);
	for (int i = headG[u]; i != -1; i = G[i].next) {
		int v = G[i].v;
		if (!dfn[v]) {
			tarjin(v);
			low[u] = min(low[u], low[v]);
		}
		else if (!sccno[v]) {
			low[u] = min(low[u], dfn[v]);
		}
	}
	if (dfn[u] == low[u]) {
		int v = -1;
		scccnt++;
		while (u != v) {
			v = q.top();
			q.pop();
			sccno[v] = scccnt;
			num[scccnt]++;
		}
	}
}
int dfs(int u) {
	vis[u] = 1;
	int sum = num[u];
	for (int i = headg[u]; i != -1; i = g[i].next) {
		int v = g[i].v;
		if (!vis[v]) {
			sum += dfs(v);
		}
	}
	return sum;
}
int main() {
	int T;
	scanf("%d", &T);
	_rep(cas, 1, T) {
		scanf("%d%d", &n, &m);
		init();
		_for(i, m) {
			int u, v;
			scanf("%d%d", &u, &v);
			add_edge(G, headG, cntG, u + 1, v + 1);
		}
		_rep(i, 1, n) {
			if (!dfn[i]) {
				tarjin(i);
			}
		}
		_rep(u, 1, n) {//把边全部逆着存一遍,为下面的深搜做准备;并且求出每个连通块的入度(针对逆边)
			for (int i = headG[u]; i != -1; i = G[i].next) {
				int v = G[i].v;
				if (sccno[u] != sccno[v]) {
					add_edge(g, headg, cntg, sccno[v], sccno[u]);
					indegree[sccno[u]]++;
				}
			}
		}
		int _max = -1;
		_rep(i, 1, scccnt) {
			if (indegree[i] == 0) {//之所以只看入度为0的连通块,是因为它处在一条链的末尾,前面积攒的票都能传到他这里,所以只看入度为0的点就能求出最大的票数。
				mem(vis, 0);
				num1[i] = dfs(i);
				_max = max(_max, num1[i]);
			}
		}
		printf("Case %d: %d\n", cas, _max - 1);
		bool f = 1;
		_rep(i, 1, n) {
			if (num1[sccno[i]] == _max) {
				if (f) {
					f = 0;
					printf("%d", i - 1);
				}
				else printf(" %d", i - 1);
			}
		}
		printf("\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值