码迷,mamicode.com
首页 > Windows程序 > 详细

[CF999E]Reachability from the Capital

时间:2018-10-05 16:12:27      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:col   bool   bsp   size   cst   clu   from   oid   起点   

题目大意:有一个$n$个点$m$条边的有向图,起点$S$,要求你添加最少的边使得$S$可以到达所有点

题解:缩点,答案就是没有入边的强连通分量个数,注意,如果起点$S$所在的强连通块没有入边则不计入答案

卡点:

 

C++ Code:

#include <cstdio>
#define maxn 5010
#define maxm 5010
int head[maxn], cnt;
struct Edge {
	int from, to, nxt;
} e[maxm];
inline void add(int a, int b) {
	e[++cnt] = (Edge) {a, b, head[a]}; head[a] = cnt;
}

int DFN[maxn], low[maxn], idx;
int S[maxn], top, res[maxn], CNT;
bool ins[maxn];
inline int min(int a, int b) {return a < b ? a : b;}
void tarjan(int u) {
	DFN[u] = low[u] = ++idx;
	ins[S[++top] = u] = true;
	int v;
	for (int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].to;
		if (!DFN[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if (ins[v]) low[u] = min(low[u], DFN[v]);
	}
	if (DFN[u] == low[u]) {
		CNT++;
		do {
			ins[v = S[top--]] = false;
			res[v] = CNT;
		} while (u != v);
	}
}
int n, m, s;
int ind[maxn];
int main() {
	scanf("%d%d%d", &n, &m, &s);
	for (int i = 0, a, b; i < m; i++) {
		scanf("%d%d", &a, &b);
		add(a, b);
	}
	for (int i = 1; i <= n; i++) if (!DFN[i]) tarjan(i);
	for (int i = 1; i <= cnt; i++) {
		int u = res[e[i].from], v = res[e[i].to];
		if (u != v) ind[v]++;
	}
	int ans = 0;
	for (int i = 1; i <= CNT; i++) if (!ind[i]) ans++;
	printf("%d\n", ans - (!ind[res[s]]));
	return 0;
}

  

[CF999E]Reachability from the Capital

标签:col   bool   bsp   size   cst   clu   from   oid   起点   

原文地址:https://www.cnblogs.com/Memory-of-winter/p/9744793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!