强联通分量
#include<cstdio> #include<vector> #include<iostream> #include<stack> using namespace std; const int maxn=100010; int n,m,cntcol; vector<int> map[maxn]; vector<int> fmap[maxn]; bool vis[maxn]; stack<int> st; int color[maxn],colnum[maxn]; void dfs1(int x) { vis[x]=true; for(int i=0;i<map[x].size();i++) { int xx=map[x][i]; if(vis[xx]==false) dfs1(xx); } st.push(x); } void dfs2(int x,int col) { color[x]=col; colnum[col]++; for(int i=0;i<fmap[x].size();i++) { int nn=fmap[x][i]; if(color[nn]==0) dfs2(nn,col); } return; } int main() { // freopen("d.txt","r",stdin); // freopen("messagez.out","w",stdout); cin>>n>>m; for(int i=1,x,y;i<=m;i++) { cin>>x>>y; map[x].push_back(y); fmap[y].push_back(x); } for(int i=1;i<=n;i++) if(vis[i]==false) dfs1(i); while(!st.empty()) { for(int i = 1;i <= n;i++) cout << color[i]<<" "; cout<<endl; int tp=st.top(); while(color[tp]>0) { st.pop(); if(st.empty()==true) break; tp=st.top(); } if(st.empty()==true) break; cntcol++; dfs2(tp,cntcol); } for(int i=1;i<=n;i++) { if(colnum[color[i]]>1) cout<<"T"<<endl; else cout<<"F"<<endl; } fclose(stdin); fclose(stdout); return 0; }