题目大意:

有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来自首都的可达性的更多相关文章

  1. [CF999E]Reachability from the Capital

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

  2. E - Reachability from the Capital

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

  3. E. Reachability from the Capital dfs暴力

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

  4. Reachability from the Capital

    题目描述 There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in ...

  5. 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 ...

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

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

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

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

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

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

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

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

随机推荐

  1. Altium designer 新建快捷键

    示例: 1.按下Ctrl: 2.点击需要建立快捷键的图标:点击交互式布线图标,然后在选择性输入要用到的快捷键:

  2. VGG网络结构

    这个结构其实不难,但是它里面训练的一些东西我还没有搞清楚,打算把昨天写的代码传上来,方便日后来看,发现了一个很有意思的库叫TF-slim打算哪天看看有没有好用的东西 from datetime imp ...

  3. python3.6 连接mysql数据库问题

    最近有个项目欲安装MySQL-python/1.2.5一直失败. 环境: win7 python3.6.2 报错信息如下: Creating library build\temp.win32-3.6\ ...

  4. js 判断变量是否为空或未定义

    判断变量是否定义: if(typeof(hao) == "undefined"){ //未定义 }else{ //定义 } 判断变量是否为空或NULL,是则返回'', 反之返回原对 ...

  5. Spine用于Timeline(NullReferenceException: Object reference not set to an instance of an object pine.Unity.Editor.AnimationReferenceAssetEditor.OnInspectorGUI ())

    报错信息:Spine.Unity.Editor.AnimationReferenceAssetEditor.OnInspectorGUI () (at Assets/Extention/Spine/E ...

  6. L342 Air Pollution Is Doing More Than Just Slowly Killing Us

    Air Pollution Is Doing More Than Just Slowly Killing Us In the future, the authorities might need to ...

  7. crontab定时执行shell脚本失败的原因

    有一段时间不用crontab定时执行任务了,这次趁着项目实施的机会来进一步分析一下crontab定时任务设置时遇到的一些棘手的问题. crontab -l 查看目前已经设置的crontab信息 cro ...

  8. 正则表达式andJS内存空间详细图解

    http://www.runoob.com/js/js-regexp.html https://blog.csdn.net/pingfan592/article/details/55189622

  9. [转]How to Send Ethereum with Web3.js and Node

    原文:https://davekiss.com/ethereum-web3-node-tutorial/   Ethereum took the web and cryptocurrency worl ...

  10. django用MySQL数据库链接

    在使用的过程中出现了没有mysqld.sock这个文件的情况,无法连接到mysql数据库. 几经周折,设置路径,改文件夹的权限,也都无济于事,只有重新安装mysql服务器,第一次尝试还是失败,服务器安 ...