题目链接: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有向图缩点)的更多相关文章

  1. Codeforces Round #490 (Div. 3)

    感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...

  2. [Codeforces]Codeforces Round #490 (Div. 3)

    Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...

  3. Codeforces Round #490 (Div. 3) :F. Cards and Joy(组合背包)

    题目连接:http://codeforces.com/contest/999/problem/F 解题心得: 题意说的很复杂,就是n个人玩游戏,每个人可以得到k张卡片,每个卡片上有一个数字,每个人有一 ...

  4. Codeforces Round #490 (Div. 3) F - Cards and Joy

    F - Cards and Joy 思路:比较容易想到dp,直接dp感觉有点难,我们发现对于每一种数字要处理的情况都相同就是有 i 张牌 要给 j 个人分, 那么我们定义dp[ i ][ j ]表示 ...

  5. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

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

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

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. C# Winform Excel的导出,根据excel模板导出数据

    namespace dxhbskymDemo { public partial class ExcelForm : DevExpress.XtraEditors.XtraForm { public E ...

  2. Thinkphp5图片、音频和视频文件上传

    首先是同步上传,最为基础的上传的方式,点击表单提交之后跳转那种.如下前端代码 <!DOCTYPE html> <html lang="en"> <he ...

  3. 转 PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)

    PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)   通过curl_setopt()函数可以方便快捷的抓取网页(采集很方便),curl_setopt 是php的一个 ...

  4. 不同品牌交换机设置telnet方法

    H3C交换机:1.设置telnet system-view super password level 3 cipher ******telnet server enable user-interfac ...

  5. 第一篇:python基础_1

    本篇内容 Python介绍 安装 第一个程序(hello,world) 变量 用户输入(input) 数据类型 数据运算 if判断 break和continue的区别 while 循环 一. Pyth ...

  6. poj3207 Ikki's Story IV - Panda's Trick 2-sat问题

    ---题面--- 题意:给定一个圈,m条边(给定),边可以通过外面连,也可以通过里面连,问连完这m条边后,是否可以做到边两两不相交 题解: 将连里面和连外面分别当做一种决策(即每条边都是决策点), 如 ...

  7. Kippo蜜罐的部署、诱捕节点的搭建以及自动告警

    Kippo是一个中等交互的SSH蜜罐,提供了一个可供攻击者操作的shell,攻击者可以通过SSH登录蜜罐,并做一些常见的命令操作. 当攻击者拿下一台服务器的权限后,很可能会进行小范围的端口探测或者批量 ...

  8. Java的各种中文乱码解决方法

    一.Servlet输出乱码 1. 用servlet.getOutStream字节流输出中文,假设要输出的是String str ="钓鱼岛是中国的,无耻才是日本的". 1.1 若是 ...

  9. 【单调队列】【P1714】 切蛋糕

    传送门 Description 今天是小Z的生日,同学们为他带来了一块蛋糕.这块蛋糕是一个长方体,被用不同色彩分成了N个相同的小块,每小块都有对应的幸运值. 小Z作为寿星,自然希望吃到的第一块蛋糕的幸 ...

  10. mysql主从同步加读写分离

    首先主从同步,一旦建立,指定了用户,就不能更改了,否则会有错误.1063 Error 'Duplicate entry '%-test-' for key 'PRIMARY'' on query. D ...