题目大意:
  给你一个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的更多相关文章

  1. [USACO08JAN]手机网络Cell Phone Network

    [USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cell phon ...

  2. 洛谷P2899 [USACO08JAN]手机网络Cell Phone Network

    P2899 [USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cel ...

  3. POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法

    POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...

  4. POJ 3659 Cell Phone Network(树的最小支配集)(贪心)

    Cell Phone Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6781   Accepted: 242 ...

  5. 树的最小支配集 E - Cell Phone Network POJ - 3659 E. Tree with Small Distances

    E - Cell Phone Network POJ - 3659 题目大意: 给你一棵树,放置灯塔,每一个节点可以覆盖的范围是这个节点的所有子节点和他的父亲节点,问要使得所有的节点被覆盖的最少灯塔数 ...

  6. 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463

    B - Strategic game POJ - 1463   题目大意:给你一棵树,让你放最少的东西来覆盖所有的边   这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...

  7. P2899 [USACO08JAN]手机网络Cell Phone Network

    P2899 [USACO08JAN]手机网络Cell Phone Networ题目描述 Farmer John has decided to give each of his cows a cell ...

  8. 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)

    题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...

  9. 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network

    题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...

随机推荐

  1. vue登录/查看/结束端口号

    下班时间到啦! --下班都是他们的,而我,还是什么都没有. vue登录(未登录情况下不允许进入) 在路由里加上登录的权限 meta: { requireAuth: true, title: 'Logi ...

  2. android ViewPager之OnPageChangeListener接口

    项目中在使用ViewPager的时候,一般都要在界面滑动的时候做一些事情,android中有个专门的状态回调接口OnPageChangeListener. /** * Callback interfa ...

  3. SQLite3 C/C++ 开发接口简介(API函数)

    from : http://www.sqlite.com.cn/MySqlite/5/251.Html 1.0 总览 SQLite3是SQLite一个全新的版本,它虽然是在SQLite 2.8.13的 ...

  4. Qualcom QMI系列-基本知识介绍(转)

    1 引言1.1 编写目的       介绍Qualcom QMI 基本知识,API使用,设计原理,基于QMI的RemoteEfs(NV)分析1.2 阅读建议       高通平台入门1.3 参考资料 ...

  5. Linux是对用户的密码的复杂度要求设置【转】

    那么Linux是如何实现对用户的密码的复杂度的检查的呢?其实系统对密码的控制是有两部分组成: 1 cracklib 2 /etc/login.defs pam_cracklib.so 才是控制密码复杂 ...

  6. Linux软件管理器(如何使用软件管理器来管理软件)2---安装及管理Linux应用程序

    安装及管理Linux应用程序 Linux应用程序的组成1.普通的可执行程序文件,一般保存在/usr/bin目录中,普通用户即可执行.2.服务器程序.管理程序文件,一般保存在/usr/sbin目录中,需 ...

  7. leetcode 之Remove Duplicates from Sorted Array(1)

    删除数组中的重复元素并返回新数组的个数 思路:保留不同的元素即可. int removeDeplicates(int A[], int n) { ; ; i < n; i++) { if (A[ ...

  8. Button Bashing(搜索)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAx8AAAI2CAIAAAC+EqK4AAAgAElEQVR4nOydf0BT9f7/37fS423mWn

  9. <<Javascript Patterns>>阅读笔记 -- 第2章 基本技巧(二)

    关于for-in循环 循环数据时, 强烈不推荐使用for-in循环.因为当Array对象被扩展后, 再用for-in循环遍历数据会导致逻辑上的错误, 举例说明: var arr = ['a', 'b' ...

  10. 烈焰遮天 cocos 手游mmo 源码 解析

    引擎: cocos2.x 代码: c++ 混合 lua 游戏类型: mmo 工程结构: game : 游戏启动地方 gamelogic:接sdk相关,登陆支付统计等 libFramework:主要本游 ...