题目大意:

有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. Redis安装部署教程

    1)下载 redis-3.2.9.tar.gz 2)用ssh工具连接目录主机,在命令窗口输入:mkdir -p /opt/redis创建redis文件夹 3)通过WinSCP工具将redis-3.2. ...

  2. python-之-深浅拷贝一

    深浅拷贝 一.数据为不可变类型 (str.int.bool) import copy v1 = "abc" v2 = copy.copy(v1) v3 = copy.deepcop ...

  3. 实现简单的ssh功能

    客户端代码: # -*- coding=utf-8 -*- __Author__ = "Dennis" import socket client = socket.socket() ...

  4. Spring工作原理及应用

    spring原理 内部最核心的就是IOC了,动态注入,让一个对象的创建不用new了,可以自动的生产,这其实就是利用java里的反射,反射其实就是在运行时动态的去创建.调用对象,Spring就是在运行时 ...

  5. JS属性修改

    document.getElementById('p1').style.visibility='visible' document.getElementById('p1').style.visibil ...

  6. 洛谷P1091 合唱队形

    输入输出样例 输入样例#1: 8 186 186 150 200 160 130 197 220 输出样例#1: 4 此题意在先升后降子序列,单调递增子序列,单调递减子序列当中找到最长的一组序列. 因 ...

  7. [转载] java多线程总结(一)

    转载自:http://www.cnblogs.com/lwbqqyumidi/p/3804883.html 作者:Windstep 多线程作为Java中很重要的一个知识点,在此还是有必要总结一下的. ...

  8. vue css背景图片打包后路径问题

    limit,代表如果小于大约4k则会自动帮你压缩成base64编码的图片,否则拷贝文件到生产目录 name,后面是打包后的路径: loader,后面 limit 字段代表图片打包限制,这个限制并不是说 ...

  9. 刷新浏览器 protractor

    //refresh browser.ignoreSynchronization = true; browser.refresh(); browser.sleep(3000); browser.swit ...

  10. WEB学习笔记13-高可读性的HTML之精简HTML代码/过时的块状元素和行内元素

    <a id="more-intro">点击此处 <img src="down-arrow.png" /></a> (1)删除 ...