Reachability from the Capital

问题描述:

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 nnmm and ss (1n5000,0m5000,1sn1≤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 uiuivivi (1ui,vin1≤ui,vi≤nuiviui≠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.

问题分析:本道题的大意是给你一张有向图,需要使得给定的点可以到达图中任意的一个节点位置,问至少有在图中修多少条路。

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.

本道题的解题思想主要是dfs。首先我们遍历1到n个点,把到达过的位置做一个标记,并将每个点可以到达的位置保存到堆栈中。然后标记清零,bfs定点s可以到达的节点,做标记。堆栈中未被标记的位置便是需要建立通路的位置。

AC代码:

#include <iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
using namespace std;

const int N = 5005;

int m,n,s;
int a[N];
int used[N];
vector<int>G[N];
stack<int>st;
//遍历每个点所能达到的节点位置,并保存 
void dfs(int u){
	used[u] = 1;
	/*
	for(int v:G[u])
        if(!vis[v])
            dfs(v);
    st.push(u);
    */
	for(int i = 0;i < G[u].size();i++){
		int v = G[u][i];
		if(!used[v]){
			dfs(v);
		}
	}
	st.push(u);
}

void dfs1(int v){
	used[v] = 1;
	for(int i = 0;i < G[v].size();i++){
		int u = G[v][i];
		if(!used[u]){
			dfs(u);
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>m>>s;
	int a,b;
	memset(used,0,sizeof(used));
	for(int i = 1;i <= m;i++){
		cin>>a>>b;
		//从a到b有路 
		G[a].push_back(b);
	}
	for(int i= 1;i <= n;i++){
		if(!used[i]){
			//遍历每个点 
			dfs(i);
		}
	}
	memset(used,0,sizeof(used));
	dfs1(s);
	int cnt = 0;
	while(!st.empty()){
		int u = st.top();
		st.pop();
		if(!used[u]){
			dfs1(u);
			cnt++;
		} 
	}
	cout<<cnt<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值