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. Wannafly挑战赛12 B T95要减肥 【贪心】

    链接:https://www.nowcoder.com/acm/contest/79/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  2. 窥探 Swift 之别具一格的 Struct 和 Class

    说到结构体和类,还是那句话,只要是接触过编程的小伙伴们对这两者并不陌生.但在Swift中的Struct和Class也有着令人眼前一亮的特性.Struct的功能变得更为强大,Class变的更为灵活.St ...

  3. String源码中hashCode算法

    针对java中String源码hashcode算法源码分析 /** The value is used for character storage. */ private final char val ...

  4. Java多线程系列 基础篇03 线程的优先级和守护线程

    1. 线程优先级 现代操作系统中基本上使用时间分片的方式调度线程,通过设置线程优先级,使优先级高的线程获得时间片的次数多于优先级低的线程. 在java 线程中,通过一个整形变量prority来控制优先 ...

  5. Java中的内存泄漏

    [转]介绍Java中的内存泄漏 1. 什么是内存泄漏? 内存泄漏的定义:对象已经没有被应用程序使用,但是垃圾回收器没办法移除它们,因为还在被引用着. 要想理解这个定义,我们需要先了解一下对象在内存中的 ...

  6. 作业:xml练习1

    作业:使用xml描述下表中的学生成绩信息,XML文件存为scores.xml. 1.打开eclipse软件. 2.file-new-java project,输入project name:201811 ...

  7. RQNOJ 95 多多看DVD(加强版):01背包

    题目链接:https://www.rqnoj.cn/problem/95 题意: 叔叔要陪多多看动画片. 有n张DVD可以买,第i张碟的打分为w[i],播放时间为t[i]. 爷爷规定他们只能在一定的时 ...

  8. html5--2.3新的布局元素(2)-article

    html5--2.3新的布局元素(2)-article 学习要点 了解article元素的语义和用法 完成一个简单的实例 article元素(标签) 用于定义一个独立的内容区块,比如一篇文章,一篇博客 ...

  9. hadoop集群的安装

    Hadoop集群安装 1.配置JDK环境和设置主机名,本地解析 JDK环境教程: http://www.cnblogs.com/wangweiwen/p/6104189.html 本地解析: vim ...

  10. hdu-5780 gcd(数学)

    题目链接: gcd Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Pro ...