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 ...
随机推荐
- redis应用-分布式锁
一个操作要修改用户的状态,修改状态需要先读出用户的状态,在内存里进行修改,改完了再存回去.如果这样的操作同时进行了,就会出现并发问题,因为读取和保存状态这两个操作不是原子的. set lock:cod ...
- spring不走注解的原因
在配置文件中没有扫描配置注解的类,导致自定义的注解无法被识别,所以不会走,检查配置文件,修改即可
- 第2次作业 -- 熟悉 JUnit 测试
2.1 Mooctest 使用心得 Mooctest很方便,可以即时测评自己写的测试代码,获得覆盖率和报告,不需要自己安装配置环境 而且安装配置插件的环境也很简单,可以专注于测试本身 2.2 Juni ...
- scrapy_redis项目配置
一.创建普通scrapy项目 二.spiders爬虫文件中修改项 import scrapy from XX.items import XXItem import json # ----1 导入类 f ...
- WebApi--------找到了与该请求匹配的多个操作问题解决
错误信息: {"Message": "出现错误.","ExceptionMessage": "找到了与该请求匹配的多个操作: \r ...
- 【SoftwareTesting】Homework1
The errors I will mention after are from the project in the last semester. The project is a Java pro ...
- “学习CSS布局” 笔记
学习网址:http://zh.learnlayout.com/no-layout.html 本文仅为学习笔记,内容非原创. position 默认值:static 没有添加额外属性的relative和 ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)
题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...
- 分布式session个人理解浅谈
在分布式中,用户的session如何处理呢? 服务器中的原生session是无法满足需求的,因为用户的请求有可能随机落入到不同的服务器中,这样的结果将会导致用户的session丢失,传统做法中有解决方 ...
- hdu1011(树形背包)(提供一个特殊样例)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 Starship Troopers Time Limit: 10000/5000 MS (Jav ...