题目大意:
  给你一个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. 2017 ACM暑期多校联合训练 - Team 5 1008 HDU 6092 Rikka with Subset (找规律)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  2. Sublime text 2/3 SVN插件及使用方法

    Sublime Text是前端利器,作为前端的盆友们已经再熟悉不过了,在项目中经常使用SVN,每次都要切换提交,很麻烦,有了这个SVN插件就很方便了,使用快捷方式提交,更新. Sublime Text ...

  3. spring-boot 属性定义和配置bean

    自定义bean属性 1.定义bean属性 // 通过@ConfigurationProperties加载properties文件内的配置, // 通过prefix属性指定properties的配置的前 ...

  4. EasyUi组合条件分页查询

    1.引入css与js文件 <link rel="stylesheet" type="text/css" href="themes/default ...

  5. Linux 用户态与内核态的交互【转载】

    Linux 用户态与内核态的交互  在 Linux 2.4 版以后版本的内核中,几乎全部的中断过程与用户态进程的通信都是使用 netlink 套接字实现的,例如iprote2网络管理工具,它与内核的交 ...

  6. shell 监控磁盘使用率【转】

    方案一: disks=(`df |sed 1d | awk '{print $1,$5}'|tr -d %`) len=${#disks[@]} ;i<=$len;i=i+));do ];the ...

  7. 剑指offer算法题

    数组中只出现一次的数字(一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字): 解法在于位运算中的异或,直接异或可以得到这两个数的异或,按照最后的有效数字位可以 ...

  8. 生成器(generator)和迭代(iterable , iterator, iteration)

    在搞清楚Generator之前,我们先讨论一下 iterable , iterator, iteration 1.Iterable 我们知道,在Python中所有东西都是object, 比如说变量,容 ...

  9. xshell 映射带跳板机服务器的端口到本地

    1.配置xshell连接跳板机服务器: 2. 3.可用navicate等同过端口连接远程数据库.

  10. [笔记]用gdb调试core dump

    总是隔一段时间才写一次C++,有些东西老是用完就忘了……记一下如何用gdb来调试core dump免得到时候又忘记. 首先需要设置core file的大小,默认是0所以不设不会生成core file ...