题目链接:

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. 社工工具包 SEToolkit

    社会工程学(Social Engineering)简称社工,其通过分析攻击对象的心理弱点,利用人性的本能反应,以及任何好奇心,贪婪等心理特征进行的,使用诸如假冒,欺骗,引诱等多种手段来达成攻击目标的一 ...

  2. SWATS算法剖析(自动切换adam与sgd)

    SWATS算法剖析(自动切换adam与sgd) 战歌指挥官 搬砖.码砖.代查水表.... 27 人赞同了该文章 SWATS是ICLR在2018的高分论文,提出的一种自动由Adam切换为SGD而实现更好 ...

  3. 怎样获取Cookie

    使用 document.cookie 获取; document.cookie

  4. 原生 JS 绑定事件 移除事件

    监听事件的绑定与移除主要是addEventListener和removeEventListener的运用. addEventListener语法 element.addEventListener(ty ...

  5. 微信小程序带参数生成二维码

    wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/token', header: { 'content-type': 'application/ ...

  6. NETGEAR路由器登录不上 重新获取ip

    当NETGEAR路由器更改了"局域网IP配置",或者重启之后,会出现登录不上的情况 释放IP地址 # ipconfig /release 重新获取 # ipconfig /rene ...

  7. Spring的启动流程

    spring的启动是建筑在servlet容器之上的,所有web工程的初始位置就是web.xml,它配置了servlet的上下文(context)和监听器(Listener),下面就来看看web.xml ...

  8. java 日期。时间

    友情链接: https://www.cnblogs.com/wanson/articles/10818955.html

  9. JQuery初始加载时注册文本框失去焦点事件

    在JQuery初始加载时注册文本框失去焦点事件 $(function(){ $('#文本框ID').blur(function(){ //对文本框内容进行处理 }); });

  10. Golang等待一组协程结束

    1. 利用waitgroup import ( "log" "sync" "sync/atomic" "time" ) ...