Luo Gu P2863 [USACO06JAN] The Cow Prom prom solution to a problem of cattle

Daily questions day11 punch

Analysis

Long time no big Tarjan, and practice exercises template.

Si long sweep over the array is greater than 1 Tarjan look like.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define maxn 10000+10
 6 #define maxm 50000+10
 7 using namespace std;
 8 inline int read() 
 9 {
10     int x=0;
11     bool f=1;
12     char c=getchar();
13     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
14     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
15     if(f) return x;
16     return 0-x;
17 }
18 inline void write(int x)
19 {
20     if(x<0){putchar('-');x=-x;}
21     if(x>9)write(x/10);
22     putchar(x%10+'0');
23 }
24 int n,m,cnt,num,top,col,ans;
25 int head[maxm],dfn[maxn],low[maxn],co[maxn],st[maxn],si[maxn];
26 struct node{int to,next;}edge[maxm];
27 inline void add(int x,int y)
28 {
29     edge[++cnt].to=y;
30     edge[cnt].next=head[x];
31     head[x]=cnt;
32 }
33 inline void Tarjan(int u)
34 {
35     dfn[u]=low[u]=++num;
36     st[++top]=u;
37     for(int i=head[u];i;i=edge[i].next)
38     {
39         int v=edge[i].to;
40         if(!dfn[v])
41         {
42             Tarjan(v);
43             low[u]=min(low[u],low[v]);
44         }
45         else if(!co[v])
46             low[u]=min(low[u],dfn[v]);
47     }
48     if(low[u]==dfn[u])
49     {
50         co[u]=++col;
51         while(st[top]!=u)
52         {
53             si[col]++;
54             co[st[top]]++;
55             --top;
56         }
57         si[col]++;
58         --top;
59     }
60 }
61 int main()
62 {
63     n=read();m=read();
64     for(int i=1;i<=m;i++)
65     {
66         int x=read(),y=read();
67         add(x,y);
68     }
69     for(int i=1;i<=n;i++)
70         if(!dfn[i])
71             Tarjan(i);
72     for(int i=1;i<=col;i++)
73         if(si[i]>1) Years ++ ;
74      write (years);
75      return  0 ;
76 }

Please Gangster treatise(Anyway, I do not know what that means treatise)

Guess you like

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