hdu 2767

View Code
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<vector>
  4 #define N 20010
  5 using namespace std;
  6 int dfn[N];
  7 int low[N];
  8 int instack[N];
  9 int stack[N];
 10 int belong[N];
 11 int in[N];
 12 int out[N];
 13 int visnum;
 14 int cnt;
 15 int stop;
 16 
 17 int n,m;
 18 vector<int>node[N];
 19 void tarjan(int x)
 20 {
 21     
 22     
 23     dfn[x]=low[x]=++visnum;
 24     instack[x]=1;
 25     stack[++stop]=x;
 26     int j;
 27     for(int i=0;i<node[x].size();i++)
 28     {
 29 
 30 
 31          j=node[x][i];
 32         if(dfn[j]==0)
 33          {
 34             tarjan(j);
 35             if(low[x]>low[j])
 36             low[x]=low[j];
 37         }
 38         else if(instack[j]==1&&low[x]>dfn[j])
 39         low[x]=dfn[j];
 40         
 41         
 42     }
 43     
 44     if(dfn[x]==low[x])
 45     {
 46         
 47         
 48         cnt++;
 49         do
 50         {
 51             j=stack[stop--];
 52             belong[j]=cnt;
 53             instack[j]=0;
 54         }while(x!=j);
 55         
 56     }
 57     
 58 }
 59 int maxa(int x,int y)
 60 {
 61 
 62 
 63   int s;
 64     if(x>y) s=x;
 65     else s=y;
 66     return s;    
 67 }
 68 void init()
 69 {
 70 
 71 
 72     memset(dfn,0,sizeof(dfn));
 73     memset(low,0,sizeof(low));
 74     memset(instack,0,sizeof(instack));
 75     memset(node,0,sizeof(node));
 76     memset(in,0,sizeof(in));
 77     memset(out,0,sizeof(out));
 78     visnum=0;
 79     cnt=0;
 80     stop=0;
 81 }
 82 int main()
 83 {
 84 
 85 
 86     int a,b;
 87     int i,j;
 88     int t; 
 89      scanf("%d",&t);
 90      while(t--){
 91          scanf("%d%d",&n,&m);
 92            if(m==0)
 93            {
 94 
 95 
 96                printf("%d\n",n);
 97                continue;
 98            }
 99            
100            
101            
102                
103          init();
104              for(i=0;i<m;i++)
105              {
106 
107 
108                  scanf("%d%d",&a,&b);
109                  node[a].push_back(b);
110                  
111              }    
112              
113              for(i=1;i<=n;i++)
114              {
115 
116 
117                   if(dfn[i]==0)
118                   tarjan(i);
119              }
120              int s,e;
121              if(cnt==1) printf("0\n");
122              else
123              {
124 
125 
126                  for(i=1;i<=n;i++)
127                  for(j=0;j<node[i].size();j++)
128                  {
129 
130 
131                      s=belong[i];
132                      e=belong[node[i][j]];
133                      if(s!=e)
134                      {
135 
136 
137                          in[e]=1;
138                          out[s]=1;
139                      }
140                  }
141                  int x=0;
142                  int y=0;
143 
144 
145 
146 
147                  for(i=1;i<=cnt;i++)
148                  {
149 
150 
151                      if(!in[i])
152                      x++;
153                      if(!out[i])
154                      y++;
155                  }
156                  
157                  printf("%d\n",maxa(x,y));
158              }
159      }
160       
161 }

 

posted on 2012-09-07 09:38  仁者无敌8勇者无惧  阅读(192)  评论(0编辑  收藏  举报

导航