poj 2186 Popular Cows 【强连通分量】
来源:互联网 发布:java中prototype 编辑:程序博客网 时间:2024/06/04 19:22
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 32368 Accepted: 13202
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图,并知道了最后一个分量可能为拓扑终点,dfs反边搜索看是否达到所有边。
代码:
#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;int n,m,cmp[10100];vector<int> V[10100],FV[10100],vs;bool used[10100];void add_edge(int a,int b){ V[a].push_back(b); FV[b].push_back(a);}void dfs(int v){ used[v]=true; for (int i=0;i<V[v].size();i++) { if (!used[V[v][i]]) dfs(V[v][i]); } vs.push_back(v);}void rdfs(int v,int k){ used[v]=true; cmp[v]=k; for (int i=0;i<FV[v].size();i++) { if (!used[FV[v][i]]) rdfs(FV[v][i],k); }}int main(){ scanf("%d%d",&n,&m); // while (~scanf("%d%d",&n,&m)) // { int a,b; // for (int i=1;i<=n;i++) // { // V[i].clear(); // FV[i].clear(); // } vs.clear(); for (int i=0;i<m;i++) { scanf("%d%d",&a,&b); add_edge(a,b); } memset(used,0,sizeof(used)); for (int i=1;i<=n;i++) { if(!used[i]) dfs(i); } memset(used,0,sizeof(used)); int k=1; for (int i=vs.size()-1;i>=0;i--) { if(!used[vs[i]]) rdfs(vs[i],k++); } int ans=0; for (int i=1;i<=n;i++) { if (cmp[i]==k-1) { m=i; ans++; } } memset(used,0,sizeof(used)); rdfs(m,0); for (int i=1;i<=n;i++) { if (!used[i]) { ans=0; break; } } printf("%d\n",ans); //} return 0;}
0 0
- POJ 2186-Popular Cows ---强连通分量
- POJ 2186 Popular Cows / 强连通分量
- POJ 2186 Popular Cows 强连通分量
- POJ 2186 Popular Cows (强连通分量)
- POJ 2186 Popular Cows (强连通分量)
- POJ 2186 Popular Cows (强连通分量)
- POJ 2186 Popular Cows 强连通分量
- POJ 2186 Popular Cows 强连通分量
- poj 2186 Popular Cows 强连通分量
- POJ 2186 Popular Cows(强连通分量)
- poj 2186 Popular Cows 【强连通分量】
- |poj 2186|强连通分量|Popular Cows
- poj 2186 Popular Cows 强连通分量
- POJ 2186 Popular Cows (强连通分量)
- 【POJ】2186.Popular Cows--强连通分量
- poj 2186 Popular Cows (强连通分量,缩点)
- poj 2186 Popular Cows (强连通分量 tarjan)
- POJ 2186 Popular Cows (强连通分量)
- Day 18 Session核心方法(持久化对象的状态)
- eclipse+环境变量+SDK+虚拟机配置
- MySQL日志系统
- jxl读取excel中Date类型
- 清除Tomcat项目
- poj 2186 Popular Cows 【强连通分量】
- thinkphp5内置分页功能第二页没有拿到参数,如何解决?
- java--集合的一些用法
- Java, 构造函数介绍
- JSP开发web应用程序
- OOP面向对象三大特点
- java 设计与模式
- 说明符
- 【已解决】Ipad 1代-随意下载应用软件(此方法,同样适用于IOS 5.1.1的Iphone手机)