[USACO08JAN]手机网络Cell Phone Network
[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
输入输出样例
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的更多相关文章
- 洛谷P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cel ...
- P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [USACO08JAN]手机网络Cell Phone Networ题目描述 Farmer John has decided to give each of his cows a cell ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...
- luogu P2899 [USACO08JAN]手机网络Cell Phone Network |贪心
include include include include include include define db double using namespace std; const int N=1e ...
- [USACO08JAN] 手机网络 - 树形dp
经典问题系列 覆盖半径\(1\)的最小点覆盖集 \(f[i][0]\) 表示不在此处建信号塔,但\(i\)及其子树都有信号 \(f[i][1]\) 表示在此处建信号塔,但\(i\)及其子树都有信号 \ ...
- Android监听手机网络变化
Android监听手机网络变化 手机网络状态发生变化会发送广播,利用广播接收者,监听手机网络变化 效果图 注册广播接收者 <?xml version="1.0" encodi ...
- 用BroadcastReceiver监听手机网络状态变化
android--解决方案--用BroadcastReceiver监听手机网络状态变化 标签: android网络状态监听方案 2015-01-20 15:23 1294人阅读 评论(3) 收藏 举报 ...
- Android之监测手机网络状态的广播
Android之监测手机网络状态的广播 Android 监控网络状态 Android利用广播监听设备网络连接(断网)的变化情况
随机推荐
- ajax分页实现(php)
ajax分页实现(php) 使用jquery.pagination.js插件 引入js文件.css文件 <link rel="stylesheet" href="/ ...
- DIV+CSS 规范命名集合
一: 命名规范说明: 1).所有的命名最好都小写 2).属性的值一定要用双引号("")括起来,且一定要有值如class="divcss5",id="d ...
- poj2513- Colored Sticks 字典树+欧拉通路判断
题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...
- 接口测试-jmeter
一.jmeter下载安装 1)安装jdk(见博文<windows上安装jdk>) 2)在jmeter官网下载 http://jmeter.apache.org/download_jmet ...
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 22: invalid continuation byte
在使用python读取文本文件,一般会这样写: # -*- coding:utf-8 -*- f = open("train.txt", "r", encodi ...
- Natas Wargame Level 15 Writeup(Content-based Blind SQL Injection)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAq4AAACGCAYAAAAcnwh0AAAABHNCSVQICAgIfAhkiAAAIABJREFUeF
- HTM CSS 笔记乱炖
一.常用实体(字符转义) '<' == '<' '©' == '©' '>' == '>' '"' == '"' ' ' == ' ' '®' == '®' ...
- iOS tableView移除某一行的分割线 让分割线宽度为整个cell的宽度
移除tableViewCell中某一行的分割线 有2种方法 1. 移除系统的分割线,自己定义每行的分割线 self.tableView.separatorStyle = UITableViewCell ...
- DOM4J介绍与代码示例(2)-XPath 详解
XPath 详解,总结 XPath简介 XPath是W3C的一个标准.它最主要的目的是为了在XML1.0或XML1.1文档节点树中定位节点所设计.目前有XPath1.0和 XPath2.0两个版本.其 ...
- 观《IT培训行业揭秘》触发北大青鸟回忆
在园子里看到这篇文章<IT培训行业解密(六)>时,挺有感触,回忆顿时涌上心头: 我想起了当年单纯的我们因为各自的原因来到北大青鸟,或因前途迷茫而选择想找一条出路,或因父母的信息闭塞而想给我 ...