题目大意:有一个$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的更多相关文章

  1. CF999E Reachability from the Capital来自首都的可达性

    题目大意: 有n个节点m条边,边都是单向的,请你添加最少的边使得起点s到其他与其他每一个点之间都能互相到达 这题一看就是一个缩点啊 其实对于原有的m条边相连的一些点,如果之前他们已经形成了强连通分量( ...

  2. E - Reachability from the Capital

    E - Reachability from the Capital  CodeForces - 999E 题目链接:https://vjudge.net/contest/236513#problem/ ...

  3. E. Reachability from the Capital dfs暴力

    E. Reachability from the Capital 这个题目就是给你一个有向图,给你起点,问增加多少条边让这个图变成一个连通图. 这个因为n只有5000m只有5000 所以可以暴力枚举这 ...

  4. Reachability from the Capital CodeForces - 999E (强连通)

    There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in Berla ...

  5. Reachability from the Capital

    题目描述 There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in ...

  6. Reachability from the Capital CodeForces - 999E(强连通分量 缩点 入度为0的点)

    题意: 问至少加几条边 能使点s可以到达所有的点 解析: 无向图的连通分量意义就是  在这个连通分量里 没两个点之间至少有一条可以相互到达的路径 所以 我们符合这种关系的点放在一起, 由s向这些点的任 ...

  7. Reachability from the Capital(Codeforces Round #490 (Div. 3)+tarjan有向图缩点)

    题目链接:http://codeforces.com/contest/999/problem/E 题目: 题意:给你n个城市,m条单向边,问你需要加多少条边才能使得从首都s出发能到达任意一个城市. 思 ...

  8. E. Reachability from the Capital(tarjan+dfs)

    求联通分量个数,在dfs一次 #include <iostream> #include <algorithm> #include <cstring> #includ ...

  9. codeforces#999 E. Reachability from the Capital(图论加边)

    题目链接: https://codeforces.com/contest/999/problem/E 题意: 在有向图中加边,让$S$点可以到达所有点 数据范围: $ 1 \leq n \leq 50 ...

随机推荐

  1. Maven学习总结(10)

    本文通过一个例子来介绍利用maven来构建一个多模块的jave项目.开发工具:intellij idea. 一.项目结构 multi-module-project是主工程,里面包含两个模块(Modul ...

  2. FAT32中文版分析+补充(2)

    从Offset 36(0x24)开始FAT12/16的内容开始区别于FAT32,现在分两个表格列出来,下表为FAT12/16的内容: 名称 Offset(Byte) 大小(Byte) 描述 BS_dr ...

  3. Java分享笔记:泛型机制的程序演示

    package packA; import java.util.*; public class GenericDemo { public static void main(String[] args) ...

  4. motto - question - bodyParser.urlencoded 中设置 extended 为 true 和 false 有什么区别吗?

    本文搜索关键字:motto node nodejs js javascript body-parser bodyparser urlencoded x-www-form-urlencoded exte ...

  5. Q&A - Nginx是做什么的?tomcat结合Nginx使用小结

    相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...

  6. Nagios 监控Windows服务器(详细篇)

    1. 监控内容 windows服务器的内部参数包括以下 a. 内存使用状况 b. CPU负载 c. 磁盘使用状况 d. 服务状态 e. 运行的进程 2. 监控原理 在windows服务器内安装NSCl ...

  7. 银行卡验证API

    一.银联开放平台 https://open.unionpay.com/tjweb/api/detail?apiSvcId=21 应用场景 综合数据服务平台是银联为接入商户提供的综合数据认证服务接口,目 ...

  8. (转)Clang 比 GCC 编译器好在哪里?

    编译速度更快.编译产出更小.出错提示更友好.尤其是在比较极端的情况下.两年多前曾经写过一个Scheme解释器,词法分析和语法解析部分大约2000行,用的是Boost.Spirit--一个重度依赖C++ ...

  9. hosts 文件的位置及作用

    一.位置 1.Window系统位置 C:\Windows\System32\drivers\etc 2.Linux系统位置 /etc/hosts 二.作用 综述:Hosts文件中指定了域名和IP地址的 ...

  10. 原子操作和volatile关键字

    原子操作:不可被中断的操作.要么全执行,要么全不执行. 现代CPU读取内存,通过读取缓存再写入主存.先去主存读--->写入缓存---->运行线程--->写入缓存---->写入主 ...