标签:algo name string tac man nod class cout stream
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<string> #include<vector> #include<cstring> #include<iomanip> #include<sstream> #include<iostream> #include<algorithm> #define INF 0x3f3f3f3f #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 //#define ll __int64 //#define int ll //typedef long long ll; typedef unsigned long long ull; const int MAXN=5e5+10; const int MOD=1e9+7; const double eps=1e-6; using namespace std; //-------------------------------------------// vector<int> g[MAXN]; stack<int> s; int dfn[MAXN],low[MAXN],instack[MAXN],cnt,k; vector<int> belong[MAXN]; void tarjan(int u) { dfn[u]=low[u]=++cnt; s.push(u); instack[u]=1; for(int i=0;i<g[u].size();++i) { int v=g[u][i]; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) { low[u]=min(low[u],dfn[v]); } } if(low[u]==dfn[u]) { int node; do { node=s.top(); s.pop(); belong[k].push_back(node); instack[node]=0; }while(node!=u); k++; } } int main() { //ios::sync_with_stdio(false); int n,m; cin>>n>>m; for(int i=0;i<m;++i) { int u,v; cin>>u>>v; g[u].push_back(v); } tarjan(1); for(int i=0;i<k;++i) { cout<<i+1<<": "; for(int j=0;j<belong[i].size();++j) cout<<belong[i][j]<<" "; cout<<endl; } return 0; }
测试数据:
7 11
1 2
2 3
2 5
2 4
3 5
3 7
7 5
5 6
6 7
4 1
4 5
标签:algo name string tac man nod class cout stream
原文地址:https://www.cnblogs.com/megadeth/p/11830122.html