"Algorithm" point double connected component
"The puzzle" is described in the introduction to the algorithm Competition Primer Classic Training Guide p318-319
Details are labeled with important in the code.
#include <cstdio>#include<algorithm>#include<vector>#include<stack>#include<cstring>using namespacestd;Const intmaxm=50010;structedge{intU,v, from;} e[maxm*3];intFirst[maxm],iscut[maxm],dfn[maxm],low[maxm],bccno[maxm],bcc_cnt,dfsnum,tot,n,m,kase;vector<int>Bcc[maxm];stack<int>s;voidInsertintUintv) {Tot++;e[tot].u=u;e[tot].v=v;e[tot]. from=first[u];first[u]=tot;}voidTarjan (intXintFA) {Dfn[x]=low[x]=++Dfsnum; intChild=0; for(intI=first[x];i;i=e[i]. from) if(e[i].v!=FA) { inty=e[i].v;//S.push (i); if(!Dfn[y]) {S.push (i);//child++;//ImportantTarjan (y,x); LOW[X]=min (low[x],low[y]); if(low[y]>=Dfn[x]) {Iscut[x]=1; Bcc_cnt++; Bcc[bcc_cnt].clear (); for(;;) { intnow=s.top (); S.pop (); intu=e[now].u,v=e[now].v; if(bccno[u]!=bcc_cnt) {bcc[bcc_cnt].push_back (u); bccno[u]=bcc_cnt;} if(bccno[v]!=bcc_cnt) {Bcc[bcc_cnt].push_back (v); bccno[v]=bcc_cnt;}//printf ("%d\n", bcc_cnt); if(u==x&&v==y) Break; } } } Else if(Dfn[y]<dfn[x]) s.push (i), Low[x]=min (Low[x],dfn[y]);//Important } if(fa<0&&child==1) iscut[x]=0;}intMain () {scanf ("%d",&m); while(m!=0) {memset (First,0,sizeof(first)); N=0; tot=0; bcc_cnt=0; for(intI=1; i<=m;i++) { intu,v; scanf ("%d%d",&u,&v); N=Max (N,max (u,v)); Insert (U,V); Insert (V,U); } memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); memset (Iscut,0,sizeof(Iscut)); memset (Bccno,0,sizeof(BCCNO)); Dfsnum=0; while(!s.empty ()) S.pop (); for(intI=1; i<=n;i++)if(!dfn[i]) Tarjan (i,-1); Long Longans1=0, ans2=1; for(intI=1; i<=bcc_cnt;i++) { intCut_cnt=0; for(intj=0; J<bcc[i].size (); j + +) if(Iscut[bcc[i][j]]) cut_cnt++; if(cut_cnt==1) {ans1++; Ans2*=(Long Long) (Bcc[i].size ()-cut_cnt); } } if(bcc_cnt==1) {ans1=2; Ans2=bcc[1].size () * (bcc[1].size ()-1)/2; } printf ("Case %d:%lld%lld\n",++kase,ans1,ans2); scanf ("%d",&m); } return 0;}
View Code
"LA" 5135 Mining Your Own Business