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

输入输出样例

输入样例#1:

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

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的更多相关文章

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

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

  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

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

  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. 缩点【洛谷P1262】 间谍网络

    [洛谷P1262] 间谍网络 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他 ...

  7. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines

    P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. ...

  8. 洛谷 P1262 【间谍网络】

    题库 : 洛谷 题号 : 1262 题目 : 间谍网络 link : https://www.luogu.org/problemnew/show/P1262 思路 : 这题可以用缩点的思想来做.先用T ...

  9. Arctic Network(洛谷)--北极通讯网络(loj)

    洛谷传送门 loj传送门 一道蛮基础的最小生成树的题 题意也没绕什么圈子 只是叙述的有点累赘而已(loj上是这样的 也就读入加建边需要稍稍稍多想一下下 对于我这么一个蒟蒻 这是一道很好的板子题 (洛谷 ...

随机推荐

  1. java和js互调 webview

    public class JavaAndJSActivity extends Activity implements View.OnClickListener { private EditText e ...

  2. JavaMail发送和接收邮件

    一.JavaMail概述:        JavaMail是由Sun定义的一套收发电子邮件的API,不同的厂商可以提供自己的实现类.但它并没有包含在JDK中,而是作为JavaEE的一部分. 厂商所提供 ...

  3. uboot 2013.01 s3c6400编译失败

    通常我们对s3c6410平台开发u-boot是在s3c6400的基础上修改而成的,但是从uboot 2013.01这个版本之后的版本都把smdk6400对应的配置给删除了. 这是因为该版本smdk64 ...

  4. CodeForces - 960F Pathwalks —— 主席树(n棵线段树)

    题目链接:https://vjudge.net/problem/CodeForces-960F You are given a directed graph with n nodes and m ed ...

  5. js中得~~是什么意思/JS按位非(~)运算符与~~运算符的理解分析

    其实是一种利用符号进行的类型转换,转换成数字类型 ~~true == 1 ~~false == 0 ~~"" == 0 ~~[] == 0 ~~undefined ==0 ~~!u ...

  6. 图解HTTP接口自动化测试框架使用

    Robot Framework是一款python语言编写,通用的功能自动化测试框架.它使用了比较易用的表格数据语法,基于关键字驱动测试,主要用来验收测试和验收测试驱动开发(ATDD). 运行RIDE, ...

  7. 如何在VMware Workstation上安装CentOS_7

    1.首先打开VMware Workstation-文件-新建虚拟机 2.选择自定义向导,下一步. 3.由于我的软件版本比较高,不想太多硬件限制就选了版本11.也可以选择低一些版本的,这样兼容性会更好, ...

  8. Java_Path_01_路径问题

    二.参考资料 1.java 路径问题 2.Java路径问题最终解决方案—可定位所有资源的相对路径寻址 3.Java获取文件的路径 4.Thread.currentThread().getContext ...

  9. Ice php配置

    1) Removed the php extension directories and recompiled apache/PHP2) Rebooted the machine.3) I remov ...

  10. AtCoder AGC #4 Virtual Participation

    我好懒啊QAQ 老规矩 从C开始 C.给一个矩阵,里面有一些紫色方块,你需要涂两个矩阵,一个红色,一个蓝色,保证你涂的颜色四连通 然后把红色蓝色矩阵叠起来要求紫色的地方必须是紫色,其他地方不能是紫色 ...