题目大意:有一个$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. LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal

    题目 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3 ...

  2. docker-compose 构建mongodb并导入基础数据示例

    使用docker-compose构建mongodb服务并导入基础数据示例. 1.文件目录结构 ——mongo/ |——docker-compose.yml |——mongo-Dockerfile |— ...

  3. HTTP协议中request报文请求方法和状态响应码

    一个HTTP请求报文由4部分组成: 请求行(request line) 请求头部(header) 空行 请求数据 下图给出了请求报文的一般格式: 请求行中包括了请求方法,常见的请求方法有: GET:从 ...

  4. linuxC编程介绍

    第一步:写完程序 /first.c/ #include <stdio.h> int main() { printf("hello,welcome to the LinuxC!\n ...

  5. C#简单的文件阅读器

    写一个简单的文件阅读器  1.可以读取大文件(2G)2.实现首页.下一页.前一页.末页的跳转3.实现到指定页面的跳转,比如跳转到第**页4.限制每页显示字符数 1029-4069byte,且用户可自定 ...

  6. C语言实例解析精粹学习笔记——29

    题目: 将字符行内单字之间的空格平均分配插入到单字之间,以实现字符行排版.也就是输入一个英文句子,单词之间的空格数目不同,将这些空格数平均分配到单词之间,重新输出. 代码如下(是原书中配套的代码,只是 ...

  7. PAT (Basic Level) Practice 1006 换个格式输出整数

    个人练习 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(&lt10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为 ...

  8. 对文件 I/O,标准 I/O 的缓冲的理解

    1.标准I/O缓冲区 要理解标准I/O,就要先知道文件I/O的业务逻辑. 下面图示为文件I/O 如执行下面的代码: write(fd, buf2, sizeof(buf2)); 图中 buf:就是bu ...

  9. IDA 对 so 的动态调试

    将IDAPro根目录下dbgsrv 目录下的android_server(模拟器用android_x86_server,这里还是用真机好点)文件push 到安卓设备(比如/data/local/tmp ...

  10. fromkeys语法/set集合/深浅拷贝/列表/字典的删除

    fromkeys语法: dic = {"apple":"苹果", "banana":"香蕉"} 返回新字典. 和原来的没 ...