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

Note

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.

题意:

给定n个节点,M个有向边,和一个节点s。

问最小需要加多少个有向边可以使全部的节点都有到达s节点的路径。

思路:

把除了s节点的其他节点都缩成强连通分量,强连通分量不能到达s节点的,这个分量多加一个边即可到达。

缩成强连通分量的方法可以用dfs+并查集。

枚举每一个边的两边的节点a和b,如果a和b所在的集合(并查集维护出的集合)有一个边联通,那么把这两个节点对应的集合合并(并查集处理合并集合。)

时间复杂度:O(n*m)

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
std::vector<int> v[maxn];
int n,m,rt;
int a,b;
int par[maxn];
void init()
{
repd(i,,n)
{
par[i]=i;
}
}
int findpar(int x)
{
if(par[x]==x)
{
return x;
}else
{
return par[x]=findpar(par[x]);
}
}
int vis[maxn];
bool check(int x,int y)
{
int res=;
if(x==y)
{
return ;
}
vis[x]=;
for(auto a:v[x])
{
if(!vis[a])
{
if(check(a,y))
{
res=;
break;
}
}
}
return res;
}
int cnt[maxn];
int u[maxn];
int vv[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n>>m>>rt;
repd(i,,m)
{
cin>>a>>b;
u[i]=a;
vv[i]=b;
v[a].pb(b);
}
init();
repd(i,,m)
{
MS0(vis);
a=u[i];
b=vv[i];
a=findpar(a);
b=findpar(b);
if(a!=b&&b!=rt&&check(a,b))
{
par[a]=b;
}
}
int ans=;
repd(i,,n)
{
if(i!=rt)
{
a=findpar(i);
if(a==i)
{
ans++;
} }
}
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Reachability from the Capital CodeForces - 999E (强连通)的更多相关文章

  1. Reachability from the Capital CodeForces - 999E(强连通分量 缩点 入度为0的点)

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

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

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

  3. E - Reachability from the Capital

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

  4. E. Reachability from the Capital dfs暴力

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

  5. codeforces#999 E. Reachability from the Capital(图论加边)

    题目链接: https://codeforces.com/contest/999/problem/E 题意: 在有向图中加边,让$S$点可以到达所有点 数据范围: $ 1 \leq n \leq 50 ...

  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. CF999E Reachability from the Capital来自首都的可达性

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

  8. [CF999E]Reachability from the Capital

    题目大意:有一个$n$个点$m$条边的有向图,起点$S$,要求你添加最少的边使得$S$可以到达所有点 题解:缩点,答案就是没有入边的强连通分量个数,注意,如果起点$S$所在的强连通块没有入边则不计入答 ...

  9. E. Reachability from the Capital(tarjan+dfs)

    求联通分量个数,在dfs一次 #include <iostream> #include <algorithm> #include <cstring> #includ ...

随机推荐

  1. webpack4.0各个击破(2)—— CSS篇

    webpack作为前端最火的构建工具,是前端自动化工具链最重要的部分,使用门槛较高.本系列是笔者自己的学习记录,比较基础,希望通过问题 + 解决方式的模式,以前端构建中遇到的具体需求为出发点,学习we ...

  2. 在win10系统开启linux子系统

    1. 2.重启计算机 3.在winstore下载和安装 ubuntu 4.查看当前win10子系统的linux版本 lsb_release -a 5.设置root账号密码, 在终端输入命令 sudo ...

  3. SQL使用总结

    本文为转载:对于SQL的学习与使用,推荐大家去这儿,讲的很系统: http://www.w3school.com.cn/sql/index.asp 练习SQL的使用,推荐大家去这里: https:// ...

  4. GsonFormat插件

    GsonFormat插件可以根据JSONObject格式的字符串,自动生成实体类参数. 要使用这个插件,首先要做的事下载它.方法如下: 方法一: 1.Android studio File->S ...

  5. Bootstrap-table 部分浏览器显示不出来

    一.问题 近日,写了一个ASP.Net项目,但是bootstrap-table在别人的电脑上显示不出来,在自己的电脑上能显示,有些浏览器也是能显示,但部分浏览器就是显示不出来.找了很多原因,最后有个老 ...

  6. java基础中this,super

    this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针. super可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类. this的用法就不累赘的说 ...

  7. Android注解神器 ButterKnife框架

    前言: 本人是一个只有几个月工作经验的码小渣.这是我写的第一篇博客,如有不足之处还请大家不要介意,还请大佬可以指出问题. 在这几个月的实战开发中自己也遇到了很多问题,真的是举步艰难啊!!! 在实战开发 ...

  8. JavaScript中的闭包和作用域链

    这部分几乎是JavaScript中最难的部分,也是面试官最爱问的地方. 下面的内容是我以前写的<JavaScript学习手册>中被客户删除的部分,理由听起来有点诡异:太难.

  9. git 的 origin 的含义

    我们从progit 一书中可以看到: 远程仓库名字 “origin” 与分支名字 “master” 一样,在 Git 中并没有任何特别的含义一样. 同时“master”是当你运行git init时默认 ...

  10. Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助

    Python第二天  变量  运算符与表达式  input()与raw_input()区别  字符编码  python转义符  字符串格式化  format函数字符串格式化  帮助 目录 Pychar ...