题目链接:

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. 使用X.509数字证书加密解密实务(二)-- 使用RSA证书加密敏感数据

    一.  使用RSA证书加.解密敏感数据 X.509证书标准支持三种不对称加密算法:RSA, DSA, Diffie-Hellman algorithms.最常用的是RSA算法.所以本文就以前面章节使用 ...

  2. Pattern Recognition and Machine Learning-01-Preface

    Preface Pattern recognition has its origins in engineering, whereas machine learning grew out of com ...

  3. luogu2261余数求和题解--整除分块

    题目链接 https://www.luogu.org/problemnew/show/P2261 分析 显然\(k\) \(mod\) \(i=k-\lfloor {k/i}\rfloor\) \(\ ...

  4. LeetCode 腾讯精选50题--二叉树中的最大路径和

    二叉树中的最大路径和 题目描述 给定一个非空二叉树,返回器最大路径和,路径指一条从任意节点出发,到达任意节点的序列,该路径至少包含一个节点,且不一定经过根节点 解题思路 树这一类数据结构我还不是很熟悉 ...

  5. vue runtime报错问题

    Webpack中导入vue和普通网页中导入vue的区别1. 普通网页导入vue方式 <script></script> 2. Webpack导入vue方式 Import Vue ...

  6. java Calendar Date 获取指定日期所在月或年的第一天和最后一天

    一.获取传入日期所在月的第一天 public static Date getFirstDayDateOfMonth(final Date date) { final Calendar cal = Ca ...

  7. Linux系统从新手到运维老鸟学习指南

    Linux是在1990年底到1991年由芬兰大学的学生LinusTorvalds利用Minix操作系统作为开发平台编写了内核,目前由世界各地的爱好者共同开发和维护的与UNIX兼容的操作系统,也是自由和 ...

  8. Linux学习笔记(五)Linux常用命令:压缩命令

    Linux中最常见的5中压缩格式: zip gz bz2 tar.gz tar.bz2 一..zip压缩命令 压缩文件 zip [压缩文件名] [源文件] 例如: zip zijeak.zip zij ...

  9. Vue介绍:vue导读3

    一.全局组件 二.父组件传递信息给子组件 三.子组件传递信息给父组件 四.vue项目开发 一.全局组件 <body> <!-- 两个全局vue实例可以不用注册全局组件,就可以使用 - ...

  10. [shell] shell echo打印换行的方法

    echo要支持同C语言一样的\转义功能,只需要加上参数-e,如下所示: echo -e hello \n echo \n