poj2186Popular Cows(强连通+缩点)
来源:互联网 发布:装修自动设计软件 编辑:程序博客网 时间:2024/04/25 23:44
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 19647 Accepted: 7960
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.
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.
* 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
USACO 2003 Fall
题目大意:给一张图,求所有点可达的点的个数。
题目分析:先求一次强连通将有环图转化成一个DAG,然后缩点,求新图中出度为0的点的个数,如果出度为0的点只有1个则这个强连通所有点都能被其他点可达,否则没有点能被所有点可达。
详情请见代码:
#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int N = 10005;const int M = 50005;struct edge{ int to,next;}g[M];int head[N];int scc[N];int cnt[N];//统计强连通的点数int stack1[N];int stack2[N];int vis[N];int out[N];int m,n;void init(){ int i; for(i = 1;i <= n;i ++) { head[i] = -1; scc[i] = cnt[i] = vis[i] = out[i] = 0; }}int nextint(){ char c; int ret; while((c = getchar()) > '9' || c < '0') ; ret = c - '0'; while((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + c - '0'; return ret;}void dfs(int cur,int &sig,int &ret){ vis[cur] = ++sig; stack1[++stack1[0]] = cur; stack2[++stack2[0]] = cur; for(int i = head[cur];i != -1;i = g[i].next) { if(vis[g[i].to] == 0) dfs(g[i].to,sig,ret); else if(scc[g[i].to] == 0) { while(vis[stack2[stack2[0]]] > vis[g[i].to]) stack2[0] --; } } if(stack2[stack2[0]] == cur) { ++ret; stack2[0] --; do { scc[stack1[stack1[0]]] = ret; cnt[ret] ++; }while(stack1[stack1[0] --] != cur); }}int Gabow(){ int i,sig,ret; stack1[0] = stack2[0] = sig = ret = 0; for(i = 1;i <= n;i ++) if(!vis[i]) dfs(i,sig,ret); return ret;}int solve(){ int ret,i,j; int num = Gabow(); if(num == 1) return n; for(i = 1;i <= n;i ++) { for(j = head[i];j != -1;j = g[j].next) { if(scc[i] != scc[g[j].to]) out[scc[i]] ++; } } int ct = 0; int ci; for(i = 1;i <= num;i ++) { if(out[i] == 0) { ct ++; ci = i; } } if(ct == 1) return cnt[ci]; else return 0;}int main(){ int i,j,a,b; while(~scanf("%d%d",&n,&m)) { init(); for(i = 1;i <= m;i ++) { a = nextint(); b = nextint(); g[i].to = b; g[i].next = head[a]; head[a] = i; } printf("%d\n",solve()); } return 0;}//1468K16MS G++//824K110MS C++
- poj2186Popular Cows(强连通+缩点)
- POJ2186Popular Cows(强连通分量+缩点)
- POJ2186Popular Cows(强连通分量)
- poj2186Popular Cows【强连通】【debug两小时】
- poj2186Popular Cows【强连通】【debug两小时】
- POJ2186Popular Cows(强连通分量分解模板)
- poj2186Popular Cows_ 强连通分支_缩点tarjan算法
- poj2186Popular Cows tarjan缩点
- poj2186Popular Cows【scc+缩点】
- 【连通图|强连通+缩点】POJ-2186 Popular Cows
- poj 2186 Popular Cows (强连通分量,缩点)
- POJ--2186[Popular Cows]强连通分支+缩点
- POJ 2186 Popular Cows 强连通分量+缩点
- PKU2186(Popular Cows)+强连通分支Tarjan算法+缩点
- POJ-2186 Popular Cows 强连通 + 缩点
- POJ2186----Popular Cows(强连通+缩点)
- POJ 2186 Popular Cows(强连通分量+缩点)
- 【强连通分量+缩点】 POJ 2186 Popular Cows
- Java中的toString()方法
- 《算法竞赛-训练指南》第一章-1.19_UVa 11549
- C++笔记
- HDU-2007
- 省赛到暑假集训小结
- poj2186Popular Cows(强连通+缩点)
- SoapUI中使用Groovy脚本操作数据库 .
- java.lang.NoClassDefFoundError: com.umeng.analytics.MobclickAgent 解决方法
- C与C++地址分配函数malloc与new
- Myeclipse中的快捷键
- Gridx Reload 重置
- 【rzxt】笔者支招:Win8系统搜索不到无线信号该怎么办?
- 16进制数逆序输出
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gaobo.e5community/com.g