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
- poj3659[USACO2008,Jan]Cell Phone Network手机网络
- POJ3659 Cell Phone Network
- poj3659 Cell Phone Network(树形dp)
- 【POJ3659】【USACO 2008 Jan Gold】 3.Cell Phone Network 树上最小支配集/贪心 两种做法
- POJ3659 Cell Phone Network【最小支配集】【贪心】
- poj3659 Cell Phone Network(最小支配集-树形dp)
- 【POJ3659】Cell Phone Network【树上最小点支配】【TreeDP】
- 洛谷P2899 [USACO08JAN]手机网络Cell Phone Network
- USACO 2008 Jan Gold 3.Cell Phone Network 树形dp
- BSOJ: 1708 【USACO 2008 January Gold】Cell Phone Network手机网络
- [BZOJ1596] [Usaco2008 Jan]电话网络
- bzoj1596 [Usaco2008 Jan]电话网络
- 【bzoj1596】[Usaco2008 Jan]电话网络
- 1596: [Usaco2008 Jan]电话网络
- BZOJ1596 [Usaco2008 Jan]电话网络
- 【bzoj1596】[Usaco2008 Jan]电话网络 树形dp
- bzoj 1596: [Usaco2008 Jan]电话网络
- bzoj 1596 [Usaco2008 Jan]电话网络
- Android学习:SeekBar实现音量调节
- java实现ftp上传下载,中文不乱码
- 流式计算storm应用场景简介
- string.h中的函数和基本应用
- HashMap与ConcurrentHashMap的区别
- poj3659[USACO2008,Jan]Cell Phone Network手机网络
- 大数据Hbase 面试题
- 文件写入A到Z
- 常用 Git 命令清单这一篇就够啦!
- 获取JAVA文件路径
- 【STM32】STM32之串口
- java实现输入一个正整数n,输出全部连续正整数相加后等于n的所有序列。
- Servlet处理请求和响应
- Java面向对象思想及其三大特性