POJ 2186 Popular Cows

                    Popular Cows

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32017 Accepted: 13039

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
题目大意:有很多奶牛,很多仰慕关系,仰慕关系存在传递性,求所有的牛都仰慕的对象有多少。。
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<stack>
 5 #define maxn 10005
 6 #define maxm 50010
 7 using namespace std;
 8 int n,m;
 9 struct edge{
10     int u,v,next;
11 }e[maxm],ee[maxm];
12 int head[maxn],js,headd[maxn],jss,bianshu;
13 bool exist[maxn];
14 int visx,cur;// cur--缩出的点的数量 
15 int dfn[maxn],low[maxn],belong[maxn],chudu[maxn],rudu[maxn];
16 stack<int>st;
17 void add_edge(int u,int v){
18     e[++js].u=u;e[js].v=v;
19     e[js].next=head[u];head[u]=js;
20 }
21 void add_edgee(int u,int v){
22     ee[++jss].u=u;ee[jss].v=v;
23     ee[jss].next=head[u];head[u]=jss;
24 }
25 void tarjan(int u){
26     dfn[u]=low[u]=++visx;
27     st.push(u);exist[u]=true;
28     for(int i=head[u];i;i=e[i].next){
29         int v=e[i].v;
30         if(dfn[v]==0) {
31             tarjan(v);
32             low[u]=min(low[v],low[u]);
33         }
34         else if(exist[v]&&low[u]>dfn[v]) low[u]=dfn[v];
35     }
36     int j;
37     if(low[u]==dfn[u]){
38         ++cur;
39         do{
40             j=st.top();st.pop();
41             exist[j]=false;belong[j]=cur;
42             rudu[cur]++;
43         }while(j!=u);
44     }
45 }
46 int main()
47 {
48     scanf("%d%d",&n,&m);
49     int u,v;
50     for(int i=1;i<=m;i++){
51         scanf("%d%d",&u,&v);
52         add_edge(u,v);
53     }
54     for(int i=1;i<=n;i++)
55       if(dfn[i]==0)
56         tarjan(i);      
57     for(int i=1;i<=js;i++){
58         int u=e[i].u,v=e[i].v;
59         if(belong[u]!=belong[v]){
60             add_edgee(belong[u],belong[v]);
61             chudu[belong[u]]++;
62         }
63     }
64     int chu=0,ru=0;
65     for(int i=1;i<=cur;i++){
66         if(chudu[i]==0) chu++,bianshu=i;
67     }
68     if(chu==1) printf("%d\n",rudu[bianshu]);
69     else printf("%d\n",0);
70     return 0;
71 }

思路:首先强连通分量中的奶牛都互相仰慕,所以进行缩点,然后求出出度为零的点的个数,若有一个,则就是答案(这个强连通分量的点的个数),若不止一个,则无答案。。。//我用rudu数组记录的每个强连通分量里点的个数。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值