博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【bzoj 2060】[Usaco2010 Nov]Visiting Cows 拜访奶牛(树形dp)
阅读量:4306 次
发布时间:2019-06-06

本文共 1746 字,大约阅读时间需要 5 分钟。

2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

Time Limit: 3 Sec  
Memory Limit: 64 MB
Submit: 350  
Solved: 256
[ ][ ][ ]

Description

经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统,里面有N-1条路,每条路连接了一对编号为C1和C2的奶牛(1 <= C1 <= N; 1 <= C2 <= N; C1<>C2).这样,在每一对奶牛之间都有一条唯一的通路. FJ希望贝茜尽快的回到农场.于是,他就指示贝茜,如果对于一条路直接相连的两个奶牛,贝茜只能拜访其中的一个.当然,贝茜希望她的假期越长越好,所以她想知道她可以拜访的奶牛的最大数目.

Input

第1行:单独的一个整数N 第2..N行:每一行两个整数,代表了一条路的C1和C2.

Output

单独的一个整数,代表了贝茜可以拜访的奶牛的最大数目.

Sample Input

7
6 2
3 4
2 3
1 2
7 6
5 6


INPUT DETAILS:

Bessie knows 7 cows. Cows 6 and 2 are directly connected by a road,
as are cows 3 and 4, cows 2 and 3, etc. The illustration below depicts the
roads that connect the cows:

1--2--3--4
|
5--6--7


Sample Output

4

OUTPUT DETAILS:

Bessie can visit four cows. The best combinations include two cows
on the top row and two on the bottom. She can't visit cow 6 since
that would preclude visiting cows 5 and 7; thus she visits 5 and
7. She can also visit two cows on the top row: {1,3}, {1,4}, or
{2,4}.

HINT

Source

[ ][ ][ ]

【题解】【简单的树型dp】

【以f[i][0]表示不选当前点,f[i][1]表示选择当前点。递归dp】

#include
#include
#include
using namespace std;int a[1000010],next[1000010],p[50010],tot;int f[50010][2];int n,ans;inline void add(int x,int y){ tot++; a[tot]=y; next[tot]=p[x]; p[x]=tot; tot++; a[tot]=x; next[tot]=p[y]; p[y]=tot; return;}inline void dp(int x,int fa){ bool b=false; int u=p[x]; while(u!=0) { if(a[u]!=fa) { b=true; dp(a[u],x); f[x][1]+=f[a[u]][0];//因为当前点选了,所以他的儿子不能被选 f[x][0]+=max(f[a[u]][0],f[a[u]][1]);//因为不选当前点,所以他的儿子可选可不选 } u=next[u]; } return;}int main(){ int i,j; scanf("%d",&n); for(i=1;i

转载于:https://www.cnblogs.com/lris-searching/p/9403154.html

你可能感兴趣的文章
量化策略回测BoolC
查看>>
量化策略回测DCCV2
查看>>
mongodb查询优化
查看>>
五步git操作搞定Github中fork的项目与原作者同步
查看>>
git 删除远程分支
查看>>
删远端分支报错remote refs do not exist或git: refusing to delete the current branch解决方法
查看>>
python multiprocessing遇到Can’t pickle instancemethod问题
查看>>
APP真机测试及发布
查看>>
通知机制 (Notifications)
查看>>
10 Things You Need To Know About Cocoa Auto Layout
查看>>
一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
查看>>
iOS 如何放大按钮点击热区
查看>>
ios设备唯一标识获取策略
查看>>
获取推送通知的DeviceToken
查看>>
Could not find a storyboard named 'Main' in bundle NSBundle
查看>>
CocoaPods安装和使用教程
查看>>
Beginning Auto Layout Tutorial
查看>>
block使用小结、在arc中使用block、如何防止循环引用
查看>>
iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
查看>>
iOS开发中遇到的问题整理 (一)
查看>>