poj 2186 Popular Cows
来源:互联网 发布:百度贴吧一键签到软件 编辑:程序博客网 时间:2024/04/28 20:37
http://poj.org/problem?id=2186
题意:
N头奶牛,给出若干欢迎关系a,b,表示a欢迎b,欢迎关系是单向的,但也是可以传递的,另外每个奶牛都是欢迎他自己的。求出被所有奶牛欢迎的奶牛的数目。
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <stack>#define maxn 100001using namespace std;int dfn[maxn],low[maxn],belong[maxn],bcnt,bcc_clock,a[maxn],b[maxn],num[maxn],n,m,head[maxn],e;stack<int>s;bool vis[maxn],flag[maxn];struct node{ int v,next;}p[maxn];void add(int u,int v){ p[e].v=v; p[e].next=head[u]; head[u]=e++;}void tarjan(int u){ dfn[u]=low[u]=++bcc_clock; vis[u]=true; s.push(u); for(int i=head[u]; i!=-1; i=p[i].next) { int v=p[i].v; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(vis[v]&&dfn[v]<low[u]) low[u]=dfn[v]; } if(dfn[u]==low[u]) { bcnt++; int j; do { j=s.top(); s.pop(); vis[j]=false; belong[j]=bcnt; }while(j!=u); }}void deal(){ while(!s.empty()) { s.pop(); } memset(dfn,0,sizeof(dfn)); bcnt=0;bcc_clock=0; for(int i=1; i<=n; i++) { if(!dfn[i]) { tarjan(i); } }}void inti(){ e=0; memset(head,-1,sizeof(head)); memset(belong,0,sizeof(belong)); memset(low,0,sizeof(low)); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(num,0,sizeof(num)); memset(flag,false,sizeof(flag)); memset(vis,false,sizeof(vis));}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { inti(); for(int i=1; i<=m; i++) { scanf("%d%d",&a[i],&b[i]); add(a[i],b[i]); } deal(); for(int i=1; i<=m; i++) { if(belong[a[i]]!=belong[b[i]]) flag[belong[a[i]]]=true; } for(int i=1; i<=n; i++) { num[belong[i]]++; } int t=0,ans; for(int i=1; i<=bcnt; i++) { if(!flag[i]) { t++; ans=num[i]; } } if(t==1) printf("%d\n",ans); else printf("0\n"); } 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
- POJ 2186 Popular Cows
- jetty安装与配置
- poj 1947 Rebuilding Roads (树形dp)
- “互联网思维”就是精简精简再精简!
- 大话快速开发——III 回归
- 内存分配粒度vs内存分页大小vs内存空间
- poj 2186 Popular Cows
- NYOJ 103 A+B Problem II
- poj 1208 模拟而已
- ZendOptimizer.so:connot restore segment prot after reloc: Permission denied
- Java 7之多线程第10篇 - Semaphore
- 2. 普通用户修改自己的密码
- (总结)关于Linux的缓存内存 Cache Memory详解
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
- 旧版API的TextInputFormat源码分析