POJ 2186 Popular Cows 强连通
2015-05-16 00:09
393 查看
Popular Cows
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.
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 Input
Sample Output
Hint
Cow 3 is the only cow of high popularity.
Source
USACO 2003 Fall
周日时准备讲的
题意:求有向图中能被所有点到达的点数
缩点后,出度为0且唯一的连通块包含的点数为解-w-
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25060 | Accepted: 10268 |
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.
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 Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
Cow 3 is the only cow of high popularity.
Source
USACO 2003 Fall
周日时准备讲的
题意:求有向图中能被所有点到达的点数
缩点后,出度为0且唯一的连通块包含的点数为解-w-
/** Author: ☆·aosaki(*’(OO)’*) niconiconi★ **/ //#pragma comment(linker, "/STACK:1024000000,1024000000") //#include<bits/stdc++.h> #include <iostream> #include <sstream> #include <cstdio> #include <cstring> #include <algorithm> #include <functional> #include <cmath> #include <vector> #include <queue> #include <map> #include <set> #include <list> //#include <tuple> #define ALL(v) (v).begin(),(v).end() #define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++) #define SIZE(v) ((int)(v).size()) #define mem(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define lp(k,a) for(int k=1;k<=a;k++) #define lp0(k,a) for(int k=0;k<a;k++) #define lpn(k,n,a) for(int k=n;k<=a;k++) #define lpd(k,n,a) for(int k=n;k>=a;k--) #define sc(a) scanf("%d",&a) #define sc2(a,b) scanf("%d %d",&a,&b) #define lowbit(x) (x&(-x)) #define ll long long #define pi pair<int,int> #define vi vector<int> #define PI acos(-1.0) #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define TT cout<<"*****"<<endl; #define TTT cout<<"********"<<endl; inline int gcd(int a,int b) { return a==0?b:gcd(b%a,a); } #define INF 1e9 #define eps 1e-8 #define mod 10007 #define maxn 10010 #define maxm 50010 using namespace std; int v[maxn],col[maxn],ans[maxn]; int rea[maxn],low[maxn],stack[maxn]; int out[maxn]; int n,m,tot=0,color,tm=0,index=0,top=0; int pre[maxn]; struct Side { int to,next; }e[maxm]; void add(int u,int v) { e[tot].to=v; e[tot].next=pre[u]; pre[u]=tot++; } void tarjan(int i) { v[i]=1; top++; stack[top]=i; ++tm; rea[i]=tm; low[i]=tm; for(int j=pre[i];j!=-1;j=e[j].next) { int x=e[j].to; if(v[x]==0) tarjan(x); if(v[x]<2) low[i]=min(low[i],low[x]); } if(rea[i]==low[i]) { color++; int tt=0; while(stack[top+1]!=i) { tt++; col[stack[top]]=color; v[stack[top]]=2; top--; } ans[color]=tt; } } void init() { mem(col); mem(v); mem1(pre); mem(rea); mem(low); tot=0; top=0; tm=0; index=0; color=0; mem(out); } int main() { //freopen("in.txt","r",stdin); int uu,vv; while(~sc2(n,m)) { init(); lp(i,m) { sc2(uu,vv); add(uu,vv); } lp(i,n) if(!rea[i]) tarjan(i); if(color==1) { printf("%d\n",n); continue; } lp(i,n) { for(int j=pre[i];j!=-1;j=e[j].next) { int x=e[j].to; if(col[i]!=col[x]) out[col[i]]++; } } int re=0; lp(i,color) { if(!out[i]) { if(!re) re=ans[i]; else { re=0; break; } } } printf("%d\n",re); } return 0; }
相关文章推荐
- poj 2186 Popular Cows(强连通)
- 【POJ 2186 Popular Cows】+ 强连通
- poj2186 Popular Cows --- 强连通
- POJ 2186 Popular Cows(强连通)
- POJ 2186 Popular Cows 强连通
- POJ 2186 Popular Cows(强连通)
- POJ 2186 Popular Cows(强连通)
- POJ 2186 Popular Cows(初学强连通)
- poj2186 Popular Cows(强连通)
- POJ 2186 Popular Cows
- POJ 2186 Popular Cows 笔记
- poj2186 Popular Cows(强连通缩点)
- POJ 2186 Popular Cows(Tarjan算法求强连通分量)
- POJ 2186 Popular Cows(强连通分量分解)
- poj2186——Popular Cows
- POJ2186 Popular Cows Tarjan 链接表 pascal解题报告
- POJ_2186 Popular Cows
- POJ 2186-Popular Cows:强连通分量问题
- POJ 2186 Popular Cows(强连通分量)
- poj 2186 Popular Cows