poj2186 Popular Cows 题解——S.B.S.

Popular Cows

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 29642   Accepted: 11996

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

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

——————————我是分割线————————————————————————————————

图的强联通分量。

题目大意:

有n只牛,牛A认为牛B很牛,牛B认为牛C很牛。

给你M个关系(谁认为谁牛),求大家都认为它很牛的牛有几只。

p.s.如果牛A认为牛B很牛,牛B认为牛C很牛。那么我们就认为牛A认为牛C很牛。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<queue>
  7 #include<cstdlib>
  8 #include<iomanip>
  9 #include<cassert>
 10 #include<climits>
 11 #define MAXN 10001
 12 #define MAXM 50001
 13 #define inf 0x7fffffff
 14 #define F(i,j,k) for(int i=j;i<=k;i++)
 15 #define M(a,b) memset(a,b,sizeof(a))
 16 using namespace std;
 17 inline int read(){
 18     int x=0,f=1;char ch=getchar();
 19     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
 20     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
 21     return x*f;
 22 }
 23 struct node
 24 {
 25     int to,next;
 26 }edge[MAXM];
 27 int n,m,head[MAXN],dfn[MAXN],low[MAXN],stack1[MAXN],num[MAXN],du[MAXN],vis[MAXN],cnt,time=1,top,cut;
 28 int tim,tp,dcnt,sum;
 29 void addedge(int u,int v)
 30 {
 31     edge[cnt].to=v;
 32     edge[cnt].next=head[u];
 33     head[u]=cnt;
 34     cnt++;
 35 }
 36 void dfs(int u,int fa)
 37 {
 38     dfn[u]=time;
 39     low[u]=time;
 40     time++;
 41     vis[u]=1;
 42     stack1[top]=u;
 43     top++;
 44     for(int i=head[u]; i!=-1; i=edge[i].next)
 45     {
 46         int v=edge[i].to;
 47         if(!vis[v])
 48         {
 49             dfs(v,u);
 50             low[u]=min(low[u],low[v]);
 51         }
 52         else if(vis[v])
 53         {
 54             low[u]=min(low[u],dfn[v]);
 55         }
 56     }
 57     if(low[u]==dfn[u])
 58     {
 59         cut++;
 60         while(top>0&&stack1[top]!=u)
 61         {
 62             top--;
 63             vis[stack1[top]]=2;
 64             num[stack1[top]]=cut;
 65         }
 66     }
 67 }
 68 int main()
 69 {
 70     std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
 71 //  freopen("data.in","r",stdin);
 72 //  freopen("data.out","w",stdout);
 73 //    int i,u,v;
 74     cin>>n>>m;
 75     M(head,-1);
 76     F(i,0,m-1)
 77     {
 78         int a,b;
 79         cin>>a>>b;
 80         addedge(a,b);
 81     }
 82     F(i,1,n)
 83     {
 84         if(!vis[i]) dfs(i,0);
 85     }
 86     F(i,1,n)for(int j=head[i];j!=-1;j=edge[j].next)
 87     {
 88         if(num[i]!=num[edge[j].to])
 89         {
 90             du[num[i]]++;
 91         }
 92     }
 93     int x;sum=0;
 94     F(i,1,cut)
 95     {
 96         if(!du[i])
 97         {
 98             sum++;
 99             x=i;
100         }
101     }
102     if(sum==1)
103     {
104         sum=0;
105         F(i,1,n)
106         {
107             if(num[i]==x)
108             {
109                 sum++;
110             }
111         }
112         cout<<sum<<endl;
113     }
114     else cout<<"0"<<endl;
115     return 0;
116 }

时间: 2024-10-30 17:04:08

poj2186 Popular Cows 题解——S.B.S.的相关文章

POJ2186 Popular Cows 【强连通分量Kosaraju】

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 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 &l

poj2186 Popular Cows --- 强连通

给一个有向图,问有多少结点是其他所有结点都可以到达的. 等价于,在一个有向无环图上,找出度为0 的结点,如果出度为0的结点只有一个,那么这个就是答案,如果大于1个,则答案是0. 这题有环,所以先缩点.求唯一出度为0的强连通分量. #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<iostream> #define inf 0x3f3f3f3f

强连通分量tarjan缩点——POJ2186 Popular Cows

这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定意味着v可达u.    相互可达则属于同一个强连通分量    (Strongly Connected Component, SCC) §有向图和它的转置的强连通分量相同 §所有SCC构成一个DAG(有向无环图) dfn[u]为节点u搜索的次序编号(时间戳),即首次访问u的时间 low[u]为u或u的

POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 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 &l

POJ2186 Popular Cows【Kosaraju】【强连通分量】

Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24266Accepted: 9954 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 <= 5

POJ2186 Popular Cows [tarjan 缩点]

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 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 &

POJ2186 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 popula

POJ2186 Popular Cows ,有向图, Tarjan算法

题意: 给定一个有向图,求有多少个顶点是由任何顶点出发都可达的. 顶点数<= 10,000,边数 <= 50,000 定理: 有向无环图中唯一出度为0的点,一定可以由任何点出发均可达 (由于无环,所以从任何点出发往前走,必然终止于一个出度为0的点) 1. 求出所有强连通分量 2. 每个强连通分量缩成一点,则形成一个有向无环图DAG. 3. DAG上面如果有唯一的出度为0的点,则该点能被所有的点可达.那么该点所代表的连通分量上的所有的原图中的点,都能被原图中的所有点可达,则该连通分量的点数,就是

[POJ2186]Popular Cows(强连通分量)

题目链接:http://poj.org/problem?id=2186 给定n个点m条边,求某点使得其他点都有通向它的一条路径,计算这个点集的大小. 强连通分解后求出度为0的连通分量的个数,如果有且仅有一个连通分量出度为1,则统计这个连通分量中点的数目. 遍历所有点的出边指向的点,判断这两个点是否属于同一个连通分量,记录每个连通分量中的点的数目. 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip&