Test instructions
First give a tree, and then give the M edge, the m edge connected, and then cut off two sides, one is the original side, a new side, asked how many kinds of programs can make the diagram is not connected.
Ideas:
From the perspective of the original side
1. The tree plus edge, must be a ring, plus a (u,v) side of the u->lca->v on the side is covered once
2. When an edge is not covered, delete the edge and any new edge can make the diagram is not connected, that is, M scheme
3. When an edge is covered 1 times, delete the new side of the ring with the edge, that is, there are 1 options
4. When an edge is overwritten more than 1 times, there is no plan
A tree-shaped dp,dp[i] indicates the number of times the edge of point I is attached to its father is overwritten. A new Edge (U,V) is added to ++dp[u],++dp[v],dp[lca (u,v)]-=2, which is calculated from the leaf nodes up, the values of the child nodes are added to the parent node, and the last value on each node is the number of overrides.
Reflection:
1. The multiplication of the evaluation of LCA is not proficient.
2. The root node is not evaluated at the time of calculation.
Code:
1#include <cstdio>2 Const intm=100005;3 #defineSwap (x, y) t=x,x=y,y=t4 intt,cnt,ans,v[m<<1],dp[m],dep[m],hea[m<<1],nex[m<<1],p[m][ -];5 6 intRead ()7 {8 intx=0;CharCh=GetChar ();9 while(ch< -|| Ch> $) ch=GetChar ();Ten while(ch> -&& ch< -) x= (x<<1) + (x<<3) +ch- -, ch=GetChar (); One returnx; A } - - voidAddintXintY) {v[++cnt]=y,nex[cnt]=hea[x],hea[x]=CNT;} the - voidDfsintUintx) - { -dep[u]=dep[p[u][0]=x]+1; + for(intI=hea[u];i;i=Nex[i]) - if(v[i]^x) DFS (v[i],u); + } A at intLcaintXinty) - { - if(dep[x]<Dep[y]) swap (x, y); - for(intI= -; ~i;--i) - if(Dep[p[x][i]]>=dep[y]) x=P[x][i]; - if(x==y)returnx; in for(intI= -; ~i;--i) - if(P[x][i]^p[y][i]) x=p[x][i],y=P[y][i]; to returnp[x][0]; + } - the voidDFS (intUintx) * { $ for(intI=hea[u],y;y=v[i],i;i=Nex[i])Panax Notoginseng if(y^x) DFS (y,u), dp[u]+=Dp[y]; - } the + intMain () A { the intN=read (), m=read (), x,y,i,j; + for(i=1; i<n;++i) X=read (), y=read (), add (x, y), add (y,x); -Dfs1,0); $ for(i=1;i< -;++i) $ for(j=1; j<=n;++j) - if(p[j][i-1]) p[j][i]=p[p[j][i-1]][i-1]; - for(i=1; i<=m;++i) ++dp[x=read ()],++dp[y=read ()],dp[lca (x, y)]-=2; theDFS (1,0); - for(i=2; i<=n;++i)Wuyi if(!dp[i]) ans=ans+m; the Else if(dp[i]==1) ++ans; -printf"%d\n", ans); Wu return 0; -}
POJ 3417 Network