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

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}.
题解:f[i][0]为不选根节点时最大人数 f[i][1]为选择根节点时最大人数
大力dp一波
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=50000+10;
vector<int>A[maxn];
int f[maxn][2];
inline void dfs(int x,int fa){
    f[x][0]=0;
    f[x][1]=1;
    for(int i=0;i<A[x].size();i++){
        int u=A[x][i];
        if(u==fa)
            continue;
        dfs(u,x);
        f[x][0]+=max(f[u][0],f[u][1]);
        f[x][1]+=f[u][0];
    }
}
int main(){
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    int n;
    scanf("%d",&n);
    int x,y;
    for(int i=1;i<n;i++){
        scanf("%d %d",&x,&y);
        A[x].push_back(y);
        A[y].push_back(x);
    }
    dfs(1,0);
    printf("%d\n",max(f[1][0],f[1][1]));
return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值