[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 ...
随机推荐
- 【Linux 命令】fping ping 包间隔时间详解
服务器间检查会用到fping的命令,期间遇到了一个问题,需要将ping包间的间隔时间设置为100毫秒,查看fping -h看下,找到了-i和-p两个参数: 看到这两个参数,我当时的表情是这样的: 看不 ...
- 快速幂取模_C++
一.题目背景 已知底数a,指数b,取模值mo 求ans = ab % mo 二.朴素算法(已知可跳过) ans = 1,循环从 i 到 b ,每次将 ans = ans * a % mo 时间复杂度O ...
- Python ctypes中cast/py_object用法
class ctypes.py_object Represents the C PyObject * datatype. Calling this without an argument create ...
- Tensorflow常用函数说明(一)
首先最开始应该清楚一个知识,最外面的那个[ [ [ ]]]括号代表第一维,对应维度数字0,第二个对应1,多维时最后一个对应数字-1:因为后面有用到 1 矩阵变换 tf.shape(Tensor) 返回 ...
- TCP 传输控制协议(转)
开头先说几个协议: IP:网际协议 TCP:传输控制协议 Http:超文本传输协议 AMQP:高级消息队列协议 一:TCP是什么? TCP(Transmission Control Protocol ...
- Dijkstra算法(转)
基本思想 通过Dijkstra计算图G中的最短路径时,需要指定起点s(即从顶点s开始计算). 此外,引进两个集合S和U.S的作用是记录已求出最短路径的顶点(以及相应的最短路径长度),而U则是记录还未求 ...
- webgote的例子(2)Sql注入(SearchGET)
Sql注入(Search/GET) 大家好!!! 现如今web服务在我们的网络上遍地都是,各个终端设备成为我们看不见的客户,web服务也成为公司的招牌.80 443为我们展现的视角也是多姿多彩但背后新 ...
- Machine Learning系列--EM算法理解与推导
EM算法,全称Expectation Maximization Algorithm,译作最大期望化算法或期望最大算法,是机器学习十大算法之一,吴军博士在<数学之美>书中称其为“上帝视角”算 ...
- openjudge-NOI 2.6-2985 数字组合
题目链接:http://noi.openjudge.cn/ch0206/2985/ 题解: 跟背包问题有点相似,暂且算背包型DP吧,虽然是一道递推题…… fj表示和为j时的结果,得: 即为j减去每一个 ...
- ASP.NET Core 上传大文件无法接收的问题
解决办法:在API项目中配置 1. 在 web.config 文件中 <system.webServer>里加入 <security> <requestFiltering ...