POJ 2186 Popular Cows
2016-04-21 18:59
471 查看
http://poj.org/problem?id=2186Popular Cows
DescriptionEvery 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 Input3 3
1 2
2 1
2 3
Sample Output1
HintCow 3 is the only cow of high popularity.
SourceUSACO 2003 Fallscc图中出度为0的顶点超过1个 那么就不可能存在 受所有牛喜爱的牛。。故输出0;当出度为0的顶点为1个,输出顶点所代表的那个强连通分支的顶点数即可
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24347 | Accepted: 9992 |
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 Input3 3
1 2
2 1
2 3
Sample Output1
HintCow 3 is the only cow of high popularity.
SourceUSACO 2003 Fallscc图中出度为0的顶点超过1个 那么就不可能存在 受所有牛喜爱的牛。。故输出0;当出度为0的顶点为1个,输出顶点所代表的那个强连通分支的顶点数即可
#include<iostream> #include<cstring> #include<algorithm> #include<cstdlib> #include<vector> #include<cmath> #include<stdlib.h> #include<iomanip> #include<list> #include<deque> #include<map> #include <stdio.h> #include <queue> #include <stack> #define maxn 10000+5 #define ull unsigned long long #define ll long long #define reP(i,n) for(i=1;i<=n;i++) #define rep(i,n) for(i=0;i<n;i++) #define cle(a) memset(a,0,sizeof(a)) #define mod 90001 #define PI 3.141592657 #define INF 1<<30 const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; bool cmp(int a,int b) { return a>b; } vector<int>G[maxn]; int lowlink[maxn],sccno[maxn],pre[maxn],dfs_clock,scc_cnt; stack<int>s; int n,m; void dfs(int u) { pre[u]=lowlink[u]=++dfs_clock; s.push(u); for(int i=0;i<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; cle(sccno),cle(pre); for(int i=1;i<=n;i++) if(!pre[i])dfs(i); } void solve() { find_scc(n); int outdegree[maxn]; cle(outdegree); for(int i=1;i<=n;i++) for(int j=0;j<G[i].size();j++) { int k=G[i][j]; if(sccno[i]!=sccno[k]) outdegree[sccno[i]]++; } int cnt=0,val; for(int i=1;i<=scc_cnt;i++) { if(outdegree[i]==0) { val=i; cnt++; } } if(cnt>1)printf("%d\n",0); else { int ans=0; for(int i=1;i<=n;i++) { if(sccno[i]==val)ans++; } printf("%d\n",ans); } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); //出度为0的点超过1个,则输出0;不然就输出出度为0的那个点所代表的那个强连通分支的顶点数即可。 while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<maxn;i++) G[i].clear(); int a,b; for(int i=1;i<=m;i++) { scanf("%d %d",&a,&b); G[a].push_back(b); } solve(); } return 0; }
相关文章推荐
- 慎用mutableCopy
- Codeforces Round #260 (Div. 2) 第一题A. Laptops
- 老外的教学网站Javascript-OOP
- OpenCV2应用Meanshift查找相似物体
- Apache Ranger0.6版本源码编译
- linux管道(pipe)
- RecyclerView smoothScrollToPosition的滚动时间
- Elasticsearch使用笔记
- 在Linux系统 CentOS 6 下安装KVM虚拟机
- 解决apache启动错误:Could not reliably determine the server's fully qualified domain name
- 虚拟机性能监控与故障处理工具
- Linux开机启动Jenkins
- HBase总结(二十)HBase常用shell命令详细说明
- net.sf.cglib.beans.BeanCopier
- Nginx/LVS/HAProxy负载均衡软件对比详解
- 常用linux命令相关
- linux异步IO编程实例分析
- 使用kickstart脚本安装CentOS7
- nginx lua获取客户端ip
- 运维简单压力测试