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 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
9 9 1
1 2
1 3
2 3
1 5
5 6
6 1
1 8
9 8
7 1
3
5 4 5
1 2
2 3
3 4
4 1
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 (强连通)的更多相关文章
- 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出发能到达任意一个城市. 思 ...
- 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 所以可以暴力枚举这 ...
- codeforces#999 E. Reachability from the Capital(图论加边)
题目链接: https://codeforces.com/contest/999/problem/E 题意: 在有向图中加边,让$S$点可以到达所有点 数据范围: $ 1 \leq n \leq 50 ...
- Reachability from the Capital
题目描述 There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in ...
- CF999E Reachability from the Capital来自首都的可达性
题目大意: 有n个节点m条边,边都是单向的,请你添加最少的边使得起点s到其他与其他每一个点之间都能互相到达 这题一看就是一个缩点啊 其实对于原有的m条边相连的一些点,如果之前他们已经形成了强连通分量( ...
- [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 ...
随机推荐
- 跨站点请求伪造(CSRF)学习
一.CSRF介绍 伪造一个站点,在站点中伪造一个向其他站点的请求,在用户访问该站点时让用户执行 假设有如下URL能删除一篇文章: 攻击者在自己的域中构造一个页面: 内容为: 使用一个img标签,其地址 ...
- NLP入门(六)pyltp的介绍与使用
pyltp的简介 语言技术平台(LTP)经过哈工大社会计算与信息检索研究中心 11 年的持续研发和推广, 是国内外最具影响力的中文处理基础平台.它提供的功能包括中文分词.词性标注.命名实体识别.依 ...
- 第56章 Client - Identity Server 4 中文文档(v1.0.0)
该Client模型的OpenID Connect或OAuth 2.0 客户端-例如,本地应用,Web应用程序或基于JS的应用程序. 56.1 Basics Enabled 指定是否启用客户端.默认为t ...
- 杭电ACM2003--求绝对值
求绝对值 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 用php输出心形曲线
<?php for($t=0;$t<360;$t++) { $y=2*cos($t)-cos(2*$t); //笛卡尔心形曲线函数 $x=2*sin($t)-sin(2*$t); $x+= ...
- Netty学习笔记(一) 实现DISCARD服务
官方那个给出的介绍是:Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序.然后我们简单理解 ...
- 基于Git项目管理客户端SourceTree的免注册安装及远程连接方法
作为程序员,不可避免的要在github上查询代码,而在企业项目中,为了使得项目好管理需要使用项目管理客户端,所以接下来详细讲解一下基于git的sourceTree在windows系统下的安装及与Git ...
- Linux 自动化部署DNS服务器
Linux 自动化部署DNS服务器 1.首先配置主DNS服务器的IP地址,DNS地址一个写主dns的IP地址,一个写从dns的地址,这里也可以不写,在测试的时候在/etc/resolv.conf中添加 ...
- C语言运行库翻译
这是从Visual C++ 6里面的C语言部分翻译过来. http://files.cnblogs.com/files/sishenzaixian/C运行库.zip
- 关于clone(java.lang.Object)重写
1. 需要实现接口java.lang.Cloneable 2. 重写java.lang.Object的clone 3. clone访问权限扩大为public 4. 不实现(java.lang.Clon ...