1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| struct edge{ int next,value,to; }e[N]; int n, m; int head[N],cnt; int root,dfn[N],low[N],stk[N],top,t,num; vector<int> dcc[N];
void add(int u, int v){ e[++cnt].to=v; e[cnt].next=head[u]; head[u]=cnt; }
void tarjan(int u, int root) { dfn[u]=low[u]=++ t; stk[++top]=u; if(u==root&&!head[u]) { dcc[++num].push_back(u); return; } for(int i=head[u];i;i=e[i].next) { int v=e[i].to; if(!dfn[v]) { tarjan(v,root); low[u]=min(low[u],low[v]); if(low[v]>=dfn[u]){ ++num; int y; do { y=stk[top --]; dcc[num].push_back(y); }while(y!=v); dcc[num].push_back(u); } } else low[u]=min(low[u],dfn[v]); } } void work(){ cin>>n>>m; rep(i,1,m){ int u,v; cin>>u>>v; if(u==v) continue; add(u,v),add(v,u); } rep(i,1,n){ if(!dfn[i]) tarjan(i,i); } }
|