博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2186 Popular Cows(Targin缩点)
阅读量:5086 次
发布时间:2019-06-13

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

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 31808   Accepted: 12921

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. 

思路

题意:给出一对牛之间的羡慕关系,并且当A羡慕B,B羡慕C时,可以认为C也被A羡慕。问N头牛中,有几头牛被其他所有牛羡慕。

题解:根据样例可以看出,这个图不是DAG图,但是我们可以通过targin缩点,使之成为DAG图,对于DAG图,我们知道,如果一头牛有出度,那么它就不是被其他所有牛仰慕的牛,如果其出度为0那么其有可能成为被其他所有牛仰慕的牛,但是当出度为0的牛超过1时,便不存在被除自身外其他牛仰慕的牛,因为肯定有另外一头出度为0的牛不仰慕它。

 

#include
#include
#include
using namespace std;const int maxn = 10005;int tot, top, scc_cnt, index;int head[maxn], dfn[maxn], low[maxn], outde[maxn], belong[maxn], st[maxn], inst[maxn], cnt[maxn];struct Edge{ int v, next;} edge[maxn*maxn];void init(){ tot = top = index = scc_cnt = 0; memset(head, -1, sizeof(head));memset(belong, 0, sizeof(belong)); memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low)); memset(st, 0, sizeof(st));memset(inst, 0, sizeof(inst)); memset(outde, 0, sizeof(outde)); memset(cnt, 0, sizeof(cnt));}void addedge(int u, int v){ edge[tot] = (Edge) { v, head[u] }; head[u] = tot++;}void targin(int u){ int v; dfn[u] = low[u] = ++index; st[++top] = u; inst[u] = 1; for (int i = head[u];i != -1;i = edge[i].next) { v = edge[i].v; if (!dfn[v]) { targin(v); low[u] = min(low[u],low[v]); } else if (inst[v]) low[u] = min(low[u],dfn[v]); } if (dfn[u] == low[u]) { scc_cnt++; do { v = st[top--]; inst[v] = 0; belong[v] = scc_cnt; cnt[scc_cnt]++; } while (u != v); }}int main(){ int N, M, u, v, res, sum = 0; init(); scanf("%d%d", &N, &M); for (int i = 0; i < M; i++) { scanf("%d%d", &u, &v); addedge(u, v); } for (int i = 1; i <= N; i++) if (!dfn[i]) targin(i); for (int i = 1; i <= N; i++) { for (int j = head[i]; ~j; j = edge[j].next) { int v = edge[j].v; if (belong[i] != belong[v]) { outde[belong[i]]++; } } } for (int i = 1; i <= scc_cnt; i++) { if (!outde[i]) { res = i; sum++; } } if (sum > 1) printf("0\n"); else printf("%d\n", cnt[res]);}

  

转载于:https://www.cnblogs.com/ZhaoxiCheung/p/6132184.html

你可能感兴趣的文章
《梦断代码》读书笔记(三)
查看>>
AngularJS学习篇(一)
查看>>
spring security 11种过滤器介绍
查看>>
代码实现导航栏分割线
查看>>
大数据学习系列(8)-- WordCount+Block+Split+Shuffle+Map+Reduce技术详解
查看>>
Mysql性能调优
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
getElement的几中属性介绍
查看>>
HTML列表,表格与媒体元素
查看>>
设计器 和后台代码的转换 快捷键
查看>>
STL容器之vector
查看>>
数据中心虚拟化技术
查看>>
复习文件操作
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
JavaScript 克隆数组
查看>>
eggs
查看>>