Hdu3452 remove the smallest edge set from the undirected tree so that any leaf is not connected to the root/min cut, and hdu3452 is connected.
The idea is coming up. The leaf connects to the edge of the sink node. inf ensures that the edge will not be cut, and the root node can be cut to the sink node. Pay attention to the bidirectional Edge building of the undirected tree. Basic questions, in minutes 1A:
#include<iostream>#include<queue>#include<cstdio>#include<cstring>#include<set>#include<vector>using namespace std;const int inf=0x3f3f3f3f;const int maxv=1005,maxe=10000;int nume=0;int head[maxv];int e[maxe][3];void inline adde(int i,int j,int c){ e[nume][0]=j;e[nume][1]=head[i];head[i]=nume; e[nume++][2]=c; e[nume][0]=i;e[nume][1]=head[j];head[j]=nume; e[nume++][2]=0;}int ss,tt,n,m;int vis[maxv];int lev[maxv];bool bfs(){ for(int i=0;i<maxv;i++) vis[i]=lev[i]=0; queue<int>q; q.push(ss); vis[ss]=1; while(!q.empty()) { int cur=q.front(); q.pop(); for(int i=head[cur];i!=-1;i=e[i][1]) { int v=e[i][0]; if(!vis[v]&&e[i][2]>0) { lev[v]=lev[cur]+1; vis[v]=1; q.push(v); } } } return vis[tt];}int dfs(int u,int minf){ if(u==tt||minf==0)return minf; int sumf=0,f; for(int i=head[u];i!=-1&&minf;i=e[i][1]) { int v=e[i][0]; if(lev[v]==lev[u]+1&&e[i][2]>0) { f=dfs(v,minf<e[i][2]?minf:e[i][2]); e[i][2]-=f;e[i^1][2]+=f; sumf+=f;minf-=f; } } if(!sumf) lev[u]=-1; return sumf;}int dinic(){ int sum=0; while(bfs())sum+=dfs(ss,inf); return sum;};int ind[maxv];void read_build(){ int aa,bb,cc; for(int i=0;i<n-1;i++) { scanf("%d%d%d",&aa,&bb,&cc); adde(aa,bb,cc); adde(bb,aa,cc); ind[aa]++;ind[bb]++; } for(int i=1;i<=n;i++) if(i!=m&&ind[i]==1) { adde(i,tt,inf); } /* for(int i=0;i<=tt;i++) for(int j=head[i];j!=-1;j=e[j][1]) { if(j%2==0) printf("%d->%d:%d\n",i,e[j][0],e[j][2]); }*/}void init(){ nume=0; ss=m;tt=n+1; for(int i=0;i<=tt;i++) { head[i]=-1;ind[i]=0; }}int main(){ while(~scanf("%d%d",&n,&m)&&(n||m)) { init(); read_build(); int ans; ans=dinic(); printf("%d\n",ans); } return 0;}