Two times Dfs shrinks, and then n times DFS bursts.
1#include <cstdio>2#include <vector>3#include <cstring>4 using namespacestd;5 #defineN 20016vector<int>G[n],rg[n],vs,g2[n];7typedef vector<int>:: iterator ITER;8 Chars[n+1][n+1];9 intCmp[n],sum,cnt[n],ans,n;Ten BOOLVis[n]; One voidDfsintU) A { -vis[u]=1; - for(ITER it=g[u].begin (); It!=g[u].end (); it++) the if(!vis[*it]) -DFS (*it); - Vs.push_back (U); - } + voidDFS2 (intU) - { +vis[u]=1; Acmp[u]=sum; atcnt[sum]++; - for(ITER it=rg[u].begin (); It!=rg[u].end (); it++) - if(!vis[*it]) -DFS2 (*it); - } - voidSCC () in { - for(intI=1; i<=n;i++) to if(!Vis[i]) + DFS (i); -memset (Vis,0,sizeof(Vis)); theITER It=vs.end (); it--; * for(;; it--) $ {Panax Notoginseng if(!vis[*it]) - { thesum++; +DFS2 (*it); A } the if(It==vs.begin ()) Break; + } -memset (Vis,0,sizeof(Vis)); $ } $ voidDFS3 (intU) - { -vis[u]=1; ans+=Cnt[u]; the for(ITER it=g2[u].begin (); It!=g2[u].end (); it++) - if(!vis[*it])WuyiDFS3 (*it); the } - intMain () Wu { -scanf"%d",&n); About for(intI=1; i<=n;i++) $ { -scanf"%s", s[i]+1); - for(intj=1; j<=n;j++) - if(s[i][j]=='1') A { + G[i].push_back (j); the G[j].push_back (i); - } $ } the SCC (); the for(intI=1; i<=n;i++) the for(intj=1; j<=n;j++) the if(s[i][j]=='1'&&cmp[i]!=Cmp[j]) - G2[cmp[i]].push_back (Cmp[j]); in for(intI=1; i<=sum;i++) the { thememset (Vis,0,sizeof(Vis)); About DFS3 (i); the } theprintf"%d\n", ans); the return 0; +}
"Strong Unicom Component Indent" "Search" bzoj2208 [Jsoi2010] Connectivity number