Luo Gu P2341 [HAOI2006] popular cattle explanations

Today the school is strongly connected component Tarjan algorithm, made this road is similar to board questions of title (although I tune 1.5h). The main idea is to use shrink after Tarjan point, find the degree of each point (actually a degree, because I was the opposite of even side). in case

There is one and only one point of penetration is greater than zero, the number is the answer that strongly connected component in some points. See the specific implementation of code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define maxn 10010
 6 #define maxm 50010
 7 using namespace std;
 8 int dfn[maxn],low[maxn],st[maxn],inn[maxn],head[maxm];
 9 int de[maxn],si[maxn];
10 int n,m,cnt,top,inl,num;
11 struct node
12 {
13     int u,v,nex;
14 }edge[maxm];
15 inline int read() 
16 {
17     int x=0;
18     bool f=1;
19     char c=getchar();
20     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
21     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
22     if(f) return x;
23     return 0-x;
24 }
25 inline void write(int x)
26 {
27     if(x<0){putchar('-');x=-x;}
28     if(x>9)write(x/10);
29     putchar(x%10+'0');
30 }
31 inline void add(int x,int y)
32 {
33     cnt++;
34     edge[cnt].u=x;
35     edge[cnt].v=y;
36     edge[cnt].nex=head[x];
37     head[x]=cnt;
38 }
39 inline void Tarjan(int from)//用Tarjan缩点 
40 {
41     dfn[from]=low[from]=++num;
42     st[++top]=from;
43     for(int i=head[from];i!=-1;i=edge[i].nex)
44     {
45         int to=edge[i].v;
46         if(!dfn[to])
47         {
48             Tarjan(to);
49             low[from]=min(low[from],low[to]);
50         }
51         else if(!inn[to])
52             low[from]=min(low[from],dfn[to]);
53     }
54     if(low[from]==dfn[from])
55     {
56         inn[from]=++inl;
57 is          ++ Si [INL]; // record for each strongly connected components in the number of points 
58          the while (ST [Top] =! From )
 59          {
 60              ++ Si [INL]; // ibid 
61 is              Inn [ST [ Top]] = INL;
 62 is              - Top;
 63 is          }
 64          - Top;
 65      }
 66  }
 67  int main ()
 68  {
 69      Memset (head, - . 1 , the sizeof (head));
 70      n-= Read (); = m Read ();
71 is      for ( int I = . 1 ; I <= m; I ++ )
 72      {
 73 is          int X, Y;
 74          X = Read (); Y = Read ();
 75          the Add (Y, X); // trans with even edges , so that the degree of order can not out of the 
76      }
 77      for ( int I = . 1 ; I <= n-; I ++ )
 78          IF (! DFN [I])
 79              Tarjan (I); // condensing point 
80      for ( int I = . 1 ; I <= n-; I ++ )
 81         for (int J = head [I];! J = - . 1 ; J = Edge [J] .nex)
 82             IF (Inn [I] = Inn [Edge [J] .v]) de [Inn [Edge [J]! .v]] ++; // update penetration 
83      int ANS = 0 , U = 0 ;
 84      for ( int I = . 1 ; I <= INL; I ++ )
 85      {
 86          IF (! de [I]) // If the zero is greater than 
87          {
 88              ANS = Si [I]; // assignment 
89              U ++; // record has several points which meet the conditions 
90          }
 91      }
92      IF (U == . 1 ) // determines whether there is only one greater than the zero point 
93          Write (ANS);
 94      the else  
95          Write ( 0 ); // else no solution output 
96      return  0 ;
 97 }
Please Gangster treatise(Anyway, I do not know what that means treatise)

 

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/11237703.html