【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛(树形dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=2060
裸的树形dp
d[x][1]表示访问x的数量,d[x][0]表示不访问x的数量
d[x][1]=sum{d[y][0]}, y是儿子
d[x][0]=sum{max(d[y][1], d[y][0])}, y是儿子
然后任意找一个点当做根dfs即可。
答案就是max(d[root][0], d[root][1])
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <queue>
- using namespace std;
- #define rep(i, n) for(int i=0; i<(n); ++i)
- #define for1(i,a,n) for(int i=(a);i<=(n);++i)
- #define for2(i,a,n) for(int i=(a);i<(n);++i)
- #define for3(i,a,n) for(int i=(a);i>=(n);--i)
- #define for4(i,a,n) for(int i=(a);i>(n);--i)
- #define CC(i,a) memset(i,a,sizeof(i))
- #define read(a) a=getint()
- #define print(a) printf("%d", a)
- #define dbg(x) cout << (#x) << " = " << (x) << endl
- #define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
- #define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
- inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
- inline const int max(const int &a, const int &b) { return a>b?a:b; }
- inline const int min(const int &a, const int &b) { return a<b?a:b; }
- const int N=50005;
- int ihead[N], cnt, n, d[N][2];
- struct ED { int to, next; }e[N<<1];
- void add(int u, int v) {
- e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
- e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;
- }
- void dfs(int x, int fa) {
- int y;
- d[x][1]=1;
- for(int i=ihead[x]; i; i=e[i].next) if((y=e[i].to)!=fa) {
- dfs(y, x);
- d[x][1]+=d[y][0];
- d[x][0]+=max(d[y][0], d[y][1]);
- }
- }
- int main() {
- read(n);
- rep(i, n-1) add(getint(), getint());
- dfs(1, 0);
- print(max(d[1][0], d[1][1]));
- return 0;
- }
Description
Input
Output
Sample Input
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
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
【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛(树形dp)的更多相关文章
- BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 树形DP
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...
- BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )
树形dp..水 ------------------------------------------------------------------------ #include<cstdio& ...
- 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛 树形dp
题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...
- bzoj 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛【树形dp】
设f[u][0/1]为u这个点不选/选,转移的时候从儿子转移,f[u][1]=sum(f[son][0])+1,f[u][0]=sum(max(f[son][0],f[e[i].to][1])) #i ...
- 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 252 Solved: 1 ...
- 【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
[算法]树形DP [题解]没有上司的舞会?233 f[x][0]=∑max(f[v][0],f[v][1]) f[x][1]=(∑f[v][0])+1 #include<cstdio> # ...
- [bzoj2060][Usaco2010 Nov]Visiting Cows 拜访奶牛_树形dp
Visiting Cows 拜访奶牛 bzoj-2060 Usaco-2010 Nov 题目大意:题目链接. 注释:略. 想法:看起来像支配集. 只是看起来像而已. 状态:dp[pos][flag]表 ...
- 【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛
题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统 ...
- [codevs1380]没有上司的舞会([BZOJ2060][Usaco2010 Nov]Visiting Cows 拜访奶牛)
[codevs1380]没有上司的舞会 试题描述 Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现 ...
随机推荐
- android wifi调试(无线调试) 一步到位
没有数据线时候,怎么进行调试开发?只要在一个局域网内,最好选择wifi调试! 网上有很多这样的教程,但是有很多步.很繁琐.最近我在gp上下载了一个软件可以实现点击一步就可以了.不需要在手机上输入任何命 ...
- 极光推送sdk使用
创建应用 进入极光控制台后,点击“创建应用”按钮,进入创建应用的界面. 填上你的应用程序的名称以及应用包名这二项就可以了, 最后点击最下方的 “创建我的应用”按钮,创建应用完毕. 创建应用 填 ...
- DDL语句--改动表
改动表是指改动数据库中已经存在的表的定义.改动表比又一次定义表简单.不须要又一次载入数据.也不会影响正在进行的服务. MySQL中通过ALTER TABLE语句来改动表.改动表包含改动表名.改动字段数 ...
- sql server 数据加密
select EncryptByPassPhrase('hksjlm',qlrmc)jmsj,* into jmqlr from qlr where qlbh in('5201411120005',' ...
- 区别原生chrome 和以chrome为内核的360浏览器
function isChrome360() { if( navigator.userAgent.toLowerCase().indexOf('chrome') > -1 ) { var des ...
- C#光盘刻录
一.背景介绍 最近公司一个老项目需要添加导出数据到光盘的功能.开始对这功能添加有点抵触的.光盘?都啥年代了. 光盘一种即将淘汰的存储媒介.就像当年的随身听,Mp3,Mp4一样,即将退出历史舞台.领导让 ...
- 翻翻git之---实现QQ空间点赞部分实现的自己定义控件 EasyLikeArea
转载请注明出处:王亟亟的大牛之路 昨天在家里弄鱼的事没上班,也就没写东西.决定今天早上补一篇,正好看到了 Easy like area in the circle of friends or QQ q ...
- C#线程同步技术(二) Interlocked 类
接昨天谈及的线程同步问题,今天介绍一个比较简单的类,Interlocked.它提供了以线程安全的方式递增.递减.交换和读取值的方法. 它的特点是: 1.相对于其他线程同步技术,速度会快很多. 2.只能 ...
- 解决Janusgraph索引状态不变更的问题
JanusGraph的索引因为要同步不同实例及不同后端的数据,因此不是实时能够完成的,视配置,网络和数据量不同,建立/生效索引通常需要一段时间,这也是为什么创建索引时会创建wait()的原因. 在实践 ...
- Cocos2d-x 3.1.1 Lua演示样例 ActionsProgressTest(进度条)
Cocos2d-x 3.1.1 Lua演示样例 ActionsProgressTest(进度条) 本篇博客介绍Cocos2d-x中的进度条动画,进度条涉及以下几个重要的类和方法,笔者来给大家具体解说一 ...