Road Networks

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

 

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

We say that City j<tex2html_verbatim_mark> is reachable from City i<tex2html_verbatim_mark> if one can drive a car from i<tex2html_verbatim_mark> to j<tex2html_verbatim_mark> , visiting a sequence of cities c1c2,..., ck<tex2html_verbatim_mark> for k 0<tex2html_verbatim_mark> , such thatic1c2...ckj<tex2html_verbatim_mark> . (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<tex2html_verbatim_mark> and j<tex2html_verbatim_mark> , i<tex2html_verbatim_mark> is reachable from j<tex2html_verbatim_mark> and j<tex2html_verbatim_mark> is also reachable from i<tex2html_verbatim_mark> . 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

  1. We use {1, 2,..., N}<tex2html_verbatim_mark> to denote the N<tex2html_verbatim_mark> cities.
  2. M2000<tex2html_verbatim_mark> is a non-negative integer
  3. N1000<tex2html_verbatim_mark> is a positive integer.
  4. If a road between i<tex2html_verbatim_mark> and j<tex2html_verbatim_mark> is bidirectional, then we use two order pairs (ij)<tex2html_verbatim_mark> and (ji)<tex2html_verbatim_mark> to represent it. Otherwise, if a road between i<tex2html_verbatim_mark>and j<tex2html_verbatim_mark> is unidirectional from i<tex2html_verbatim_mark> to j<tex2html_verbatim_mark> (respectively, j<tex2html_verbatim_mark> to i<tex2html_verbatim_mark> ), we use ( i<tex2html_verbatim_mark> , j<tex2html_verbatim_mark> ) (respectively, ( j<tex2html_verbatim_mark> , i<tex2html_verbatim_mark> )) 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<tex2html_verbatim_mark>and M<tex2html_verbatim_mark> , separated by a single space. The next M<tex2html_verbatim_mark> lines contain the description of M<tex2html_verbatim_mark> roads such that one line contains two cities representing an order pair (ij)<tex2html_verbatim_mark> . 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)<tex2html_verbatim_mark> -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 题目大意:给你n个点,m条有向边。问你这个图中的scc个数。 解题思路:求强连通分量的模板题,Tarjan算法水过。
/*
Tarjan
求强连通分量个数
*/
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 1e5+200;
vector<int>G[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
stack<int>S;
void dfs(int u){
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for(int i = 0;i < G[u].size(); i++){
int v = G[u][i];
if(!pre[v]){
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}else if(!sccno[v]){
lowlink[u] = min(lowlink[u], pre[v]);
}
}
if(lowlink[u] == pre[u]){
scc_cnt++;
for(;;){
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
}
void find_scc(int n){
dfs_clock = scc_cnt = 0;
while(!S.empty()) S.pop();
memset(sccno , 0, sizeof(sccno));
memset(pre, 0, sizeof(pre));
for(int i = 1; i <= n; i++){
if(!pre[i]) dfs(i);
}
}
int main(){
int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int a,b;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
G[a].push_back(b);
}
scanf("%d",&a);
find_scc(n);
printf("%d\n",scc_cnt);
for(int i = 0; i <= n; i++){
G[i].clear();
}
}
return 0;
}

  

UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】的更多相关文章

  1. UESTC 901 方老师抢银行 --Tarjan求强连通分量

    思路:如果出现了一个强连通分量,那么走到这个点时一定会在强连通分量里的点全部走一遍,这样才能更大.所以我们首先用Tarjan跑一遍求出所有强连通分量,然后将强连通分量缩成点(用到栈)然后就变成了一个D ...

  2. tarjan求强连通分量+缩点+割点以及一些证明

    “tarjan陪伴强联通分量 生成树完成后思路才闪光 欧拉跑过的七桥古塘 让你 心驰神往”----<膜你抄>   自从听完这首歌,我就对tarjan开始心驰神往了,不过由于之前水平不足,一 ...

  3. Tarjan求强连通分量,缩点,割点

    Tarjan算法是由美国著名计算机专家发明的,其主要特点就是可以求强连通分量和缩点·割点. 而强联通分量便是在一个图中如果有一个子图,且这个子图中所有的点都可以相互到达,这个子图便是一个强连通分量,并 ...

  4. tarjan求强连通分量+缩点+割点/割桥(点双/边双)以及一些证明

    “tarjan陪伴强联通分量 生成树完成后思路才闪光 欧拉跑过的七桥古塘 让你 心驰神往”----<膜你抄>   自从听完这首歌,我就对tarjan开始心驰神往了,不过由于之前水平不足,一 ...

  5. HDU 1827 Summer Holiday(tarjan求强连通分量+缩点构成新图+统计入度+一点贪心思)经典缩点入门题

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. CCF 高速公路 tarjan求强连通分量

    问题描述 某国有n个城市,为了使得城市间的交通更便利,该国国王打算在城市之间修一些高速公路,由于经费限制,国王打算第一阶段先在部分城市之间修一些单向的高速公路. 现在,大臣们帮国王拟了一个修高速公路的 ...

  7. tarjan求强连通分量(模板)

    https://www.luogu.org/problem/P2341 #include<cstdio> #include<cstring> #include<algor ...

  8. Tarjan求强连通分量、求桥和割点模板

    Tarjan 求强连通分量模板.参考博客 #include<stdio.h> #include<stack> #include<algorithm> using n ...

  9. poj 2186 tarjan求强连通分量

    蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算 ...

随机推荐

  1. 写一个c程序辨别系统是大端or小端字节序

    字节序有两种表示方法:大端字节序(big ending),小端字节序(little  ending) 看一个unsigned short 数据,它占2个字节,给它赋值0x1234.若采用的大端字节序, ...

  2. JDBC编程之数据准备

    --------------------siwuxie095 JDBC 编程之数据准备 启动 MySQL 服务,在管理员模式下的 CMD 窗口中输入 net start mysqldb 「对应的关闭 ...

  3. macOS 安装 Docker

    系统要求 Docker for Mac 要求系统最低为 macOS 10.10.3 Yosemite,或者 2010 年以后的 Mac 机型,准确说是带 Intel MMU 虚拟化的,最低 4GB 内 ...

  4. 从PCD文件中读取点云数据

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=84 在本小节我们学习如何从PCD文件中读取点云数据. 代码 章例1文件夹中, ...

  5. 6、perl创建模块(Exporter)及路径 引用 嵌套 查询模块

    参考博客:http://www.cnblogs.com/xudongliang/tag/perl/ 1.perl 模块的创建以及制定perl 模块的路径 (1)创建一个Myfun.pm模块. #/us ...

  6. (转载)Windows无法安装到GPT分区形式磁盘解决办法

    之前使用的是windows7 + ubuntu18.04双系统,硬盘分区采用的是GPT格式.重装windows系统时,提示“windows无法安装到这个磁盘.选中的磁盘采用GPT分区形式”,导致安装失 ...

  7. Java基础之cmd入门操作笔记

    前提:jdk已安装且环境变量配置成功,参考上文jdk 安装及环境变量配置 入门操作步骤: 1.打开记事本或者notepad,编写Abc代码,具体如下: public class Abc{    pub ...

  8. Cygwin install apt-cyg

    1. UPDATE CYGWIN First of all you will need to ensure that Cygwin has the necessary binaries require ...

  9. 2016 年排名 Top 100 的 Java 类库

    我们分析了GitHub中47,251个依赖,从中找出了排名前一百的Java类库,让我们看看谁在前面,谁在后面. 我们在漫长的周末的消遣方式就是浏览GitHub并且搜索流行的Java类库.我们决定把其中 ...

  10. vue中v-if 与v-show的区别

    v-if vs v-show v-if 是“真正的”条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建. v-if 也是惰性的:如果在初始渲染时条件为假,则什么也不做—— ...