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

 

### 强连通分量算法模板 强连通分量(Strongly Connected Component, SCC)是指在一个有向图中,任意两个节都可以互相到达的最大子图。通过 Tarjan 算法可以高效地找到这些强连通分量并将其成单个节,从而简化原图结构。 以下是基于 Tarjan 算法的 C++ 实现代码模板: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; // 节数量上限 int n, m; vector<int> adj[MAXN]; // 邻接表存储图 stack<int> stk; // 辅助栈 bool in_stack[MAXN]; // 判断当前节是否在栈中 int dfn[MAXN], low[MAXN]; // 时间戳数组和追溯值数组 int scc_id[MAXN]; // 存储每个节所属的强连通分量编号 int idx = 0, cnt_scc = 0; // 当前时间戳计数器 和 强连通分量计数器 void tarjan(int u) { dfn[u] = low[u] = ++idx; // 初始化dfn和low stk.push(u); // 将u压入栈 in_stack[u] = true; // 标记u已在栈中 for (auto &v : adj[u]) { // 枚举所有邻接 if (!dfn[v]) { // 如果v未访问过 tarjan(v); low[u] = min(low[u], low[v]); // 更新low[u] } else if (in_stack[v]) { // 如果v已经在栈中 low[u] = min(low[u], dfn[v]); } } if (dfn[u] == low[u]) { // 找到一个新的强连通分量 ++cnt_scc; // 新增一个SCC编号 while (true) { // 提取该SCC中的所有节 int v = stk.top(); stk.pop(); in_stack[v] = false; scc_id[v] = cnt_scc; // 记录属于哪个SCC if (v == u) break; // 当弹出的是u时结束循环 } } } // 主函数调用部分 void solve() { cin >> n >> m; // 输入节数和边数 for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; // 输入一条有向边 adj[u].push_back(v); } memset(dfn, 0, sizeof(dfn)); // 初始化dfn数组 memset(in_stack, 0, sizeof(in_stack)); idx = cnt_scc = 0; for (int i = 1; i <= n; ++i) { // 对每个节运行tarjan if (!dfn[i]) tarjan(i); } cout << "Total Strongly Connected Components: " << cnt_scc << "\n"; } ``` #### 关键说明 上述代码实现了 Tarjan 算法的核心逻辑[^2]。 - `dfn` 数组记录了每个节第一次被访问的时间戳。 - `low` 数组用于追踪能够回溯到的最早祖先节的时间戳。 - 使用辅助栈来保存当前路径上的节,并判断哪些节已经形成一个完整的强连通分量。 - 每次当发现某个节满足条件 `dfn[u] == low[u]` 时,表示找到了一个新的强连通分量[^3]。 完成 Tarjan 算法后,可以通过构建新的后的图来进行进一步处理。具体做法如下: - 创建新图,其中每个节代表原始图的一个强连通分量。 - 统计每条边连接的不同强连通分量之间的关系,忽略内部重复边。 --- ### 后的应用实例 假设我们需要计算拓扑序或者解决某些动态规划问题,在之后的新图上操作会更加简单有效。例如,对于 DAG 上的经典 DP 或最短路问题,可以直接在此基础上展开分析。 ```cpp vector<vector<int>> new_adj(cnt_scc + 1); // 后的新图 long long indegree[cnt_scc + 1] = {}; // 入度统计 for (int u = 1; u <= n; ++u) { for (auto &v : adj[u]) { if (scc_id[u] != scc_id[v]) { // 只考虑跨不同SCC的边 new_adj[scc_id[u]].push_back(scc_id[v]); indegree[scc_id[v]]++; } } } ``` 此段代码展示了如何根据原有图生成后的图结构[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值