usaco Nov gold 2010 vacation

本文介绍了一个经典的树状动态规划问题——如何在遵循特定规则的情况下尽可能多地访问牛朋友。通过递归构建子树并应用动态规划策略,文章详细解释了算法的设计思路与实现细节。

Problem 3: Visiting Cows [Neal Wu, 2008]

After many weeks of hard work, Bessie is finally getting a vacation!

Being the most social cow in the herd, she wishes to visit her N

(1 <= N <= 50,000) cow friends conveniently numbered 1..N. The cows

have set up quite an unusual road network with exactly N-1 roads

connecting pairs of cows C1 and C2 (1 <= C1 <= N; 1 <= C2 <= N; C1

!= C2) in such a way that there exists a unique path of roads between

any two cows.

FJ wants Bessie to come back to the farm soon; thus, he has instructed

Bessie that if two cows are directly connected by a road, she may

not visit them both. Of course, Bessie would like her vacation to

be as long as possible, so she would like to determine the maximum

number of cows she can visit.

PROBLEM NAME: vacation

INPUT FORMAT:

* Line 1: A single integer: N

* Lines 2..N: Each line describes a single road with two

        space-separated integers: C1 and C2

SAMPLE INPUT (file vacation.in):

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

OUTPUT FORMAT:

* Line 1: A single integer representing the maximum number of cows

        that Bessie can visit.

SAMPLE OUTPUT (file vacation.out):

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}.

 

题意是在树中找一些节点,使得这些节点两两不相邻,问满足该条件的点集中节点最多有多少个。

很显然,树状dp ,深搜后对于每一个子树,都有

dp[t][0]+=Max(dp[k][0],dp[k][1]);

dp[t][1]+=dp[k][0];

其中,t 为父节点, k 为子节点, 0 为不选该节点, 1 为选该节点。

代码如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值