POJ2186Popular Cows
来源:互联网 发布:基轮轮嫂淘宝店 编辑:程序博客网 时间:2024/03/29 19:00
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 21149 Accepted: 8620
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.
#include <iostream>#include <cstdio>#include <vector>#include <algorithm>#include <cstring>#include <stack>#define DEBUG 10using namespace std;const int maxn = 10000+10;const int maxm = 50000+10;int gn, gm;vector<int> G[maxn], G2[maxn];stack<int> S;int pre[maxn], sccno[maxn], lowlink[maxn];int dfs_clock, scc_cnt;int weight[maxn];// the number of every scc.//Accepted2756K782MSG++2296B2013-12-01 22:54:30void dfs(int u) { pre[u] = lowlink[u] = ++dfs_clock; S.push(u); for(int i = 0; i < (int)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; memset(pre, 0, sizeof(pre)); memset(sccno, 0, sizeof(pre)); for(int i = 1; i <= gn; i++) { if(!pre[i]) { dfs(i); } }}void build_map() { int u, v; memset(weight, 0, sizeof(weight)); for(int i = 1; i <= gn; i++) { int v = sccno[i]; weight[v] += 1; }#ifndef DEBUG for(int i = 1; i <= scc_cnt; i++) { printf("weight[%d] = %d\n", i, weight[i]); }#endif for(int i = 1; i <= gn; i++) { for(int j = 0; j < (int)G[i].size(); j++) { u = sccno[i]; v = sccno[G[i][j]]; if(u != v) { G2[u].push_back(v); } } }}int work() { vector<int> V; V.clear(); for(int i = 1; i <= scc_cnt; i++) { if(!G2[i].size()) { V.push_back(i); } } if(V.size() > 1) return 0; else return weight[V[0]];}int main(){ // freopen("in", "r", stdin); int u, v; while(scanf("%d%d", &gn, &gm) != EOF) { for(int i = 1; i <= gm; i++) { scanf("%d%d", &u, &v); G[u].push_back(v); } find_scc(gn); build_map(); int output = work(); printf("%d\n", output); } return 0;}
- poj2186Popular Cows
- POJ2186Popular Cows
- poj2186Popular Cows
- poj2186Popular Cows tarjan缩点
- poj2186Popular Cows【scc+缩点】
- POJ2186Popular Cows(强连通分量)
- poj2186Popular Cows(强连通+缩点)
- POJ2186Popular Cows(强连通分量+缩点)
- poj2186Popular Cows【强连通】【debug两小时】
- poj2186Popular Cows【强连通】【debug两小时】
- POJ2186Popular Cows(强连通分量分解模板)
- Cows
- Cows
- poj2186Popular Cows_ 强连通分支_缩点tarjan算法
- Jumping Cows
- Milking Cows
- Cows 算法
- Milking Cows
- SSH2三大框架整合出错(三)
- 云算法调用平台--web 工程调用hadoop集群1.4
- tar.gz
- 将oracle10g安装在lvm映射的裸设备上
- SQL语句的增删改查及数据库表的多表连接查询
- POJ2186Popular Cows
- const及const在函数前与函数后的区别
- 模式匹配的问题
- Django 静态文件部署
- [struts2]2.3.14 json-plugin 存在bug<java.lang.NoSuchFieldException: DEFAULT_PARAM>
- 【白书】第一章实验B&C
- SHELL编程学习笔记
- Uva 6436 - The Busiest City 树形DP
- ${pageContext.request.contextPath} JSP取得绝对路径