1 /********************************************2 Network (POJ 3694)3 http://poj.org/problem?id=36944 Bridge Entry question + LCA (the smallest common ancestor in the tree)5 To do this, you have to use the adjacency table (to do this problem to learn), because6 the edge of the given side has a heavy edge, the vector is not good to record the heavy edge. 7 8 ********************************************/9 Ten#include <iostream> One#include <algorithm> A#include <cstring> -#include <cstdio> - using namespacestd; the - Const intm=100005; - - structEage///adjacency Table Building Map + { - intv; + intNext; A intvs; at }; - -Eage eage[m*4]; - intLOW[M],DFN[M],FATHER[M];///Father[i] For the Father node of I - intFLAG[M],VS[M],HEAD[M];///Falg[i] for the side of the Father node of I and I - intdfs_cut,ans,k; in - voidInit () to { +memset (DFN,0,sizeof(DFN)); -memset (Low,0,sizeof(Low)); thememset (Flag,0,sizeof(flag)); *Memset (VS,0,sizeof(VS)); $memset (head,-1,sizeof(head));Panax Notoginsengdfs_cut=ans=k=0; - } the + voidAddintUintv) A { theeage[k].v=v; +eage[k].vs=0; -eage[k].next=Head[u]; $head[u]=k++; $ } - - voidDfsintU///Find a bridge the { -vs[u]=1;Wuyidfn[u]=low[u]=++Dfs_cut; the for(inti=head[u];i!=-1; i=eage[i].next) - { Wu if(!Eage[i].vs) - { Abouteage[i].vs=eage[i^1].vs=1; $ intv=eage[i].v; - if(!Vs[v]) - { -father[v]=u; A Dfs (v); +low[u]=min (low[u],low[v]); the if(dfn[u]<Low[v]) - { $ans++; theflag[v]=1; the } the } the Elselow[u]=min (low[u],dfn[v]); - } in } the } the About voidLCA (intUintv) the { the if(dfn[u]<Dfn[v]) the { +u^=v; -v^=u; theu^=v;Bayi } the while(dfn[u]>Dfn[v]) the { - if(Flag[u]) ans--; -flag[u]=0; theu=Father[u]; the } the while(u!=v) the { - if(Flag[v]) ans--; the if(Flag[u]) ans--; theflag[u]=0; theflag[v]=0;94v=Father[v]; theu=Father[u]; the } the }98 About intMain () - {101 //freopen ("C://Users//Administrator//Desktop//in.txt "," R ", stdin);102 //freopen ("C://Users//Administrator//Desktop//OUT.txt "," w ", stdout);103 intn,m,cut=0;104 while(cin>>n>>m) the {106 if(!n&&!m)return 0;107 Init ();108 for(intI=1; i<=n;i++) father[i]=i;109 intu,v; the while(m--)111 { theCin>>u>>v;113 Add (u,v); the Add (v,u); the } theDfs1);117cout<<" Case"<<++cut<<":"<<Endl;118 intQ;119Cin>>Q; - while(q--)121 {122Cin>>u>>v;123 Lca (u,v);124cout<<ans<<Endl; the }126cout<<Endl;127 } - return 0;129}
POJ 3694 Network (bridge Entry)