Reachability from the Capital
题目描述
There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.
What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?
New roads will also be one-way.
Input
The first line of input consists of three integers nn, mm and ss (1≤n≤5000,0≤m≤5000,1≤s≤n1≤n≤5000,0≤m≤5000,1≤s≤n) — the number of cities, the number of roads and the index of the capital. Cities are indexed from 11 to nn.
The following mm lines contain roads: road ii is given as a pair of cities uiui, vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi). For each pair of cities (u,v)(u,v), there can be at most one road from uu to vv. Roads in opposite directions between a pair of cities are allowed (i.e. from uu to vv and from vv to uu).
Output
Print one integer — the minimum number of extra roads needed to make all the cities reachable from city ss. If all the cities are already reachable from ss, print 0.
Examples
Input
9 9 1
1 2
1 3
2 3
1 5
5 6
6 1
1 8
9 8
7 1
Output
3
Input
5 4 5
1 2
2 3
3 4
4 1
Output
1
The first example is illustrated by the following:
For example, you can add roads (6,46,4), (7,97,9), (1,71,7) to make all the cities reachable from s=1s=1.
The second example is illustrated by the following:
In this example, you can add any one of the roads (5,15,1), (5,25,2), (5,35,3), (5,45,4) to make all the cities reachable from s=5s=5.
题解:
强连通缩点后统计入度为0的个数ans,然后看首都的入度是否为0;如果是则ans-1;
#include<cstdio>
#include <algorithm>
#include <stack>
#include <vector>
#include <cstring>
using namespace std; const int MAXN=1e5+;
const int inf=0x3f3f3f3f;
struct node{
int to;
int next;
}edge[MAXN*];
int head[MAXN];
int val[MAXN];
bool instack[MAXN];
int cnt;
int dfn[MAXN],low[MAXN];
int sum[MAXN];
void add(int x,int y)
{
edge[++cnt].to =y;
edge[cnt].next=head[x];
head[x]=cnt;
}
int Time,num;
stack<int >st;
int du[MAXN];
int color[MAXN];
int x[MAXN],y[MAXN];
void tarjan(int u)
{
dfn[u]=low[u]= ++Time;
st.push(u);
instack[u]=true;
for (int i = head[u]; i !=- ; i=edge[i].next) {
int v=edge[i].to;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int x;
num++;
while() {
x=st.top();
st.pop();
color[x]=num;
instack[x]=false;
if(x==u) break;
} }
} int main()
{
int n,m,s;
scanf("%d%d%d",&n,&m,&s);
cnt=;
memset(head,-,sizeof(head));
memset(instack,false, sizeof(instack));
memset(sum, ,sizeof(sum));
for (int i = ; i <=m ; ++i) {
scanf("%d%d",&x[i],&y[i]);
add(x[i],y[i]);
}
for (int i = ; i <=n ; ++i) {
if(!dfn[i]) tarjan(i);
}
for (int i = ; i <=m ; ++i) {
if(color[x[i]]!=color[y[i]])
{
du[color[y[i]]]++;
}
} int ans=;
for (int i = ; i <=num ; ++i) {
if(du[i]==) ans++;
}
if(du[color[s]]==) ans--;
printf("%d\n",ans);
return ;
}
Reachability from the Capital的更多相关文章
- E - Reachability from the Capital
E - Reachability from the Capital CodeForces - 999E 题目链接:https://vjudge.net/contest/236513#problem/ ...
- E. Reachability from the Capital dfs暴力
E. Reachability from the Capital 这个题目就是给你一个有向图,给你起点,问增加多少条边让这个图变成一个连通图. 这个因为n只有5000m只有5000 所以可以暴力枚举这 ...
- 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 ...
- CF999E Reachability from the Capital来自首都的可达性
题目大意: 有n个节点m条边,边都是单向的,请你添加最少的边使得起点s到其他与其他每一个点之间都能互相到达 这题一看就是一个缩点啊 其实对于原有的m条边相连的一些点,如果之前他们已经形成了强连通分量( ...
- Reachability from the Capital CodeForces - 999E(强连通分量 缩点 入度为0的点)
题意: 问至少加几条边 能使点s可以到达所有的点 解析: 无向图的连通分量意义就是 在这个连通分量里 没两个点之间至少有一条可以相互到达的路径 所以 我们符合这种关系的点放在一起, 由s向这些点的任 ...
- Reachability from the Capital(Codeforces Round #490 (Div. 3)+tarjan有向图缩点)
题目链接:http://codeforces.com/contest/999/problem/E 题目: 题意:给你n个城市,m条单向边,问你需要加多少条边才能使得从首都s出发能到达任意一个城市. 思 ...
- [CF999E]Reachability from the Capital
题目大意:有一个$n$个点$m$条边的有向图,起点$S$,要求你添加最少的边使得$S$可以到达所有点 题解:缩点,答案就是没有入边的强连通分量个数,注意,如果起点$S$所在的强连通块没有入边则不计入答 ...
- E. Reachability from the Capital(tarjan+dfs)
求联通分量个数,在dfs一次 #include <iostream> #include <algorithm> #include <cstring> #includ ...
- codeforces#999 E. Reachability from the Capital(图论加边)
题目链接: https://codeforces.com/contest/999/problem/E 题意: 在有向图中加边,让$S$点可以到达所有点 数据范围: $ 1 \leq n \leq 50 ...
随机推荐
- FormsAuthentication IsAuthenticated一直为false
解决办法: 在Web.Config中添加一下红框的内容
- Android图表库XCL-Charts
首先,这个是国人开发的,支持下必须顶!github项目地址:点击打开,由于项目的基本功能已经实现,所以项目作者也说以后基本不会在有更新了. 项目简介: Android图表库(XCL-Charts is ...
- let和const命令整理
一.let命令 基本用法 ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. for循环的计数器,就很合适使用let命令. for循环还 ...
- This is your path and you will pursue it with excellence.
This is your path and you will pursue it with excellence.自己选的路就要走出精彩.
- MeshLab中插件的添加过程
MeshLab中主要插件类型有 filter plugins, i/o plugins, edit plugins,这些插件实现了MeshLab的大部分功能.新加入的插件命名规则最好也遵循规范,可命名 ...
- C#,什么是Attribute?什么特性?怎么被调用?
定制特性attribute,本质上是一个类,其为目标元素提供关联附加信息,并在运行期以反射的方式来获取附加信息(获取到特性类),相当于优雅的为元素添加了一个tag,这个tag是一个类. Attribu ...
- X86/X64 函数调用约定
C 语言有 __cdecl.__stdcall.__fastcall.naked.__pascal. C++ 语言有 __cdecl.__stdcall.__fastcall.naked.__pasc ...
- linux 命令——18 locate (转)
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...
- 打造颠覆你想象中的高性能,轻量级的webform框架-----如何替换webform的垃圾控件(第一天)
前文描述: 随着.net 推出 MVC框架以来,webform 与 mvc 的争论一直没有停止过,一直以来 mvc 的 拥护者远远高于 webform,但是webfrom的有些优势又是mvc而无法替 ...
- 【洛谷1337】[JSOI2004] 吊打XXX(模拟退火经典题)
点此看题面 大致题意: 一个平面上有\(n\)个点,每个点有1个权值,现在要选择平面上的一个点,使这\(n\)个点的权值乘上到达选定点的距离之和最小. 模拟退火 我们可以用模拟退火来做这道题. 先将\ ...