标签:alt queue add std logs hid lap algorithm tarjan
T1:
tarjan裸不能再裸了
1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<cstring>
5 #include<cstdlib>
6 #include<cmath>
7 #include<vector>
8 #include<queue>
9 #define inf 2147483611
10 #define ll long long
11 #define MAXN 10101
12 using namespace std;
13 inline int read()
14 {
15 int x=0,f=1;char ch=getchar();
16 while(!isdigit(ch)) {if(ch==‘-‘) f=-1;ch=getchar();}
17 while(isdigit(ch)) {x=x*10+ch-‘0‘;ch=getchar();}
18 return x*f;
19 }
20 int n,m,first[MAXN],next[MAXN*20],to[MAXN*20],cnt;
21 int st[MAXN],top,scc,stp,dfn[MAXN],low[MAXN],now;
22 bool vis[MAXN];
23 int T;
24 void add(int u,int v) {next[++cnt]=first[u],first[u]=cnt,to[cnt]=v;}
25 void tarjan(int x)
26 {
27 low[x]=dfn[x]=++stp,vis[x]=1,st[++top]=x;
28 for(int i=first[x];i;i=next[i])
29 {
30 if(!dfn[to[i]]) {tarjan(to[i]);low[x]=min(low[x],low[to[i]]);}
31 else if(vis[to[i]]) low[x]=min(low[x],dfn[to[i]]);
32 }
33 if(low[x]==dfn[x])
34 {
35 scc++,now=0;
36 while(now!=x)
37 now=st[top--],vis[now]=0;
38 }
39 }
40 int main()
41 {
42 freopen("net.in","r",stdin);
43 freopen("net.out","w",stdout);
44 int a,b;
45 T=read();
46 while(T--)
47 {
48 memset(next,0,sizeof(next));
49 memset(to,0,sizeof(to));
50 memset(first,0,sizeof(first));
51 memset(dfn,0,sizeof(dfn));
52 memset(low,0,sizeof(low));
53 memset(vis,0,sizeof(vis));
54 scc=cnt=stp=top=0;
55 n=read(),m=read();
56 while(m--)
57 {
58 a=read(),b=read();
59 add(a,b);
60 }
61 for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
62 printf("%d\n",scc);
63 }
64 }
T2:
标签:alt queue add std logs hid lap algorithm tarjan
原文地址:http://www.cnblogs.com/yyc-jack-0920/p/7772286.html