hdu 3836 Equivalent Sets

本文介绍了一种使用Tarjan算法进行缩点,解决给定有向图最少加多少条边使其成为强连通图的问题。通过实现Tarjan_scc结构,完成初始化、边添加、构建图、Tarjan算法执行及最终问题解答。

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=3836  

Equivalent Sets

Description

To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
Now you want to know the minimum steps needed to get the problem proved.

Input

The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.

Output

For each case, output a single integer: the minimum steps needed.

Sample Input

4 0
3 2
1 2
1 3

Sample Output

4
3

题目大意:给你一张有向图要求最少加多少条边时该图变成强连通图。
Tarjan缩点。。

#include<bits/stdc++.h>
using namespace std;
const int N = 20100;
struct Tarjan_scc {
	stack<int> s;
	bool instack[N];
	struct edge { int to, next; }G[N * 3];
	int idx, scc, tot, in[N], out[N], dfn[N], low[N], head[N], sccnum[N];
	inline void init(int n) {
		idx = scc = tot = 0;
		while (!s.empty()) s.pop();
		for (int i = 0; i < n + 2; i++) {
			head[i] = -1;
			instack[i] = false;
			in[i] = out[i] = dfn[i] = low[i] = sccnum[i] = 0;
		}
	}
	inline void add_edge(int u, int v) {
		G[tot].to = v, G[tot].next = head[u], head[u] = tot++;
	}
	inline void built(int m) {
		int u, v;
		while (m--) {
			scanf("%d %d", &u, &v);
			add_edge(u, v);
		}
	}
	inline void tarjan(int u) {
		dfn[u] = low[u] = ++idx;
		instack[u] = true;
		s.push(u);
		for (int i = head[u]; ~i; i = G[i].next) {
			int &v = G[i].to;
			if (!dfn[v]) {
				tarjan(v);
				low[u] = min(low[u], low[v]);
			} else if (instack[v] && dfn[v] < low[u]) {
				low[u] = dfn[v];
			}
		}
		if (dfn[u] == low[u]) {
			int v = 0;
			scc++;
			do {
				v = s.top(); s.pop();
				instack[v] = false;
				sccnum[v] = scc;
			} while (u != v);
		}
	}
	inline void solve(int n, int m) {
		init(n);
		built(m);
		for (int i = 1; i <= n; i++) {
			if (!dfn[i]) tarjan(i);
		}
		int x1 = 0, x2 = 0;
		for (int u = 1; u <= n; u++) {
			for (int i = head[u]; ~i; i = G[i].next) {
				int v = G[i].to;
				if (sccnum[u] != sccnum[v]) {
					in[sccnum[v]]++;
					out[sccnum[u]]++;
				}
			}
		}
		for (int i = 1; i <= scc; i++) {
			if (!in[i]) x1++;
			if (!out[i]) x2++;
		}
		printf("%d\n", 1 == scc ? 0 : max(x1, x2));
	}
}go;
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int n, m;
	while (~scanf("%d %d", &n, &m)) {
		go.solve(n, m);
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/GadyPu/p/5003447.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值