bzoj 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛(树形DP)
2016-04-26 23:15
477 查看
2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 346 Solved: 253
[Submit][Status][Discuss]
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
76 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
4OUTPUT 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
Gold[Submit][Status][Discuss]
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define N 50003 using namespace std; int n,m,next[N*2],point ,u[N*2],tot,f [3]; void add(int x,int y) { tot++; next[tot]=point[x]; point[x]=tot; u[tot]=y; tot++; next[tot]=point[y]; point[y]=tot; u[tot]=x; } void dfs(int x,int fa) { for (int i=point[x];i;i=next[i]) if (u[i]!=fa) { dfs(u[i],x); f[x][0]=f[x][0]+max(f[u[i]][0],f[u[i]][1]); f[x][1]=f[x][1]+f[u[i]][0]; } f[x][1]++; } int main() { scanf("%d",&n); for (int i=1;i<n;i++) { int x,y; if (x>y) swap(x,y); scanf("%d%d",&x,&y); add(x,y); } dfs(1,0); printf("%d",max(f[1][1],f[1][0])); return 0; }
相关文章推荐
- Windwos 08R2_DNS+AD安装图文
- c++第四次实验
- LocalNotification 与远程通知的是如何操作的?
- 随心所欲的DateTime显示格式
- TLD学习(3) arthurv版TLD
- Mysql cluster初学
- swift面向对象之方法
- 关于服务器raid的一个记录
- iOS中UISearchBar(搜索框)使用总结
- 关于服务器raid的一个记录
- xUtils里使用HttpUtils时出现的无法访问HttpRequestBase的错误
- python连接mysql和mongo数据库
- 围圈小朋友报数退出问题
- JavaScript 面向对象编程
- java之代理模式
- iOS UISearchBar 属性 设置详解
- python 调用top命令获取输出信息
- ASI与AFN的区别
- 作业6
- 作业6