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. 三、Git 分支

    使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线.有人把 Git 的分支模型称为它的`‘必杀技特性’',也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出. 1.分支简介 ...

  2. C++递归方法实现全排列

    #include<iostream> using namespace std; void perm(int list[],int k,int m);//声明 void perm(int l ...

  3. php 文件加载方式

    两种加载文件的方式 include require 使用场景: 动态加载文件的时候,使用include,否则使用require. 示例: # 引入php文件--include方式 inlcude(&q ...

  4. pytorch 6 batch_train 批训练

    import torch import torch.utils.data as Data torch.manual_seed(1) # reproducible # BATCH_SIZE = 5 BA ...

  5. 《你又怎么了我错了行了吧》【Beta】Scrum Meeting 2

    第二天 日期:2019/6/25 前言: 第2次会议在女生宿舍召开 确认编码阶段已经完成,继续测试项目 1.1 今日完成任务情况以及明日任务安排 姓名 当前阶段任务 下一阶段任务 刘 佳 完善了未开发 ...

  6. 前端和后台对接时对sign加密方法

    前端和后台对接时对sign加密方法 /*后台php对接进行sign标签加密 1 获取向后台请求的数据data(key/value方式),可以是个对象(obj),也可以是数组(arr); 2 将数据的k ...

  7. WinServer-IIS-js无法加载问题

    IIS中无法加载JS文件错误 尝试下面的几种解决方法,一起用

  8. HDU 4309 Contest 1

    最大流建图.开始以为旧桥有1000座,没敢用枚举,后来看看题目发现了只是十二座.枚举桥的状态没问题. 对于隧道的容量W,可以虚拟出第三个结点表示,如u->v.增加一个点p,u->p(INF ...

  9. IntelliJ IDEA启动spring boot项目出现Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]

    IntelliJ IDEA启动spring boot项目出现Failed to start component [StandardEngine[Tomcat].StandardHost[localho ...

  10. linux 磁盘分区,主分区,扩展分区,逻辑分区以sata接口为例

     以sata接口(依据linux内核检測其顺序 sda,sdb...)为例, 1, 硬盘的限制,最多仅仅能设置4个分区(主分区+扩展分区),路径例如以下, /dev/sda1  /dev/sda2 ...