题目链接:

https://codeforces.com/contest/999/problem/E

题意:

在有向图中加边,让$S$点可以到达所有点

数据范围:

$ 1 \leq n \leq 5000$

分析:

先从$S$点出发,所有不可达点标记一下

如果某个不可达点可以被另一个不可达点到达,那么把这个不可达点标记为可达

最后计算不可达点的数量

去年做过的题目,今年反而不会写了

ac代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 5e3 + 5;
ll mod = 998244353;
vector<int>ve[maxn];
int vis[maxn],fla[maxn];
void dfs(int x){
vis[x]=1;
for(int i=0;i<ve[x].size();i++){
int v=ve[x][i];
if(vis[v]==0)dfs(v);
}
}
int main()
{
int n,m,s,ans=0;
scanf("%d %d %d",&n,&m,&s);
for(int i=1;i<=m;i++){
int a,b;
scanf("%d %d",&a,&b);
ve[a].push_back(b);
}
dfs(s);
for(int i=1;i<=n;i++)
if(vis[i]==0)fla[i]=1;
for(int i=1;i<=n;i++){
if(fla[i]==0)continue;
for(int j=1;j<=n;j++)vis[j]=0;
dfs(i);
for(int j=1;j<=n;j++)
if(j!=i&&vis[j])fla[j]=0;
}
for(int i=1;i<=n;i++)if(fla[i])ans++;
printf("%d\n",ans);
return 0;
}

  

codeforces#999 E. Reachability from the Capital(图论加边)的更多相关文章

  1. E - Reachability from the Capital

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

  2. E. Reachability from the Capital dfs暴力

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

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

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

  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 CodeForces - 999E(强连通分量 缩点 入度为0的点)

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

  6. Reachability from the Capital

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

  7. [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环

    E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...

  8. Codeforces Round #439 (Div. 2) C DP(图论)

    C. The Intriguing Obsession time limit per test 1 second memory limit per test 256 megabytes input s ...

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

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

随机推荐

  1. 怎样创建一个canvas画布环境

    1. 由于canvas画布在网页中, 所以需要在html中添加canvas标签: <!DOCTYPE html> <html lang="en"> < ...

  2. gdb-example-ncurses

    gdb-example-ncurses http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html 1. The Prob ...

  3. 判断两个list是否元素一样

    首先创建枚举 public enum TheType { type1 = , type2 = , type3 = } 1.如果不考虑顺序,即顺序不一样,只要元素都一样即可 List<TheTyp ...

  4. Oracle学习笔记:rank、dense_rank、row_number、ntile等排序算法

    在 oracle 中有很多函数可以实现排序的功能,但是不尽相同.下面一一解说. row_number函数 功能:可实现分组排序,为数据行添加序号,多用于分页查询. 语法:row_number() ov ...

  5. vue-cli3 本地数据模拟后台接口

    vue-cli3 本地数据模拟后台接口 原理: 将本地的json数据在前端模拟为后台接口,然后调用接口,完成前端操作.在后台接通后可以直接在api配置文件中修改路径,完成前后台对接. 配置: 1.文件 ...

  6. win 10 睡眠无法唤醒

    近日遇到win10系统睡眠后无法唤醒的问题,于是网上到处搜索解决办法,试了其中几个比如回退 Intel(R) Management Engine Interface 的版本.设置电源的睡眠选项以利用休 ...

  7. nhandled rejection Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\node_cache npm ERR! cb() never called!

    安装全局包时报错,之前已经遇到过,结果第二次又忘记解决方法,果然还是要记下来,好记性不如烂笔头哇 $ npm i electron -gUnhandled rejection Error: EPERM ...

  8. vue+element-ui 项目中实现复制文字链接功能

    需求: 点击复制按钮,复制一个链接 在GitHub上找到一个clipboard组件,功能比较齐全 使用方法:   安装 npm i clipboard --save HTML <template ...

  9. gulp die('click').live('click' composer

    gulp  die('click').live('click' composer packagist.org https://getcomposer.org/ 下载后 php composer.pha ...

  10. css div嵌套层中button的margin-top不起作用解决方法

    首先声明本人资质尚浅,本文只用于个人总结.如有错误,欢迎指正.共同提高. --------------------------------------------------------------- ...