Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so they can all communicate.

Exactly N-1 pairs of pastures are adjacent, and for any two pastures A and B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and B is the last. Farmer John can only place cell phone towers in the pastures, and each tower has enough range to provide service to the pasture it is on and all pastures adjacent to the pasture with the cell tower.

Help him determine the minimum number of towers he must install to provide cell phone service to each pasture.

John想让他的所有牛用上手机以便相互交流(也是醉了。。。),他需要建立几座信号塔在N块草地中。已知与信号塔相邻的草地能收到信号。给你N-1个草地(A,B)的相邻关系,问:最少需要建多少个信号塔能实现所有草地都有信号。

输入输出格式

输入格式:

  • Line 1: A single integer: N

  • Lines 2..N: Each line specifies a pair of adjacent pastures with two space-separated integers: A and B

输出格式:

  • Line 1: A single integer indicating the minimum number of towers to install

输入输出样例

输入样例#1:

5
1 3
5 2
4 3
3 5
输出样例#1:

2
思路:经典的树上最小支配集。

判断树的最小支配集,条件如下

1、选一结点为根后序遍历

2、遍历过程中,如果该结点没被覆盖,该结点的父亲结点没被覆盖,且该结点的子结点中没有被覆盖的,则该结点的父亲结点即为一覆盖点

#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 10010
using namespace std;
int n,ans;
int vis[MAXN],over[MAXN];
vector<int>vec[MAXN];
void dfs(int fa,int now){
vis[now]=;
bool flag=false;
for(int i=;i<vec[now].size();i++)
if(!vis[vec[now][i]]){
dfs(now,vec[now][i]);
flag=flag||over[vec[now][i]];
}
if(!over[fa]&&!flag&&!over[now]){
over[fa]=;
ans++;
}
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(,);
cout<<ans;
}
 

P2899 [USACO08JAN]手机网络Cell Phone Network的更多相关文章

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

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

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

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

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

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

  4. luogu P2899 [USACO08JAN]手机网络Cell Phone Network |贪心

    include include include include include include define db double using namespace std; const int N=1e ...

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

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

  6. [USACO08JAN] 手机网络 - 树形dp

    经典问题系列 覆盖半径\(1\)的最小点覆盖集 \(f[i][0]\) 表示不在此处建信号塔,但\(i\)及其子树都有信号 \(f[i][1]\) 表示在此处建信号塔,但\(i\)及其子树都有信号 \ ...

  7. Android监听手机网络变化

    Android监听手机网络变化 手机网络状态发生变化会发送广播,利用广播接收者,监听手机网络变化 效果图 注册广播接收者 <?xml version="1.0" encodi ...

  8. 用BroadcastReceiver监听手机网络状态变化

    android--解决方案--用BroadcastReceiver监听手机网络状态变化 标签: android网络状态监听方案 2015-01-20 15:23 1294人阅读 评论(3) 收藏 举报 ...

  9. Android之监测手机网络状态的广播

    Android之监测手机网络状态的广播 Android 监控网络状态 Android利用广播监听设备网络连接(断网)的变化情况

随机推荐

  1. 好文应该收藏-----redis 配置自启动

    话不多说,请看原作者笔记,亲测,可用 https://blog.csdn.net/qq_42810276/article/details/81296012

  2. qt 摄像头程序

    http://www.oschina.net/code/snippet_124925_3789?p=3#comments http://www.codesoso.net/Search?q=qt+%C9 ...

  3. 关于一些运算(&(与运算)、|(或运算)、^(异或运算)........)的本质理解【转】

    看到一篇博客,关于一些运算的解析,觉得有用,怕以后找不着,直接复制下来,以备以后学习用 原文链接:https://blog.csdn.net/xiaopihaierletian/article/det ...

  4. H5知识点

    一.总体变化 1.H5文档结构 <!DOCTYPE html> <html> <head> <title>  这是标题  </title> ...

  5. CSS3特效——六面体

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 基本配置及安全级别security-level

    interface GigabitEthernet0/0 nameif outside  //指定接口名称 security-level 0  //安全级别设置 ip address 1.1.1.2 ...

  7. Linux学习总结(15)——提高 Vim 和 Shell 效率的 9 个建议

    你上一次使用 CAPSLOCK 键是什么时候?很久没有了对不对?噢,我也是,它已经被遗忘了,它浪费了键盘上一个黄金位置.让我们把它重映射成 Control 键来发挥它的作用吧!这里告诉了你在不同的操作 ...

  8. l洛谷 P2326 AKN’s PPAP

    P2326 AKN’s PPAP 题目描述 “I have a pen,I have an apple.Eh,Apple-Pen!. I have a pen,I have pineapple.En, ...

  9. jquery outerHeight方法 outerWidth方法 获取元素实际宽度高度

    曾经写代码中,每当须要获取元素的实际"宽度"(这里的宽度是指元素宽度加上其边距)时,都须要用元素宽度加上margin值才行,今天发现一个叫outerWidth(options)的方 ...

  10. OSGi 和 C++

    2011年 9月我参加了OSGi社区在达姆施塔特的会议,并且有机会与其他与会者探讨本机c++实现的OSGi规范的现状.在这一事件之前我也一直想写一篇博客,来描述关于当前实现OSGi规范的现状和努力—— ...