poj3659[USACO2008,Jan]Cell Phone Network手机网络

来源:互联网 发布:avi 编辑软件 编辑:程序博客网 时间:2024/05/30 20:09

题目链接:http://poj.org/problem?id=3659

题目大意:


题解:

f[x][0]表示x不选但被覆盖.f[x][1]表示x选.f[x][2]表示x不选且没有被覆盖。(保证孩子们都安全)

f[x][0]就是至少有一个孩子被选。f[x][1]的孩子就随便啊哪种小选哪种。f[x][2]的孩子都不选但要保证安全那就只能是f[y][0]。

#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define inf 1e8#define maxn 11000int mymin(int x,int y){return (x<y)?x:y;}struct node{int x,y,next;}a[maxn*2];int len,first[maxn];int f[maxn][3];void ins(int x,int y){a[++len].x=x;a[len].y=y;a[len].next=first[x];first[x]=len;}void dp(int x,int fa){int f0=inf,f1=0,f2=0,w=0;for (int k=first[x];k!=-1;k=a[k].next){int y=a[k].y;if (y==fa) continue;dp(y,x);int ls=mymin(f[y][0],f[y][1]);w+=ls;if (f[y][1]-ls<f0) {f0=f[y][1]-ls;}f1+=mymin(f[y][1],mymin(f[y][0],f[y][2]));if (f2<inf) f2+=f[y][0];}f[x][1]=f1+1;f[x][2]=f2;if (f0==inf) f[x][0]=inf;else f[x][0]=w+f0;}int main(){//freopen("tower.in","r",stdin);//freopen("tower.out","w",stdout);int n,i,x,y;scanf("%d",&n);len=0;memset(first,-1,sizeof(first));for (i=1;i<n;i++){scanf("%d%d",&x,&y);ins(x,y);ins(y,x);}dp(1,0);printf("%d\n",mymin(f[1][0],f[1][1]));return 0;}


0 0