[关闭]
@jesseliu612 2016-07-06T11:04:22.000000Z 字数 2731 阅读 1276

经过无数次WA的洗礼,终于得到了AC
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 29828 Accepted: 12101
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.

附代码和注释

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cmath>
  7. #include <stack>
  8. #include <queue>
  9. #include <vector>
  10. #define inf 2147483640
  11. //#define debug
  12. #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
  13. using namespace std;
  14. struct Edge{
  15. int x;
  16. int y;
  17. };
  18. vector<int> e[1000000];
  19. vector<int> g[1000000];
  20. int dfn[1000000],low[1000000],stacka[1000000],inset[1000000],out[1000000],in[1000000];
  21. Edge edge[1000000];
  22. bool instack[1000000]={0};
  23. int dfsnow=0,n,m,top=0,gnow=500000;
  24. void tarjan(int a){
  25. if(dfn[a]==-1){//如果这个点没有被访问,则访问这个点。
  26. dfsnow++;
  27. dfn[a]=dfsnow;//将这个点对应的dfn数组设置为它的dfn
  28. low[a]=dfn[a];
  29. stacka[top]=a;
  30. top++;//把这个点入栈
  31. instack[a]=1;//标记这个点已经入栈
  32. inset[a]=a;//认为这个点初始处在的环即为它自己(似乎不写这一句也可以)
  33. for(int i=0;i<e[a].size();i++){//枚举这个点所有的出边
  34. int j=e[a][i];//j代表i这条边连接的点
  35. if(dfn[j]==-1){//如果j点没有被访问过
  36. tarjan(j);//访问j点
  37. low[a]=min(low[a],low[j]);//用j点能到达的最小值来更新这个点能到达的最小值
  38. }
  39. else{//如果j点被访问过
  40. if(instack[j]==1){//假如j点在栈中,即它仍属于这个环而不是已经被划分到另一个环中
  41. low[a]=min(low[a],low[j]);//用j点能到达的最小值来更新这个点能到达的最小值
  42. }
  43. }
  44. }
  45. if(dfn[a]==low[a]){//当这个点能到达的最小值在更新后仍然等于自己
  46. int j=stacka[top-1];
  47. top--;
  48. instack[j]=0;
  49. g[gnow].push_back(j);
  50. inset[j]=gnow;
  51. while(j!=a){
  52. j=stacka[top-1];
  53. top--;
  54. instack[j]=0;
  55. g[gnow].push_back(j);
  56. inset[j]=gnow;
  57. }//从栈顶开始退栈,退到这个点为止
  58. gnow++;//准备好下一个环的序号
  59. }
  60. }
  61. }
  62. int main(){
  63. memset(in,0,sizeof(in));
  64. memset(out,0,sizeof(out));
  65. scanf("%d%d",&n,&m);
  66. for(int i=0;i<1000000;i++){
  67. dfn[i]=-1;
  68. low[i]=-1;
  69. }
  70. int enow=0;
  71. int t1,t2;
  72. for(int i=1;i<=m;i++){
  73. scanf("%d%d",&t1,&t2);
  74. edge[enow].x=t1;
  75. edge[enow].y=t2;
  76. enow++;
  77. e[t1].push_back(t2);
  78. }
  79. for(int i=1;i<=n;i++)tarjan(i);
  80. int flag=0,who=0,noanswer=0;
  81. for(int i=0;i<enow;i++){
  82. edge[i].x=inset[edge[i].x];
  83. edge[i].y=inset[edge[i].y];
  84. if(edge[i].x!=edge[i].y)
  85. in[edge[i].x]++,
  86. out[edge[i].y]++;
  87. for(int i=500000;i<gnow;i++){
  88. if(in[i]==0 && out[i]>=0){
  89. if(flag!=1){
  90. flag=1;
  91. who=i;
  92. }
  93. else{
  94. noanswer=1;
  95. }
  96. }
  97. }
  98. int ans=0;
  99. if((flag==1 && noanswer!=1 && out[who]>0)||(g[who].size()==n)){
  100. printf("%d",g[who].size());
  101. }
  102. else{
  103. printf("0");
  104. }
  105. fclose(stdin);fclose(stdout);
  106. return 0;
  107. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注