Exercises
1.Tarjan indentation after the deep search for each connected component, see which connected components can be reached, the sum of all connected components can be recorded as sums. The contribution of the I connected component to the answer is size[i]*sum (to other connected components) +size[i]*size[i] (itself to each other)
2. Search the Internet ... This problem can be directly DFS over ... Sweat. "Positive Solution" is Tarjan + topological sort + state compression.
1 /**************************************************************2 problem:22083 User:tunix4 language:c++5 result:accepted6 time:9476 Ms7 memory:64772 KB8 ****************************************************************/9 Ten //Bzoj 2208 One#include <vector> A#include <cstdio> -#include <cstdlib> -#include <cstring> the#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) + #defineD (i,j,n) for (int i=j;i>=n;--i) - using namespacestd; + Const intn= -; AtypedefLong LongLL; at voidReadint&v) { -v=0;intsign=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9') {if(ch=='-') sign=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} -v*=Sign ; - } in /*******************tamplate********************/ - intTo1[n*n],head1[n],next1[n*n],cnt=0; to intto2[n*n],head2[n],next2[n*N]; + voidINS1 (intXinty) { -To1[++cnt]=y; NEXT1[CNT]=HEAD1[X]; head1[x]=CNT; the } * voidIns2 (intXinty) { $To2[++cnt]=y; NEXT2[CNT]=HEAD2[X]; head2[x]=CNT;Panax Notoginseng } - /***********************************************/ the intn,m; + intdfn[n],low[n],dfs_clock=0, belong[n],num,size[n]; A intst[n],top=0; the BOOL inch[N]; + voidTarjan (intx) { - inty; $dfn[x]=low[x]=++Dfs_clock; $st[top++]=x; - inch[x]=1; - for(intI=head1[x];i;i=Next1[i]) { they=To1[i]; - if(!Dfn[y]) {Wuyi Tarjan (y); thelow[x]=min (low[x],low[y]); - } Wu Else if(inch[y]) low[x]=min (low[x],dfn[y]); - } About if(low[x]==Dfn[x]) { $num++; size[num]=0; - for(y=0; y!=x;) { -y=st[--top]; - inch[y]=0; Abelong[y]=num; +size[num]++; the } - } $ } the voidrebuild () { theCnt=0; theF (x,1, N) the for(intI=head1[x];i;i=Next1[i]) - if(belong[x]!=Belong[to1[i]]) in ins2 (Belong[x],belong[to1[i]); the } the /***********************************************/ About BOOLVis[n]; the intsum=0; the voidDfsintx) { the inty; + for(intI=head2[x];i;i=Next2[i]) { -y=To2[i]; the if(!Vis[y]) {Bayivis[y]=1; thesum+=Size[y]; the dfs (y); - } - } the } the voidsolve () { theLL ans=0; theF (I,1, num) { -memset (Vis,0,sizeofvis); thesum=0; the DFS (i); theans+=size[i]*sum+size[i]*Size[i];94 } theprintf"%lld\n", ans); the } the intMain () {98 #ifndef Online_judge AboutFreopen ("Input.txt","R", stdin); - #endif101 read (n);102 CharS[n];103F (I,1, N) {104scanf"%s", s); the Rep (J,strlen (s))106 if(s[j]=='1') ins1 (i,j+1);107 }108F (I,1, N)if(!Dfn[i]) Tarjan (i);109 rebuild (); the solve ();111 return 0; the}View Code
"Bzoj" "2208" "JSOI2010" connectivity number