tarjan算法,反向建图,HUD:Hawk-and-Chicken

Problem - 3639 (hdu.edu.cn)

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
 

Author
Dragon
 

Source

解析 :

tarjan算法缩点,然后反响建图,dfs遍历统计

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
const int N = 5e3 + 5, M = 3e4 + 5;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int id[N], siz[N], scc_cnt;
int stk[N], top;
bool instk[N];
int din[N];
vector<int>G[N];
int d[N];
bool v[N];

void add(int a, int b) {
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void tarjan(int u) {
	dfn[u] = low[u] = ++timestamp;
	stk[++top] = u;
	instk[u] = 1;
	for (int i = h[u]; i != -1; i = ne[i]) {
		int j = e[i];
		if (!dfn[j]) {
			tarjan(j);
			low[u] = min(low[u], low[j]);
		}
		else if (instk[j]) {
			low[u] = min(low[u], dfn[j]);
		}
	}
	if (dfn[u] == low[u]) {
		scc_cnt++;
		int y;
		do {
			y = stk[top--];
			instk[y] = 0;
			id[y] = scc_cnt;
			siz[scc_cnt]++;
		} while (y != u);
	}
}
int sum = 0;
void dfs(int u) {
	for (int i = 0; i < G[u].size(); i++) {
		int j = G[u][i];
		if (v[j])continue;
		v[j] = 1;
		dfs(j);
		sum += siz[j];
	}
}

int main() {
	int T;
	cin >> T;
	int cnt = 0;
	while (T--) {
		cnt++;
		scanf("%d%d", &n, &m);
		memset(h, -1, sizeof h);
		memset(instk, 0, sizeof instk);
		memset(dfn, 0, sizeof dfn);
		memset(siz, 0, sizeof siz);
		timestamp = top = idx = scc_cnt = 0;
		memset(d, 0, sizeof d);
		memset(din, 0, sizeof din);
		for (int i = 1, a, b; i <= m; i++) {
			scanf("%d%d", &a, &b);
			add(a, b);
		}
		for (int i = 0; i < n; i++) {
			if (!dfn[i])
				tarjan(i);
		}
		for (int i = 0; i < n; i++) {
			for (int j = h[i]; j != -1; j = ne[j]) {
				int k = e[j];
				if (id[i] != id[k]) {
					din[id[i]]++;
					G[id[k]].push_back(id[i]);
				}
			}
		}
		int ans = 0;
		for (int i = 1; i <= scc_cnt; i++) {
			if (din[i] == 0) {
				dfs(i);
				d[i] = sum + siz[i] - 1;
				sum = 0;
				ans = max(ans, d[i]);
				for (int j = 0; j <= scc_cnt; j++) {
					v[j] = 0;
				}
			}
		}
		int flg = 0;
		printf("Case %d: %d\n", cnt, ans);
		for (int i = 0; i < n; i++) {
			if (d[id[i]] == ans) {
				if (flg)printf(" ");
				printf("%d", i);
				flg = 1;
			}
		}
		printf("\n");
		for (int i = 0; i <= n; i++)
			G[i].clear();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值