CF999E Reachability from the Capital来自首都的可达性
题目大意:
有n个节点m条边,边都是单向的,请你添加最少的边使得起点s到其他与其他每一个点之间都能互相到达
这题一看就是一个缩点啊
其实对于原有的m条边相连的一些点,如果之前他们已经形成了强连通分量(scc),那么它们之前就可以相互到达(不用修路),对于这些点我们可以把它们“缩”成一个“点”,这其实就是Tarjian缩点的思想
其实luogu里还有很多缩点的模板题,自己去找找吧,都不难的
那么如果你会了缩点,这个题只要缩完点之后统计一下入度为0的点就行了(让强连通分量之间连边)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
const int inf=1e9+;
inline int read()
{
int p=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){p=p*+c-'';c=getchar();}
return f*p;}
const int maxn=;
const int maxm=;
struct Edge
{
int next,from,to;
}p[maxm];
struct point
{
int low,dnf,vis,fa,in;
}A[maxn];
int n,m,cnt,sum_scc,tot,S;
int Stack[maxn],top,ans,head[maxm];
inline void add_edge(int x,int y)//加边
{
cnt++;
p[cnt].from=head[x];
head[x]=cnt;
p[cnt].next=x;
p[cnt].to=y;
}
inline void Tarjian(int x)//Tarjian缩点
{
A[x].dnf=A[x].low=++tot;
A[x].vis=,Stack[++top]=x;
for(int i=head[x];i;i=p[i].from)
{
int y=p[i].to;
if(!A[y].dnf)
Tarjian(y),A[x].low=min(A[x].low,A[y].low);
else if(A[y].vis)
A[x].low=min(A[x].low,A[y].dnf);
}
if(A[x].dnf==A[x].low)
{
int y;
sum_scc++;
while(y=Stack[top--])
{
A[y].vis=;
A[y].fa=sum_scc;
if(x==y)break;
}
}
}
int main()
{
n=read(),m=read(),S=read();
for(int i=;i<=m;i++)
{
int x=read(),y=read();
add_edge(x,y);
}
for(int i=;i<=n;i++)
if(!A[i].dnf)Tarjian(i);
for(int i=;i<=m;i++)//统计入度
{
int x=A[p[i].next].fa,y=A[p[i].to].fa;
if(x!=y)A[y].in++;
}
for(int i=;i<=sum_scc;i++)
//没有入度的scc个数++
if(!A[i].in)ans++;
if(!A[A[S].fa].in)ans--;
//特判,起点所在的scc如果没有入度那么答案-1
printf("%d\n",ans);
return ;
}
CF999E Reachability from the Capital来自首都的可达性的更多相关文章
- [CF999E]Reachability from the Capital
题目大意:有一个$n$个点$m$条边的有向图,起点$S$,要求你添加最少的边使得$S$可以到达所有点 题解:缩点,答案就是没有入边的强连通分量个数,注意,如果起点$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 所以可以暴力枚举这 ...
- Reachability from the Capital
题目描述 There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in ...
- 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 ...
- Reachability from the Capital(Codeforces Round #490 (Div. 3)+tarjan有向图缩点)
题目链接:http://codeforces.com/contest/999/problem/E 题目: 题意:给你n个城市,m条单向边,问你需要加多少条边才能使得从首都s出发能到达任意一个城市. 思 ...
- Reachability from the Capital CodeForces - 999E(强连通分量 缩点 入度为0的点)
题意: 问至少加几条边 能使点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 ...
随机推荐
- h5或者微信端吊起app
[https://www.cnblogs.com/shadajin/p/5724117.html]! 魔窗sdk http://www.magicwindow.cn/doc/universal-lin ...
- Linux 动态链接库 - dll劫持
如何使用动态链接库 Linux下打开使用动态链接库需要三步(实际上和windows下基本一样):1.加载动态链接库,通过调用库函数dlopen()获得链接库的句柄,对应于windows下的 AfxLo ...
- ProtocolBuffer for Objective-C Mac运行环境配置
上班第4天,上司让我研究Google的Protocol Buffer,对于我这个小白来说这是一大难题.结合了一下网上资料,用了几个小时的时间,终于搞明白了.做个笔记,也当做资料给大家分享一下. 什么是 ...
- Python随笔--爬虫(下载妹子图片)
- admin 自定义字段颜色 并加以简单判断
在model中class Books(models.Model): nid = models.AutoField(primary_key=True, ) title = models.CharFiel ...
- LNMP(三)
第二十二次课 LNMP(三) 目录 一.Nginx负载均衡 二.ssl原理 三.生成ssl密钥对 四.Nginx配置ssl 五.php-fpm的pool 六.php-fpm慢执行日志 七.open_b ...
- python基础 字典练习
练习1:info = [ {'wangming': { 'money':1111, 'car':['bmo','bsj'], 'info':{ 'phone':1511111, 'age':18} } ...
- dede织梦系统接入熊掌号推送api,完整详细教程
第一步: 根据熊掌号要求完成校验页面,官方文档很详细,照着弄就行了 第二步: 开始后台改造 1.进入后台文件夹dede(自己实际的文件夹),然后进入templets目录,打开body_inde ...
- Javascript学习一数据类型
1.介绍js的基本数据类型 Undefined.Null.Boolean.Number.String (1) Undefined和Undifined区别 null是一个表示"无"的 ...
- vgcreate语法
vgcreate 用于创建LVM卷组 补充说明 vgcreate命令 用于创建LVM卷组.卷组(Volume Group)将多个物理卷组织成一个整体,屏蔽了底层物理卷细节.在卷组上创建逻辑卷时不用考虑 ...