E. Reachability from the Capital
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn cities and mm roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.

What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?

New roads will also be one-way.

Input

The first line of input consists of three integers nn, mm and ss (1≤n≤5000,0≤m≤5000,1≤s≤n1≤n≤5000,0≤m≤5000,1≤s≤n) — the number of cities, the number of roads and the index of the capital. Cities are indexed from 11 to nn.

The following mm lines contain roads: road ii is given as a pair of cities uiui, vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi). For each pair of cities (u,v)(u,v), there can be at most one road from uu to vv. Roads in opposite directions between a pair of cities are allowed (i.e. from uu to vv and from vv to uu).

Output

Print one integer — the minimum number of extra roads needed to make all the cities reachable from city ss. If all the cities are already reachable from ss, print 0.

Examples
input
Copy
9 9 1
1 2
1 3
2 3
1 5
5 6
6 1
1 8
9 8
7 1
output
Copy
3
input
Copy
5 4 5
1 2
2 3
3 4
4 1
output
Copy
1
Note

The first example is illustrated by the following:

For example, you can add roads (6,46,4), (7,97,9), (1,71,7) to make all the cities reachable from s=1s=1.

The second example is illustrated by the following:

In this example, you can add any one of the roads (5,15,1), (5,25,2), (5,35,3), (5,45,4) to make all the cities reachable from s=5s=5.

题意:就是给你很多条路,这些路,城市之间的是无向的,而城市与首都之间的只能由首都到达城市不能由城市到首都。

题解:用到了拓扑图,我们可以把不能通到首都的城市加一条需边使其能到达城市,然后遍历每一条虚边,如果新加的虚边能够使前面加的虚边联通的城市联通,则取消原来的标记。最后标记的数量即为答案;

AC代码为:

#include<bits/stdc++.h>
using namespace std;
const int maxn=5010;
int n,m,k,u,v,tot,cnt;
int first[maxn],vis[maxn],judge[maxn],connect[maxn];

struct Node{
    int to,net;
} node[maxn<<1];

void Init()
{
    tot=1,cnt=0;
    memset(first,-1,sizeof first);
}

void add(int u,int v)
{
    node[tot].to=v;
    node[tot].net=first[u];
    first[u]=tot++; 
}

void dfs(int st)
{
    vis[st]=connect[st]=1;
    for(int e=first[st];e!=-1;e=node[e].net)
    {
        int v=node[e].to;
        if(!vis[v])
        {
            judge[v]=0;
            dfs(v);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m>>k;
    Init();
    for(int i=0;i<m;i++) 
    {
        cin>>u>>v;
        add(u,v);
    }
    dfs(k);
    memset(vis,0,sizeof vis);
    for(int i=1;i<=n;i++)
    {
        if(!connect[i])
        {
            judge[i]=1;
            dfs(i);
            memset(vis,0,sizeof vis);
        }
    }
    for(int i=1;i<=n;i++) if(judge[i]) cnt++;
    cout<<cnt<<endl;
    return 0;
}

CoderForces999E-Reachability from the Capital的更多相关文章

  1. E - Reachability from the Capital

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

  2. E. Reachability from the Capital dfs暴力

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

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

  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. CF999E Reachability from the Capital来自首都的可达性

    题目大意: 有n个节点m条边,边都是单向的,请你添加最少的边使得起点s到其他与其他每一个点之间都能互相到达 这题一看就是一个缩点啊 其实对于原有的m条边相连的一些点,如果之前他们已经形成了强连通分量( ...

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

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

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

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

  8. [CF999E]Reachability from the Capital

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

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

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

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

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

随机推荐

  1. java多线程与线程并发三:线程同步通信

    本文章内容整理自:张孝祥_Java多线程与并发库高级应用视频教程. 有些时候,线程间需要传递消息,比如下面这道面试题: 子线程循环10次,然后主线程循环100次,然后又回到子线程循环50次,然后再回到 ...

  2. C# III: 数据库基本操作

    用C#操作数据库——数据库使用SQL Server为例,对应的namespace是System.Data.SqlClient. 读取数据 从数据库中读取数据是最基本的操作了. 示例代码如下: Stri ...

  3. VMware Workstation Pro(15.5)下安装Windows_Server_2008_R2

    一.新建虚拟机 1.打开VMware Workstation Pro 15.5虚拟机,点击新建虚拟机 2.选择典型(推荐),单击下一步 3.选最后一个 稍后安装操作系统,点击下一步 4.进来页面,选择 ...

  4. hdu 1166 敌兵布阵 (线段树、单点更新)

    敌兵布阵Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  5. suseoj The wheat of the prime minister

    1202: 2018四川理工学院大学生ACM程序设计:The wheat of the prime minister 时间限制: 1 Sec  内存限制: 128 MB提交: 4  解决: 3[提交] ...

  6. AsyncDisplayKit编译和使用注意事项

    Facebook开源框架,在github上可下载到.首先要编译AsyncDisplayKit库项目,有可能会出现下面错误: cocoaPods是基于ruby的项目版本控制软件,如果是ruby新手就会不 ...

  7. 关于手机微信端ios的input不能选中问题解决方案

    最近在做一个微信端的商城,以前做web端的比较多,手机端做的相对来说要少点,老板说让我用俗称”靠谱的移动前端框架”—-AUI来搭建项目. 当时觉得用不用框架无所谓啦.结果后来写到一半把项目发布到手机上 ...

  8. 投票通过,PHP 8 确认引入 Union Types 2.0

    关于是否要在 PHP 8 中引入 Union Types 的投票已于近日结束,投票结果显示有 61 名 PHP 开发组成员投了赞成票,5 名投了反对票. 还留意到鸟哥在投票中投了反对票~) 因此根据投 ...

  9. UDP 协议的那点事儿

    最近在回顾计算机网络的知识,以前上课没有认真学,只记得几个高大上的术语,所以趁着这次回顾,把学到的知识用博客的形式记录下来,一来加深自己的印象,二来希望让你对这些基础知识有一个更深入的了解.当然,我会 ...

  10. odoo12 修行基础篇之 添加字段 (一)

    本人刚刚接触odoo12,大概有2个多月的时间,这两天有点时间,就集中写下博客. 本来是打算整理成笔记,想到这段时间的开发经历,着实感觉网上有关odoo的资料太少,学习资料也不多,既然与odoo有缘, ...