博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2186(强连通分量分解)
阅读量:5159 次
发布时间:2019-06-13

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

Popular Cows

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 35035   Accepted: 14278

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M 
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 31 22 12 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source

 
题意:求从其他所有顶点都可以到达的顶点数目。
思路:所求顶点数目即为拓扑序最后的强连通分量中的顶点数目,检查其他点是否都可以到达该强连通分量。
1 //2017-08-20  2 #include 
3 #include
4 #include
5 #include
6 #include
7 8 using namespace std; 9 10 const int N = 10010; 11 vector
G[N];//邻接表存图 12 vector
rG[N];//存反向图 13 vector
vs;//后序遍历顺序的顶点列表 14 bool vis[N]; 15 int cmp[N];//所属强连通分量的拓扑序 16 17 void add_edge(int u, int v){ 18 G[u].push_back(v); 19 rG[v].push_back(u); 20 } 21 22 //input: u 顶点 23 //output: vs 后序遍历顺序的顶点列表 24 void dfs(int u){ 25 vis[u] = true; 26 for(int i = 0; i < G[u].size(); i++){ 27 int v = G[u][i]; 28 if(!vis[v]) 29 dfs(v); 30 } 31 vs.push_back(u); 32 } 33 34 //input: u 顶点编号; k 拓扑序号 35 //output: cmp[] 强连通分量拓扑序 36 void rdfs(int u, int k){ 37 vis[u] = true; 38 cmp[u] = k; 39 for(int i = 0; i < rG[u].size(); i++){ 40 int v = rG[u][i]; 41 if(!vis[v]) 42 rdfs(v, k); 43 } 44 } 45 46 //Strongly Connected Component 强连通分量 47 //input: n 顶点个数 48 //output: k 强连通分量数; 49 int scc(int n){ 50 memset(vis, 0, sizeof(vis)); 51 vs.clear(); 52 for(int u = 0; u < n; u++) 53 if(!vis[u]) 54 dfs(u); 55 int k = 0; 56 memset(vis, 0, sizeof(vis)); 57 for(int i = vs.size()-1; i >= 0; i--) 58 if(!vis[vs[i]]) 59 rdfs(vs[i], k++); 60 return k; 61 } 62 63 void solve(int n){ 64 int k = scc(n); 65 int u = 0, ans = 0; 66 for(int v = 0; v < n; v++){ 67 if(cmp[v] == k-1){ 68 u = v; 69 ans++; 70 } 71 } 72 memset(vis, 0, sizeof(vis)); 73 rdfs(u, 0); 74 for(int i = 0; i < n; i++){ 75 if(!vis[i]){ 76 ans = 0; 77 break; 78 } 79 } 80 printf("%d\n", ans); 81 } 82 83 int main() 84 { 85 int n, m; 86 while(scanf("%d%d", &n, &m)!=EOF){ 87 int u, v; 88 for(int i = 0; i < n; i++){ 89 G[i].clear(); 90 rG[i].clear(); 91 } 92 while(m--){ 93 scanf("%d%d", &u, &v); 94 u--; v--; 95 add_edge(u, v); 96 } 97 solve(n); 98 } 99 100 return 0;101 }

 

转载于:https://www.cnblogs.com/Penn000/p/7399829.html

你可能感兴趣的文章
微信公众平台开发实战Java版之如何网页授权获取用户基本信息
查看>>
一周TDD小结
查看>>
(三)建筑物多边形化简系列——去除冗余点
查看>>
Spring Boot Oauth2缓存UserDetails到Ehcache
查看>>
sizeof与strlen的用法
查看>>
2017 ICPCECPC 邀请赛 F,D,E, I 题解
查看>>
Linux 下常见目录及其功能
查看>>
python Termux Android 开发介绍
查看>>
开源框架中常用的php函数
查看>>
Java语法糖初探(三)--变长参数
查看>>
Liunx常用命令(Mile)
查看>>
nginx 的提升多个小文件访问的性能模块
查看>>
set&map
查看>>
集合类总结
查看>>
spring boot开发REST接口
查看>>
4.AE中的缩放,书签
查看>>
CVE-2014-6321 && MS14-066 Microsoft Schannel Remote Code Execution Vulnerability Analysis
查看>>
给一次重新选择的机会_您还会选择程序员吗?
查看>>
Mysql MHA高可用集群架构
查看>>
心急的C小加
查看>>