P2863 [USACO06JAN] cow The Cow Prom Prom

Face questions

This is indeed a problem near (is) tarjan board of a question, the topic is rare point of not shrink

The title translated it, that is to say if the body of the rope a cow out in a clockwise direction until you can traverse back down, it shows to complete the round dance, and if a herd of cows in the same strongly connected component, you can complete the round dance, but only because the clockwise access, it is directed (I had to do as a directed graph without actually could get 90 points ??)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<stack>
 7 using namespace std;
 8 const int M=500005;
 9 
10 stack < int > q;
11 int n,m,tot,t,ans;
12 int head[M],next[M],to[M],dfn[M],low[M],num[M];
13 bool ok[M];
14 
15 int mi(int a,int b){return a<b?a:b;}
16 
17 int read(){//快读
18     int x=0,w=1;
19     char ch=getchar();
20     while(ch>'9'||ch<'0'){
21         if(ch=='-'){
22             w=-1;
23         }
24         ch=getchar();
25     }
26     while(CH <= ' . 9 ' && CH> = ' 0 ' ) {
 27          X = (X << . 3 ) + (X << . 1 ) CH-, + ' 0 ' ;
 28          CH = getchar ();
 29      }
 30      return X * W;
 31 is  }
 32  
33 is  void the Add ( int u, int v) {// chain prior to the star point via u, v construct a directed edge
 34 is      TOT ++ ;
 35      Next [TOT] = head [u];
 36      head [U] = TOT;
 37 [     to[tot]=v;
38 }
39 
40 void tarjan(int g){//tarjan板子
41     dfn[g]=low[g]=++t;
42     ok[g]=1;
43     q.push(g);
44     for(int i=head[g];i;i=next[i]){
45         if(!dfn[to[i]]){
46             tarjan(to[i]);
47             low[g]=mi(low[g],low[to[i]]);
48         }
49         else if(ok[to[i]]){
50             low[g]=mi The (Low [G], Low [to [I]]);
 51 is          }
 52 is      }
 53 is      IF (DFN [G] == Low [G]) {
 54 is          int L = 0 ;
 55          the while (q.top ()! && G =! q.empty ()) {
 56 is              L ++ ;
 57 is              OK [q.top ()] = 0 ;
 58              q.pop ();
 59          }
 60          L ++ ; // do here with EMM do-while, not before think of it. .
61 is          OK [q.top ()] = 0 ;
 62 is          q.pop ();
 63 is          ANS + = (L> . 1); // We need to consider the situation from the point to a single strongly connected component
 64      }
 65  }
 66  
67  int main () {
 68      n-= Read ();
 69      m = Read ();
 70      the while (M-- ) {
 71 is          int u, v;
 72          u = Read ();
 73 is          v = Read ();
 74          the Add (u, v); // directed graph, a point u v is a directed edge
 75          // the Add (v, U); 
76      }
 77      for ( int I = . 1 ; I <= n-; I ++ ) {
 78          IF (!dfn[i]){
79             tarjan(i);
80         }
81     }
82     printf("%d\n",ans);
83     return 0;
84 }

Ah vain

Guess you like

Origin www.cnblogs.com/hahaha2124652975/p/11144198.html