博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVALive 4262——Trip Planning——————【Tarjan 求强连通分量个数】
阅读量:5094 次
发布时间:2019-06-13

本文共 5342 字,大约阅读时间需要 17 分钟。

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

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 1$ \le$i<tex2html_verbatim_mark> , j$ \le$N<tex2html_verbatim_mark> and i$ \neq$j<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 i$ \leadsto$j<tex2html_verbatim_mark> ) and from j<tex2html_verbatim_mark> to i<tex2html_verbatim_mark> (denoted by j$ \leadsto$i<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$ \ge$ 0<tex2html_verbatim_mark> , such thati$ \leadsto$c1$ \leadsto$c2$ \leadsto$...$ \leadsto$ck$ \leadsto$j<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. M$ \le$2000<tex2html_verbatim_mark> is a non-negative integer
  3. N$ \le$1000<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 21 21 30 3 31 22 33 1-1

 

Sample Output

 

3 1 题目大意:给你n个点,m条有向边。问你这个图中的scc个数。 解题思路:求强连通分量的模板题,Tarjan算法水过。
/*    Tarjan     求强连通分量个数*/#include
#include
#include
#include
#include
#include
using namespace std;const int maxn = 1e5+200;vector
G[maxn];int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;stack
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;}

  

转载于:https://www.cnblogs.com/chengsheng/p/4887059.html

你可能感兴趣的文章
Nginx的虚拟主机配置
查看>>
overflow 属性
查看>>
Java中多态的一些简单理解
查看>>
洛谷 1449——后缀表达式(线性数据结构)
查看>>
JZOJ 3.10 1539——三条直线
查看>>
[最小割][Kruskal] Luogu P5039 最小生成树
查看>>
Data truncation: Out of range value for column 'Quality' at row 1
查看>>
Dirichlet分布深入理解
查看>>
Javascript的调试利器:Firebug使用详解
查看>>
(转)Android之发送短信的两种方式
查看>>
使用vue脚手架搭建项目
查看>>
Java基础之ArrayList与LinkedList、Vector,以及HashMap与HashTable的区别
查看>>
网络爬虫初步:从一个入口链接开始不断抓取页面中的网址并入库
查看>>
iOS archive(归档)的总结 (序列化和反序列化,持久化到文件)
查看>>
python第九天课程:遇到了金角大王
查看>>
字符串处理
查看>>
ECharts(Enterprise Charts 商业产品图表库)初识
查看>>
LeetCode Factorial Trailing Zeroes (阶乘后缀零)
查看>>
hdu 5402 Travelling Salesman Problem (技巧,未写完)
查看>>
[AIR] 获取U盘,打开U盘
查看>>