[USACO08JAN]手机网络Cell Phone Network

题目描述

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
题解:
很经典的树形dp,状态为f[i][3]分别表示此节点不选且不能被覆盖,此节点选,此节点不选但能被覆盖三种情况,然后动规方程就很显然了。不过多概述,以下是AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m;
int f[][];
struct student
{
int next,to;
}edge[];
int head[],size;
void putin(int from,int to)
{
size++;
edge[size].next=head[from];
edge[size].to=to;
head[from]=size;
}
void dfs(int k,int father)
{
int f0=,f2=,f1=,w=,i,s=;
for(i=head[k];i!=-;i=edge[i].next)
{
int y=edge[i].to;
if(y==father)continue;
dfs(y,k);
s=min(f[y][],f[y][]);
w+=s;
if(f[y][]-s<f0)f0=f[y][]-s;
f1+=min(f[y][],min(f[y][],f[y][]));
if(f2<)f2+=f[y][];
}
f[k][]=f1+;f[k][]=f2;
if(f0==)f[k][]=;
else f[k][]=w+f0;
}
int main()
{
int i,j,from,to;
cin>>n;
for(i=;i<=n;i++)head[i]=-;
for(i=;i<n;i++)
{
scanf("%d%d",&from,&to);
putin(from,to);
putin(to,from);
}
dfs(,);
cout<<min(f[][],f[][]);
return ;
}


[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

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

  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. 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network

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

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

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

  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. Kafka 源码剖析

    1.概述 在对Kafka使用层面掌握后,进一步提升分析其源码是极有必要的.纵观Kafka源码工程结构,不算太复杂,代码量也不算大.分析研究其实现细节难度不算太大.今天笔者给大家分析的是其核心处理模块, ...

  2. Yomob广告在cocos2dx安卓平台的Demo

    Yomob 也是一个广告平台:他的使用方式比较简单哈,这里只说说他的步骤 按照官方文档说明配置环境,我使用的是eclipse,在eclipse上自己将文件copy到项目proj.android下,覆盖 ...

  3. 573. Squirrel Simulation

    Problem statement: There's a tree, a squirrel, and several nuts. Positions are represented by the ce ...

  4. javascript的面向对象详解

    每次说到javascript到面向对象,总感觉自己心里懂,但是却不知道该怎么说,这就是似懂非懂到表现,于是乎,每次一说,就要到处去查找资料,零零碎碎到看了一些,感觉有懂了,但是过段时间,好像又不知道是 ...

  5. JAVA内存模型5-锁

    锁的释放-获取建立的happens before关系        锁是java并发编程中最重要的同步机制.锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息.下面是锁释放-获 ...

  6. clojure 使用阿里云仓库

    clojure 使用阿里云仓库 学习一门语言,如果没有梯子真的是一件非常痛苦的事情,好在阿里巴巴为我们java程序员提供了maven镜像.近期学习clojure,为了找解决方案在stackoverfl ...

  7. ANSJ中文分词使用方法

    一.前言 之前做solr索引的时候就使用了ANSJ进行中文分词,用着挺好,然而当时没有写博客记录的习惯.最近又尝试了好几种JAVA下的中文分词库,个人感觉还是ANSJ好用,在这里简单总结之. 二.什么 ...

  8. GreenDao 使用二

    1.创建一个实体类 1 Entity note = schema.addEntity("Note"); 默认表名就是类名,也可以自定义表名 1.dao.setTableName(& ...

  9. java web 导出Excel 的工具类公用实现

    平时我们在开发后端管理的系统时,经常会出现导出Excel的功能.这个功能很简单,但是呢,我一般就会使用公用的工具类,在一个Util中,暴露公用导出的接口,只需要传入需要导出的数据(一般使用list集合 ...

  10. Swift学习笔记(4):字符串

    目录: 初始化 常用方法或属性 字符串索引 初始化 创建一个空字符串作为初始值: var emptyString = "" // 空字符串字面量 var anotherEmptyS ...