POJ 2186 Popular Cows

Popular Cows

Time Limit: 2000MS
Memory Limit: 65536K

Total Submissions: 15043
Accepted: 5959

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

抽象后的题意:求一个给定的有向图中满足下列性质的顶点的数目,这个顶点能够被除其自身以外的所有的其它的顶点通过某条路径到达

个人思路:

利用Kosraju算法(两次DFS)得到给定图的各强连通分量,将得到的各个强连通分量看成顶点(即所谓的缩点)。

想到以上的信息之后只能算是成功了一点点,以下才是关键——

(1) 如果给定的有向图的转置图是非弱连通的(why?),那么可以立刻得出:符合题意的顶点的数目为0

else

(2)(如果给定的有向图的转置图是能够满足弱连通的)那么符合题意的顶点必定存在——不妨设这些符合条件的点为v1,v2,…,vx,那么必然有以下性质:

(2.1)v1,v2,…,vk必定属于同一个强连通分量,不妨令它们所属的强连通分量“顶点”为C1,

反之,所有属于C1的顶点必定是满足题意的顶点

(2.2)它们所属的强连通分量C1被看成顶点后,那么C1的出度必定为0(即给定图中不存在从C1中的某个顶点指向其它强连通分量的顶点的边)

(2.3) 有且仅有一个强连通分量的出度0(即C1)

(以上各性质的详细证明可以参见网上)

根据以上各条性质,最终得到以下算法:

首先判断给定有向图的转置是否弱连通(两次DFS),如果非弱连通那么直接可得答案else

Kosraju->得到各个强连通分量;

将各个强连通分量看成顶点,设法得到各个强连通分量的出度,最终出度为0的强连通分量所包涵的顶点的数目即为题目答案;

另外,由于此题的顶点数N可达到10000,所以我没有采用邻接矩阵记录边的信息,而是采用邻接表,这样难免导致编程复杂度提高;设法求强连通分量的出度时,并不需要真正求出出度,只需知道出度为不为0即可。

 

 

最后贴代码:

Source Code

Problem: 2186

Memory: 1428K
Time: 94MS

Language: C++
Result: Accepted

  • Source Code
       1: #include<iostream>
       2: #include<cstdio>
       3: #include<cstdlib>
       4: #include<cstring>
       5: #include<algorithm>
       6: using namespace std;
       7:  
       8: struct enode{
       9:     int y,next;
      10: };
      11: #define esize 60000
      12: enode e[esize],et[esize];
      13: #define vsize 13000
      14: int first[vsize],etfirst[vsize];
      15: int scnum=0;// number of strong connected part
      16:  
      17: void getdata(int n,int m,enode e[],enode et[]){
      18:     int x,y;
      19:     int index=0;
      20:     for (int i=1;i<=m;++i){
      21:         scanf("%d%d",&x,&y);
      22:         ++index;
      23:         e[index].y=y; e[index].next=first[x]; first[x]
      24:  
      25:         =index;
      26:         et[index].y=x; et[index].next=etfirst[y]; etfirst
      27:  
      28:             [y]=index;
      29:     }
      30: }
      31:  
      32: int ttime=0;
      33: int d[vsize],f[vsize],color[vsize];
      34: void dfs1(int x,int first[],enode e[]){
      35:     color[x]=1;
      36:     d[x]=++ttime;
      37:     int index=first[x];
      38:     while (index){
      39:         if (color[e[index].y]==0) dfs1(e[index].y,first,e);
      40:         index=e[index].next;
      41:     }
      42:     f[x]=++ttime;
      43:     color[x]=2;
      44: }
      45:  
      46: bool isconnected(int n,int f[],int first[],enode e[]){
      47:     int x=1;
      48:     for (int i=2;i<=n;++i)
      49:         if (f[x]<f[i]) x=i;
      50:     dfs1(x,first,e);
      51:     for (int i=1;i<=n;++i)
      52:         if (color[i]==0) return false;
      53:     return true;
      54: }
      55:  
      56: void dfs2(int x,int etfirst[],enode et[]){
      57:     color[x]=scnum;
      58:     int index=etfirst[x];
      59:     while (index){
      60:         if (color[et[index].y]==0) dfs2(et[index].y,etfirst,et);
      61:         index=et[index].next;
      62:     }
      63: }
      64:  
      65: bool cmp(int& p1,int& p2){
      66:     return f[p1]>f[p2];
      67: }
      68: int main(){
      69:     memset(first,0,sizeof(first)); memset(etfirst,0,sizeof(etfirst));
      70:     memset(e,0,sizeof(e));
      71:     memset(d,0,sizeof(d)); memset(f,0,sizeof(f)); 
      72:     int n,m;
      73:     scanf("%d%d",&n,&m);
      74:     getdata(n,m,e,et);
      75:     //Check the connectivity
      76:     memset(color,0,sizeof(color));
      77:     for (int i=1;i<=n;++i)
      78:         if (color[i]==0) dfs1(i,etfirst,et);
      79:     memset(color,0,sizeof(color));
      80:     if (!isconnected(n,f,etfirst,et)) {printf("0\n"); return 0;}
      81:     memset(color,0,sizeof(color));
      82:     ttime=0;
      83:     for (int i=1;i<=n;++i)
      84:         if (color[i]==0) dfs1(i,first,e);
      85:     int forder[vsize];
      86:     for (int i=1;i<=n;++i) forder[i]=i;
      87:     //**以后务必注意sort()的参数的意义:(起始元素位置,最后一个元素的后一个位置,比较函数)
      88:     sort(forder+1,forder+n+1,cmp); 
      89:     memset(color,0,sizeof(color));
      90:     for (int i=1;i<=n;++i)
      91:         if (color[forder[i]]==0) {++scnum; dfs2(forder[i],etfirst,et);}
      92:         //have got the number of strong connected part
      93:         int vnum[vsize]; //vnum[k]:the number of vertex in kth part 
      94:         memset(vnum,0,sizeof(vnum));
      95:         for (int i=1;i<=n;++i) ++vnum[color[i]];
      96:     int hasout[vsize];
      97:     memset(hasout,false,sizeof(hasout));
      98:     int ans=n;
      99:     for (int x=1;x<=n;++x) if (!hasout[color[x]]){
     100:         int index=first[x]; int y;
     101:         while (index){
     102:             y=e[index].y; if (color[y]!=color[x]){ans-=vnum[color[x]]; hasout[color[x]]=false; break;}
     103:             index=e[index].next;
     104:         }
     105:     }
     106:     printf("%d\n",ans);
     107:     return 0;
     108: }

 

1 John 2:15-16“[On Not Loving the World] Do not love the world or anything in the world. If anyone loves the world, love for the Father is not in them. For everything in the world—the lust of the flesh, the lust of the eyes, and the pride of life—comes not from the Father but from the world.”

Brought to you by BibleGateway.com. Copyright (C) . All Rights Reserved.
posted @ 2011-11-12 10:37  NightCat_song  阅读(214)  评论(0编辑  收藏  举报