[USACO08JAN]Cell Phone Network
题目大意:
给你一个n个结点的树,请你搞一些破坏。
你可以选择手动弄坏某个点,那么与它直接相连的点也会自动坏掉。
问你把整棵树搞坏至少要手动弄坏几个点?
思路:
f[0~2][i]表示不同状态下以i为根的子树至少要手动弄坏几个点。
我们可以把点的不同状态分为以下三种:
0:它的孩子有被手动弄坏的。
1:它自己被手动弄坏。
2:它父母被手动弄坏。
设当前根为x,枚举每个孩子y,一个固定的手动弄坏的孩子z,转移方程如下:
f[0][x]=sum{min(f[0][y],f[1][y])|y≠z}+f[1][z];
f[1][x]=sum{min(f[0][y],f[1][y],f[2][y])}+1;
f[2][x]=sum{min(f[0][y],f[1][y])};
最后答案为min(f[0][1],f[1][1])。
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int inf=0x7fffffff;
const int N=;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
int f[][N];
int t[N];
void dp(const int &x,const int &par) {
f[][x]=inf;
f[][x]=;
int tmp=;
for(register std::vector<int>::iterator i=e[x].begin();i!=e[x].end();i++) {
const int &y=*i;
if(y==par) continue;
dp(y,x);
t[y]=tmp;
tmp+=std::min(f[][y],f[][y]);
f[][x]+=std::min(std::min(f[][y],f[][y]),f[][y]);
f[][x]+=std::min(f[][y],f[][y]);
}
tmp=;
for(register std::vector<int>::reverse_iterator i=e[x].rbegin();i!=e[x].rend();i++) {
const int &y=*i;
if(y==par) continue;
t[y]+=tmp;
tmp+=std::min(f[][y],f[][y]);
}
for(register std::vector<int>::iterator i=e[x].begin();i!=e[x].end();i++) {
const int &y=*i;
if(y==par) continue;
f[][x]=std::min(f[][x],t[y]+f[][y]);
}
}
int main() {
const int n=getint();
for(register int i=;i<n;i++) {
add_edge(getint(),getint());
}
dp(,);
printf("%d\n",std::min(f[][],f[][]));
return ;
}
[USACO08JAN]Cell Phone Network的更多相关文章
- [USACO08JAN]手机网络Cell Phone Network
[USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cell phon ...
- 洛谷P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cel ...
- POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法
POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...
- POJ 3659 Cell Phone Network(树的最小支配集)(贪心)
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6781 Accepted: 242 ...
- 树的最小支配集 E - Cell Phone Network POJ - 3659 E. Tree with Small Distances
E - Cell Phone Network POJ - 3659 题目大意: 给你一棵树,放置灯塔,每一个节点可以覆盖的范围是这个节点的所有子节点和他的父亲节点,问要使得所有的节点被覆盖的最少灯塔数 ...
- 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463
B - Strategic game POJ - 1463 题目大意:给你一棵树,让你放最少的东西来覆盖所有的边 这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...
- P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [USACO08JAN]手机网络Cell Phone Networ题目描述 Farmer John has decided to give each of his cows a cell ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...
随机推荐
- 【leetcode 简单】 第五十一题 有效电话号码
给定一个包含电话号码列表(一行一个电话号码)的文本文件 file.txt,写一个 bash 脚本输出所有有效的电话号码. 你可以假设一个有效的电话号码必须满足以下两种格式: (xxx) xxx-xxx ...
- 信息收集之zoomeye
一.浏览器上使用api接口 1.https://api.zoomeye.org/user/login post传参:{"username" : "username&quo ...
- WordPress的SEO插件——WordPress SEO by yoast安装及使用
插件:WordPress SEO by yoast 使用方法: 做好网站SEO一直是站长们的愿望,说简单也简单,但是说难也难,因为需要注意的地方太多,一个不小心被百度K了你都不知道怎么回事.这里和大家 ...
- 121.Best Time to Buy and Sell Stock---dp
题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目大意:给出一串数组,找到差值最大的差 ...
- 135.Candy---贪心
题目链接 题目大意:分糖果,每个小朋友都有一个ratings值,且每个小朋友至少都要有一个糖果,而且每个小朋友的ratings值如果比左右邻舍的小朋友的ratings值高,则其糖果数量也比邻舍的小朋友 ...
- spring web 生命周期理解
spring web /bean 生命周期 反射注解 aop代理类生成 init servlet 初始化 load spring-context.xml load XmlParser 类解析对象 ...
- weblogic12.1.3 静默安装 建域
--安装依赖包 yum -y install compat-libcap1 compat-libstdc++ gcc gcc-c++ glibc-devel libaio-devel libstdc+ ...
- windows下制作debian U盘启动
制作平台:Windows 7 制作debian版本:debian 7.4 wheezy 1.下载引导镜像,包含三个文件:boot.img.gz(解压备用).initrd.gz 和 vmlinuz. h ...
- python模块之cx_Oracle
安装cx_Oracle wget http://download.oracle.com/otn/linux/instantclient/122010/instantclient-basic-linux ...
- navigator.geolocation详解
https://blog.csdn.net/qq_27626333/article/details/51815467 PositionOptions: JSON对象,监听设备位置信息参数 naviga ...