洛谷P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [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
2
/*
f[i][0]表示节点i不选但被覆盖
f[i][1]表示节点i选
f[i][2]表示节点i不选也不被覆盖
*/
#include<iostream>
#include<cstdio>
#define maxn 10010
#define INF 2000000000
using namespace std;
int f[maxn][],n,num,head[maxn];
struct node{
int to,pre;
}e[maxn*];
void Insert(int from,int to){
e[++num].to=to;
e[num].pre=head[from];
head[from]=num;
}
void dfs(int now,int father){
int f0=INF,f2=,f1=,w=,s=;
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
if(to==father)continue;
dfs(to,now);
s=min(f[to][],f[to][]);
w+=s;
f0=min(f0,f[to][]-s);
f1+=min(f[to][],min(f[to][],f[to][]));
if(f2<INF)f2+=f[to][];
}
f[now][]=f1+;f[now][]=f2;
if(f0==INF)f[now][]=INF;
else f[now][]=w+f0;
}
int main(){
int x,y;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
Insert(x,y);
Insert(y,x);
}
dfs(,);
int ans=min(f[][],f[][]);
cout<<ans;
}
洛谷P2899 [USACO08JAN]手机网络Cell Phone Network的更多相关文章
- 洛谷 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 ...
- P2899 [USACO08JAN]手机网络Cell Phone Network
P2899 [USACO08JAN]手机网络Cell Phone Networ题目描述 Farmer John has decided to give each of his cows a cell ...
- luogu P2899 [USACO08JAN]手机网络Cell Phone Network |贪心
include include include include include include define db double using namespace std; const int N=1e ...
- [USACO08JAN]手机网络Cell Phone Network
[USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cell phon ...
- 缩点【洛谷P1262】 间谍网络
[洛谷P1262] 间谍网络 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他 ...
- 洛谷 P1948 [USACO08JAN]电话线Telephone Lines
P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. ...
- 洛谷 P1262 【间谍网络】
题库 : 洛谷 题号 : 1262 题目 : 间谍网络 link : https://www.luogu.org/problemnew/show/P1262 思路 : 这题可以用缩点的思想来做.先用T ...
- Arctic Network(洛谷)--北极通讯网络(loj)
洛谷传送门 loj传送门 一道蛮基础的最小生成树的题 题意也没绕什么圈子 只是叙述的有点累赘而已(loj上是这样的 也就读入加建边需要稍稍稍多想一下下 对于我这么一个蒟蒻 这是一道很好的板子题 (洛谷 ...
随机推荐
- Java for LeetCode 122 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 前端JSONPJIE解决跨域问题
解决同源策略的两个方法 1 . JSONP jsonp (将 JSON 数据填充进回调函数,这就是JSONP的JSON+Padding 的含义) jsonp是json用来跨域的一个东西,原理是通过sc ...
- Flask中的模板语法jinjia2
Flask中默认的模板语言是Jinja2 I. Jinja2模板语言中的 for {% for foo in g %} {% endfor %} II. Jinja2模板语言中的 if {% if g ...
- PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】
题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...
- CSS3 3D下拉折叠菜单
在线演示 本地下载
- PS图片透明处理方法
// http://www.3lian.com/edu/2014/06-18/149890.html 今天想把图片背景做成透明效果,以前叫美工的妹纸帮忙做过,自己没留意学. 今天需要做这个东西,特别记 ...
- sqlite:多线程操作数据库“database is locked”解决方法
1. 使sqlite支持多线程(不确定是否非加不可,暂且加上,以备后患) 可以在编译时/启动时/运行时选择线程模式,参考:http://www.cnblogs.com/liaj/p/4015219.h ...
- 第六章-jQuery
jQuery的理念是: 写更少的代码, 完成更多的工作 jQuery有两个版本1.x和2.x, 版本2.x不再支持IE678 jQuery最明显的标志就是$, jQuery把所有的功能都封装在了jQu ...
- linux命令学习笔记(44):top命令
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管 理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷 ...
- 机器学习:Colorization using Optimization
今天介绍 Siggraph 2004 年的一篇文章: Colorization using Optimization,利用优化的方法对灰度图像进行着色,这里用到了非常经典的泊松方程以及稀疏矩阵的线性优 ...