Road Networks 强连通分量的个数 tarjan

4262 - Road Networks
Asia - Taipei - 2008/2009
PDF   Submit   Ranking

 

 

There is a road network comprised by M roads and N cities. For convenience, we use {1, 2,..., N} to denote the N cities. Each road between two cities i and j , where 1i , jN and i  j , has two types: One type is bidirectional, which allows a citizen to drive a car from i to j (denoted by ij ) and from j to i (denoted by ji ). The other type is unidirectional, which allows a citizen to drive a car following exactly one direction, either from i to j or from j to i .

We say that City j is reachable from City i if one can drive a car from i to j , visiting a sequence of cities c1, c2,..., ck for k 0 , such that ic1c2...ckj . (Every city is always reachable from itself.) A region is a maximal set of cities so that the following mutually reachable property holds: for two arbitrary cities i and j , i is reachable from j and j is also reachable from i . The adjective ``maximal" means that if we include any other city in the given region, the mutually reachable property cannot be retained. Given a road network, your task is to write a computer program to compute the number of regions in the road network.

 


Technical Specification

We use {1, 2,..., N} to denote the N cities.
M2000 is a non-negative integer
N1000 is a positive integer.
If a road between i and j is bidirectional, then we use two order pairs (i, j) and (j, i) to represent it. Otherwise, if a road between i and j is unidirectional from i to j (respectively, j to i ), we use (i , j ) (respectively, (j , i )) to represent it.

Input 
The input consists of a number of test cases. The first line of the input file contains an integer indicating the number of test cases to follow. Each test case consists of a road network, which has the following format: the first line of each test case contains two numbers, N and M , separated by a single space. The next M lines contain the description of M roads such that one line contains two cities representing an order pair (i, j) . Each line is represented by two positive numbers separated by a single space; the first number representing the former element in the order pair and the second number representing the latter element in the order pair. A `0' at the (M + 2) -th line of each test case (except for the last test case) indicates the end of this test case.

The next test case starts after the previous ending symbol `0'. Finally, a `-1' signals the end of the whole inputs.


Output 
The output contains one line for each test case. Each line contains an integer, which is the number of the regions in the given road network.


Sample Input 

2
3 2
1 2
1 3
0
3 3
1 2
2 3
3 1
-1


Sample Output 

3
1

 


#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int V=1010;
vector<int> vec[V];
int id[V], pre[V], low[V], s[V], stop, cnt, scnt;
void tarjan(int v, int n) // vertex: 0 ~ n-1
{
int t, minc = low[v] = pre[v] = cnt++;
vector<int>::iterator pv;
s[stop++] = v;
for (pv = vec[v].begin(); pv != vec[v].end(); ++pv) {
if(-1 == pre[*pv]) tarjan(*pv, n);
if(low[*pv] < minc) minc=low[*pv];
}
if(minc < low[v]) {
low[v] = minc; return;
}
do {
id[t = s[--stop]] = scnt; low[t] = n;
} while(t != v);
++scnt; // 强连通分量的个数
}
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        scnt=cnt=stop=0;//初始化
        memset(pre,-1,sizeof(pre));//初始化
        int n,m;scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++) vec[i].clear();
        while(m--)
        {
            int x,y;scanf("%d%d",&x,&y);
            vec[x-1].push_back(y-1);
        }   
        for(int i=0; i<n; ++i) if(-1==pre[i]) tarjan(i, n);
        printf("%d/n",scnt);
        int pl;scanf("%d",&pl);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值