PKU 2186 Popular Cows

看完解题报告敲的....
没啥好说的做个模板贴出来-_-
Popular Cows
Time Limit:1s Memory limit:32M
Accepted Submit:25 Total Submit:64
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.


这里把解题报告里面的一些思路贴出来 。。
容易发现,新图中唯一的出度为0的点即为所求。
因为新图不含有环,这样的点一定存在。
如果出度为0的点不唯一则无解。
幻灯片 15
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. #define SIZE 10050
  5. typedef vector<int> VI;
  6. typedef vector<VI> VII;
  7. VII adj;//原图
  8. VII tadj;//反向图
  9. bool v[SIZE];//标记是否访问
  10. int ttime[SIZE];//顺序
  11. int n,m,top,scc;//scc标号
  12. int id[SIZE];
  13. int idtotal[SIZE];
  14. int out[SIZE];//出度
  15. int num;
  16. void dfs1(int i)//按顺序标记点
  17. {
  18.  int k,temp;
  19.  for(k=0;k<adj[i].size();k++)
  20.  {
  21.   temp=adj[i][k];//下一个点
  22.   if(!v[temp])
  23.   {
  24.    v[temp]=true;
  25.    dfs1(temp);
  26.    ttime[top++]=temp;
  27.   }
  28.  }
  29. }
  30. void dfs2(int i)
  31. {
  32.  int k,temp;
  33.  id[i]=scc;
  34.  for(k=0;k<tadj[i].size();k++)
  35.  {
  36.   temp=tadj[i][k];
  37.   if(!v[temp])
  38.   {
  39.    v[temp]=true;
  40.    num++;
  41.    dfs2(temp);  
  42.   }
  43.  }
  44. }
  45. void init()
  46. {
  47.  int i,x,y;
  48.  top=0;
  49.  adj.resize(n+1);
  50.  tadj.resize(n+1);
  51.  for(i=0;i<adj.size();i++)adj[i].clear();
  52.  for(i=0;i<tadj.size();i++)tadj[i].clear();
  53.  fill_n(out,SIZE,0);
  54.  for(i=1;i<=m;i++)
  55.  {
  56.   scanf("%d%d",&x,&y);
  57.   adj[x].push_back(y);
  58.   tadj[y].push_back(x);
  59.  }
  60. }
  61. void solve()
  62. {
  63.  int i;;
  64.  int len,j,temp;
  65.  init();
  66. fill_n(v,SIZE,false);
  67. for(i=1;i<=n;i++)
  68.  {
  69.   if(!v[i])
  70.   {
  71.    v[i]=true;
  72.    dfs1(i);
  73.    ttime[top++]=i;
  74.   }
  75.  }
  76. fill_n(v,SIZE,false);
  77. fill_n(idtotal,SIZE,0);
  78.  scc=0;
  79.  for(i=top-1;i>=0;i--)//逆序
  80.  {
  81.   if(!v[ttime[i]])
  82.   {
  83.    num=1;scc++;
  84.    v[ttime[i]]=true;
  85.    dfs2(ttime[i]);
  86.    idtotal[scc]=num;//子图编号的总的顶点数
  87.   }
  88.  }
  89.  int find_ans=0;
  90.  int ans;
  91.  for(i=1;i<=n;i++)
  92.  {
  93.   len=adj[i].size();
  94.   for(j=0;j<len;j++)
  95.   {
  96.    temp=adj[i][j];
  97.    if(id[temp]!=id[i])out[id[i]]++;
  98.   }
  99.  }
  100.  for(i=1;i<=scc;i++)
  101.  {
  102.      if(!out[i])find_ans++,ans=idtotal[i];
  103.  }
  104. printf("%d/n",find_ans==1?ans:0);
  105. }
  106. int main()
  107. {
  108.  while(scanf("%d%d",&n,&m)!=EOF)solve();
  109.  return 0;
  110. }



下面是自己写的
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. typedef vector<int> VI;
  5. typedef vector<VI> VII;
  6. #define N 10001
  7. VII mat1,mat2;
  8. bool v[N];
  9. int o[N],olen,n,t,out[N],ccnt[N],clen,cnt,pre[N];
  10. void mark(int x)
  11. {
  12.     int i,tmp,tlen=mat1[x].size();
  13.     for(i=0;i<tlen;i++)
  14.     {
  15.         tmp=mat1[x][i];
  16.         if(!v[tmp]){
  17.         
  18.             v[tmp]=true;
  19.             mark(tmp);
  20.             o[olen++]=tmp;
  21.         }
  22.     }
  23. }
  24. void dfs(int x)
  25. {
  26.     int i,tmp,tlen=mat2[x].size();
  27.     pre[x]=clen;
  28.     for(i=0;i<tlen;i++){
  29.         tmp=mat2[x][i];
  30.         if(!v[tmp]){
  31.             v[tmp]=true;
  32.             cnt++;
  33.             dfs(tmp);
  34.         }
  35.     }
  36. }
  37. inline void init()
  38. {
  39.     int i,x,y;
  40.     mat1.resize(n+1);mat2.resize(n+1);
  41.     for(i=olen=0;i<=n;i++)mat1[i].clear(),mat2[i].clear();
  42.     for(i=0;i<t;i++)scanf("%d%d",&x,&y),mat1[x].push_back(y),mat2[y].push_back(x);
  43. }
  44. inline void solve()
  45. {
  46.     int i,j,tmp,tlen,ans;
  47.     memset(v,false,sizeof(v));
  48.     for(i=1;i<=n;i++){
  49.         if(!v[i])
  50.             {
  51.                 v[i]=true;
  52.                 mark(i);
  53.                 o[olen++]=i;
  54.             }
  55.     }
  56.     memset(v,false,sizeof(v));
  57.     memset(ccnt,0,sizeof(ccnt));
  58.     memset(out,0,sizeof(out));
  59.     clen=0;
  60.     for(i=olen-1;i>=0;i--){
  61.         if(!v[o[i]]){
  62.         cnt=1;
  63.         clen++;
  64.         v[o[i]]=true;
  65.         dfs(o[i]);
  66.         ccnt[clen]=cnt;
  67.         }
  68.     }
  69.     for(i=1;i<=n;i++){
  70.         tlen=mat1[i].size();
  71.         for(j=0;j<tlen;j++){
  72.             tmp=mat1[i][j];
  73.             if(pre[tmp]!=pre[i])out[pre[i]]++;
  74.         }
  75.     }
  76.     for(i=1,tmp=0;i<=clen;i++){
  77.         if(out[i]==0)tmp++,ans=ccnt[i];
  78.     }
  79.     if(tmp==1)printf("%d/n",ans);
  80.     else printf("0/n");
  81. }
  82. int main()
  83. {
  84.     while(scanf("%d%d",&n,&t)!=EOF){init();solve();}
  85.     return 0;
  86. }

你可能感兴趣的:(Integer,url,textbox,output,behavior,Numbers)