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 ...
随机推荐
- JAVAEE第七周
JSON 一.什么是JSON -JSON指的是JavaScript对象表示法(Javascript Object Notation) -JSON是轻量级的文本数据交换格式 -JSON独立于语言 -JS ...
- Altium Designer 使用中的小技巧1
在布线的过程中所学到的一点技巧:在没有画原理图的情况下,直接绘制PCB板,需要敷铜Ppolygon pour,但没有网络标号,就无法连上要连的网络,焊盘,怎么办呢?需要事先将需要连接在一起的元器件(焊 ...
- springboot快速开发(简单web)
这是一个springboot基础配置文件介绍的demo.只涉及到 控制层与前端的简单交互,用于验证代码的畅通. spring-boot pom.xml解释 <?xml version=&quo ...
- JAVA 平时作业二
编写一个 Java 应用程序,统计数组{1,3,4,7,2,1,1,5,2,5,7,2,1,1,3},统 计显示每种数字其出现的次数以及出现最多和最少次数的数字 public class Number ...
- 什么是 Message Queue
Message Queue 是一种非同步的从一个服务到另一个服务的交流形式, 被用于无服务器架构和微服务架构中. Messages 被储存在一个队列中直到被处理了或被删除. 每个Messages只会被 ...
- C++中的tolower()函数与toupper()函数
https://blog.csdn.net/weixin_41053564/article/details/81349353 在C++语言中tolower()函数是把字符串都转化为小写字母 toupp ...
- 十四、使用framebuffer填充纯色
简单描述一下framebuffer的使用,它其实就相当于将屏幕上的像素映射到内存中,改变内存中的内容后屏幕自动就变颜色了. 首先要调用open("/dev/fb0", O_RDWR ...
- cf A Simple Task---线段树
Examples Input 10 5abacdabcda7 10 05 8 11 4 03 6 07 10 1 Output cbcaaaabdd Input 10 1agjucbvdfk1 10 ...
- IOS 极光推送自定义通知遇到的一些坑
主要方法: //自定义推送 - (void)networkDidReceiveMessage:(NSNotification *)notification { NSDictionary * userI ...
- windows 10安装docker一直挂起在Installing Components and Removing Files
碰到这个问题百度了好久都没有找到解决方式,什么用管理员方式运行,给文件夹权限啊,都不好使. 后面在bing上面搜docker install compoents关键字找到一条结果,如下如 点进链接,内 ...