POJ 1236 Network of Schools(强连通分量)

简介:

POJ 1236 Network of Schools

题目链接

题意:题意本质上就是,给定一个有向图,问两个问题
1、从哪几个顶点出发,能走全全部点
2、最少连几条边,使得图强连通

思路:

#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;

const int N = 105;

int n;
vector<int> g[N];

int pre[N], sccno[N], dfn[N], dfs_clock, sccn;
stack<int> S;

void dfs_scc(int u) {
	pre[u] = dfn[u] = ++dfs_clock;
	S.push(u);
	for (int i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		if (!pre[v]) {
			dfs_scc(v);
			dfn[u] = min(dfn[u], dfn[v]);
		} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
	}
	if (dfn[u] == pre[u]) {
		sccn++;
		while (1) {
			int x = S.top(); S.pop();
			sccno[x] = sccn;
			if (x == u) break;
		}
	}
}

void find_scc() {
	sccn = dfs_clock = 0;
	memset(pre, 0, sizeof(pre));
	memset(sccno, 0, sizeof(sccno));
	for (int i = 1; i <= n; i++)
		if (!pre[i]) dfs_scc(i);
}

int in[N], out[N];

int main() {
	while (~scanf("%d", &n)) {
		int v;
		for (int i = 1; i <= n; i++) g[i].clear();
		int cnt = n;
		for (int u = 1; u <= n; u++) {
			while (~scanf("%d", &v) && v)
				g[u].push_back(v);
		}
		find_scc();
		memset(in, 0, sizeof(in));
		memset(out, 0, sizeof(out));
		for (int u = 1; u <= n; u++) {
			for (int j = 0; j < g[u].size(); j++) {
				int v = g[u][j];
				if (sccno[u] != sccno[v]) {
					in[sccno[v]]++;
					out[sccno[u]]++;
				}
			}
		}
		int ins = 0, outs = 0;
		for (int i = 1; i <= sccn; i++) {
			if (!in[i]) ins++;
			if (!out[i]) outs++;
		}
		int ans1 = ins, ans2 = max(ins, outs);
		if (sccn == 1) ans2 = 0;
		printf("%d\n%d\n", ans1, ans2);
	}
	return 0;
}


版权声明:本文博主原创文章,博客,未经同意不得转载。






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4875337.html,如需转载请自行联系原作者


相关文章
UVa11710 - Expensive subway(最小生成树)
UVa11710 - Expensive subway(最小生成树)
46 0
UVa10075 - Airlines(所有点对之间的最短距离)
UVa10075 - Airlines(所有点对之间的最短距离)
43 0
[POJ 1236] Network of Schools | Tarjan缩点
Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”).
140 0
[POJ 1236] Network of Schools | Tarjan缩点
|
Go
[Nowcoder / POJ2728] 最优比率生成树 | 二分 + prim
有n个点,其中,每个点给出位置坐标( x , y ) 以及高度z ,两点之间的距离为两点之间的欧几里得距离 两点之间建立一条路的代价为两点之间的高度差,问将n 个点联通的情况下,求出最大的cost/dis
132 0