Reachability from the Capital(Codeforces Round #490 (Div. 3)+tarjan有向图缩点)
题目链接:http://codeforces.com/contest/999/problem/E
题目:




题意:给你n个城市,m条单向边,问你需要加多少条边才能使得从首都s出发能到达任意一个城市。
思路:tarjan缩点,结果就是缩点新建的图中入度为0的点的数量。
代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 5e3 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, m, tot, s, cnt, top, u, v, num;
int head[maxn], vis[maxn], in[maxn];
int tot1, head1[maxn], in1[maxn];
int dfn[maxn], low[maxn], c[maxn], stc[maxn]; struct edge {
int v, next;
}ed[maxn], ed1[maxn]; void addedge(int u, int v) {
ed[tot].v = v;
ed[tot].next = head[u];
head[u] = tot++;
} void addedge1(int u, int v) {
ed1[tot1].v = v;
ed1[tot1].next = head1[u];
head1[u] = tot1++;
} void tarjan(int x) {
dfn[x] = low[x] = ++num;
vis[x] = , stc[++top] = x;
for(int i = head[x]; ~i; i = ed[i].next) {
int y = ed[i].v;
if(!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
} else if(vis[y]) {
low[x] = min(low[x], low[y]);
}
}
if(dfn[x] == low[x]) {
int y; cnt++;
do {
y = stc[top--]; vis[y] = ;
c[y] = cnt;
} while(x != y);
}
} int main() {
//FIN;
scanf("%d%d%d", &n, &m, &s);
memset(head, -, sizeof(head));
memset(head1, -, sizeof(head1));
for(int i = ; i <= m; i++) {
scanf("%d%d", &u, &v);
addedge(u, v);
in[v]++;
}
for(int i = ; i <= n; i++) {
if(in[i] == && !dfn[i]) {
tarjan(i);
}
}
for(int i = ; i <= n; i++) {
if(!dfn[i]) {
tarjan(i);
}
}
int sum = ;
for(int i = ; i <= n; i++) {
for(int j = head[i]; ~j; j = ed[j].next) {
int y = ed[j].v;
if(c[i] == c[y]) continue;
addedge1(c[i], c[y]);
in1[c[y]]++;
}
}
s = c[s];
for(int i = ; i <= cnt; i++) {
if(i != s && in1[i] == ) {
sum++;
}
}
printf("%d\n", sum);
return ;
}
Reachability from the Capital(Codeforces Round #490 (Div. 3)+tarjan有向图缩点)的更多相关文章
- Codeforces Round #490 (Div. 3)
感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...
- [Codeforces]Codeforces Round #490 (Div. 3)
Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...
- Codeforces Round #490 (Div. 3) :F. Cards and Joy(组合背包)
题目连接:http://codeforces.com/contest/999/problem/F 解题心得: 题意说的很复杂,就是n个人玩游戏,每个人可以得到k张卡片,每个卡片上有一个数字,每个人有一 ...
- Codeforces Round #490 (Div. 3) F - Cards and Joy
F - Cards and Joy 思路:比较容易想到dp,直接dp感觉有点难,我们发现对于每一种数字要处理的情况都相同就是有 i 张牌 要给 j 个人分, 那么我们定义dp[ i ][ j ]表示 ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- Codeforces Round #257 (Div. 1)A~C(DIV.2-C~E)题解
今天老师(orz sansirowaltz)让我们做了很久之前的一场Codeforces Round #257 (Div. 1),这里给出A~C的题解,对应DIV2的C~E. A.Jzzhu and ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
随机推荐
- Vim新手节省时间的10多个小技巧
Vim新手节省时间的10多个小技巧 Vim 是很多开发者的首选编辑器,通过设置正确的命令和快捷方式,它可以帮你更快的完成工作.这篇文章我们为 Vim 新手提供一些快捷键等方面的小技巧,帮你提升工作效率 ...
- 【Docker 命令】- pull命令
docker pull : 从镜像仓库中拉取或者更新指定镜像 语法 docker pull [OPTIONS] NAME[:TAG|@DIGEST] OPTIONS说明: -a :拉取所有 tagge ...
- 为什么 MongoDB (索引)使用B-树而 Mysql 使用 B+树
B-树由来 定义:B-树是一类树,包括B-树.B+树.B*树等,是一棵自平衡的搜索树,它类似普通的平衡二叉树,不同的一点是B-树允许每个节点有更多的子节点.B-树是专门为外部存储器设计的,如磁盘,它对 ...
- ajax 请求 后台返回的文件流
download(url) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); // 也可以使用POST方式,根据接口 xhr. ...
- RT-thread内核之互斥量
一.互斥量控制块:在include/rtdef.h中 #ifdef RT_USING_MUTEX /** * Mutual exclusion (mutex) structure */ struct ...
- 【bzoj4736/uoj#274】[清华集训2016]温暖会指引我们前行 语文题+LCT
题目描述 http://uoj.ac/problem/274 题解 语文题+LCT 对于这种语文题建议还是自己读题好一些... 读懂题后发现:由于温度互不相同,最大生成树上的路径必须走(不走的话温度大 ...
- NetScaler VLAN’s Demystified
NetScaler VLAN’s Demystified https://www.citrix.com/blogs/2014/12/29/netscaler-vlans-demystified/ Th ...
- 洛谷3800:Power收集——题解
https://www.luogu.org/problemnew/show/P3800 可以把游戏界面理解成一个N行M列的棋盘,有K个格子上有P点,其价值为val(i,j) 初始灵梦可以选择在第一行的 ...
- cf 460 E. Congruence Equation 数学题
cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...
- BZOJ1022 [SHOI2008]小约翰的游戏John 【博弈论】
1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3014 Solved: 1914 [Submi ...