【题解】CF999E:Reachability from the Capital

原题传送门
这是道水题
先缩点,然后把新图中入度为0的点个数统计出来,就是答案
注意的是如果起点 s s s所在的强连通分量入度为0,不能统计入答案
Code:

#include <bits/stdc++.h>
#define maxn 5010
using namespace std;
struct Edge{
	int to, next;
}edge[maxn << 1];
struct Line{
	int x, y;
}line[maxn];
int num, head[maxn], dfn[maxn], low[maxn], Index, vis[maxn], top, sta[maxn], tot, color[maxn];
int n, m, s, deg[maxn], ans;

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
} 

void addedge(int x, int y){ edge[++num] = (Edge){ y, head[x] }; head[x] = num; }

void tarjan(int u){
	dfn[u] = low[u] = ++Index;
	vis[u] = 1, sta[++top] = u;
	for (int i = head[u]; i; i = edge[i].next){
		int v = edge[i].to;
		if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]); else
		if (vis[v]) low[u] = min(low[u], dfn[v]);
	}
	if (dfn[u] == low[u]){
		++tot;
		while (sta[top + 1] != u) vis[sta[top]] = 0, color[sta[top--]] = tot;
	}
}

int main(){
	n = read(), m = read(), s = read();
	for (int i = 1; i <= m; ++i){
		line[i].x = read(), line[i].y = read();
		addedge(line[i].x, line[i].y);
	}
	for (int i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i);
	for (int i = 1; i <= m; ++i) if (color[line[i].x] != color[line[i].y]) ++deg[color[line[i].y]];
	for (int i = 1; i <= tot; ++i) if (!deg[i] && color[s] != i) ++ans;
	printf("%d\n", ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值