Problem A
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 266 Accepted Submission(s): 116
You are given an undirected tree (i.e., a connected graph with no cycles), where each edge (i.e., branch) has a nonnegative weight (i.e., thickness). One vertex of the tree has been designated the root of the tree.The remaining vertices of the tree each have unique paths to the root; non-root vertices which are not the successors of any other vertex on a path to the root are known as leaves.Determine the minimum weight set of edges that must be removed so that none of the leaves in the original tree are connected by some path to the root.
15 15 1 2 1 2 3 2 2 5 3 5 6 7 4 6 5 6 7 4 5 15 6 15 10 11 10 13 5 13 14 4 12 13 3 9 10 8 8 9 2 9 11 3 0 0
16
#include<stdio.h>
#include<string.h>
const int V=1000001,E=1000001;//我不知道为什么,必须要开这么大
int flag[10001];
struct edge
{
int aim,cap;
edge *next,*pair;
edge() {}
edge(int t,int c,edge *n):aim(t),cap(c),next(n) {}
void* operator new(unsigned,void* p)
{
return p;
}
}*e[V],Te[E+E],*Pe=Te;
int n,num[V],dis[V],s,t,r;
int augment(int u,int augc)
{
if(u==t)return augc;
int d,tmp=n-1;
for(edge *i=e[u]; i; i=i->next)if(i->cap)
{
if(dis[u]==dis[i->aim]+1)
{
d=augment(i->aim,augc<i->cap?augc:i->cap);
i->cap-=d,i->pair->cap+=d;
if(d||dis[s]==n)return d;
}
if(dis[i->aim]<tmp)tmp=dis[i->aim];
}
if(!--num[dis[u]])dis[s]=n;
num[dis[u]=tmp+1]++;
return 0;
}
int main()
{
while(scanf("%d%d",&n,&r)!=EOF)
{
if(n==0&&r==0) break;
memset(e,0,sizeof(e));
memset(flag,0,sizeof(flag));
for(int i=1; i<n; i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
e[x+1]=new(Pe++)edge(y+1,c,e[x+1]);
e[y+1]=new(Pe++)edge(x+1,c,e[y+1]);
e[x+1]->pair=e[y+1];
e[y+1]->pair=e[x+1];
flag[x]++;
flag[y]++;
}
for(int i=1; i<=n; i++)
if(flag[i]==1&&i!=r)
{
e[1]=new(Pe++)edge(i+1,1<<25-1,e[1]);
e[i+1]=new(Pe++)edge(1,0,e[i+1]);
e[1]->pair=e[i+1];
e[i+1]->pair=e[1];
}
e[n+2]=new(Pe++)edge(r+1,0,e[n+2]);
e[r+1]=new(Pe++)edge(n+2,1<<25-1,e[r+1]);
e[n+2]->pair=e[r+1];
e[r+1]->pair=e[n+2];
n=n+2;
memset(dis,0,sizeof(dis));
memset(num,0,sizeof(num)),num[0]=n;
int flow=0;
s=1,t=n;
while(dis[s]<n) flow+=augment(s,0xfffff);
printf("%d/n",flow);
}
return 0;
}