[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 简单】第四十九题 颠倒二进制位
颠倒给定的 32 位无符号整数的二进制位. 示例: 输入: 43261596 输出: 964176192 解释: 43261596 的二进制表示形式为 000000101001010000011110 ...
- zabbix 监控服务器的TCP状态
本文介绍如何监控TCP的11种状态: 1.命令选择: ss or netstat netstat 在 Centos7上已经不再支持,ss 打印基于socket的统计信息,实际运行下来,ss的速度比ne ...
- Tensorflow常用函数说明(一)
首先最开始应该清楚一个知识,最外面的那个[ [ [ ]]]括号代表第一维,对应维度数字0,第二个对应1,多维时最后一个对应数字-1:因为后面有用到 1 矩阵变换 tf.shape(Tensor) 返回 ...
- 设计模式之Composite
设计模式总共有23种模式这仅仅是为了一个目的:解耦+解耦+解耦...(高内聚低耦合满足开闭原则) Composite定义? 将对象以树形结构组织起来,以达成“部分-整体” 的层次结构. 想到Compo ...
- 【shell】shell编程总结
总结一下在写shell脚本时的常见注意事项: 1.shell脚本中的命令最好用命令的全路径,如果不知道全路径可以用which cmd查找命令的全路径. 2.shell脚本中定义环境变量用export ...
- Python3 反射及常用的方法
反射就是通过字符串映射或修改程序运行时的状态.属性.方法 有四个常用方法: hasattr(obj,name_str) 判断一个obj对象是否有对应name_str的方法 getattr(obj,na ...
- 64_p10
python3-matplotlib-qt4-2.0.0-2.fc26.2.x86_64.rpm 05-Apr-2017 09:54 29438 python3-matplotlib-qt5-2.0. ...
- MySQL常见错误代码说明
附:MySQL常见错误代码说明 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据 ...
- rocketmq 记
Rocketmq选型 Rocket是一个专业的队列服务,性能优于Rabbitmq,优势是性能和并发,源于Kafka的扩展版,增强了数据的可靠性. Rocketmq的队列类型 普通队列,广播队列.顺序队 ...
- jquery 通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及未来的元素(比如由脚本创建的新元素)
jquery 通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及未来的元素(比如由脚本创建的新元素) $("ul").append("<li cla ...