BZOJ2466: [中山市选2009]树

138 篇文章 0 订阅

树形DP
首先一个点要么按一次按钮要么不按
f[i][x][y]表示i的子树内除了i全部点亮,i节点是否亮,是否按过按钮,最少按的次数

code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1e8
using namespace std;

inline int read()
{
    char c; int x;
    while(!((c=getchar())>='0'&&c<='9'));
    x=c-'0';
    while((c=getchar())>='0'&&c<='9') (x*=10)+=c-'0';
    return x;
}
inline void down(int &x,const int &y){if(x>y)x=y;}
const int maxn = 310;
const int maxm = 2100;

struct edge
{
    int y,nex;
    edge(){}
    edge(const int &_y,const int &_nex){y=_y;nex=_nex;}
}a[maxn]; int len,fir[maxn];
inline void ins(const int &x,const int &y){a[++len]=edge(y,fir[x]);fir[x]=len;}
int n;
int f[maxn][2][2];

void dfs(const int x,const int fa)
{
    f[x][0][0]=0; f[x][1][1]=1;
    for(int k=fir[x];k;k=a[k].nex)
    {
        const int &y=a[k].y;
        if(y!=fa)
        {
            dfs(y,x);
            int t00=min(f[x][0][0]+f[y][1][0],f[x][1][0]+f[y][1][1]);
            int t01=inf;
            int t10=min(f[x][0][0]+f[y][1][1],f[x][1][0]+f[y][1][0]);
            int t11=f[x][1][1]+f[y][0][0];
            down(t00,inf); down(t10,inf); down(t11,inf);
            f[x][0][0]=t00; f[x][0][1]=t01; f[x][1][0]=t10; f[x][1][1]=t11;
        }
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        memset(fir,0,sizeof fir); len=0;
        for(int i=1;i<n;i++)
        {
            int x=read(),y=read();
            ins(x,y); ins(y,x);
        }
        memset(f,63,sizeof f);
        dfs(1,0);
        printf("%d\n",min(f[1][1][0],f[1][1][1]));
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值