Codeforces581F

参考代码及简单注释:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<sstream>
#include<string>
#include<bitset>
using namespace std;

typedef long long LL;

const LL LINF = (1LL <<63);
const int INF = 1 << 28;

const int NS = 5010;
const int MS = 19;
const LL MOD = 1000000007;

int n;
int deg[NS];//节点的度数
int c[NS];
int dp[NS][NS][2];
//dp[rt][j][0] 表示以rt为根的子树中 有j个节点属于0集合 且根节点rt属于0集合
//dp[rt][j][1] 表示以rt为根的子树中 有j个节点属于0集合 且根节点rt属于1集合
int re[NS][2];

int head[NS], to[NS << 1], next[NS << 1], top;

void init()
{
    fill(deg, deg + n + 1, 0);
    for(int i = 0; i <= n; i++)
    {
        for(int j = 0; j <= n; j++)
        {
            dp[i][j][0] = INF;
            dp[i][j][1] = INF;
        }
    }

    top = 0;
    fill(head, head + n + 1, -1);
}

void addEdge(int u, int v)
{
    to[top] = v;
    next[top] = head[u];
    head[u] = top ++;
}

void dfs(int rt, int fa)
{
    c[rt] = 0;
    dp[rt][0][0] = 0;
    dp[rt][0][1] = 0;
    if(1 == deg[rt])//如果是叶节点
    {
        c[rt] = 1;
        dp[rt][0][0] = INF;//状态不存在
        dp[rt][0][1] = 0;
        dp[rt][1][0] = 0;
        dp[rt][1][1] = INF;//状态不存在
        return ;
    }

    for(int i = head[rt]; i != -1; i = next[i])
    {
        int son = to[i];
        if(fa == son) continue;

        dfs(son, rt);

        int lim = c[rt] + c[son];//最多有lim个叶节点
        for(int j = 0; j <= lim; j++)
        {
            re[j][0] = re[j][1] = INF;
        }

        for(int x = c[rt]; x >= 0; x--)//枚举已经合并部分son的rt子树中 叶节点属于0集合的个数
        {
            for(int k = c[son]; k >= 0; k--)//枚举son子树中 叶节点属于0集合的个数
            {
                int s = x + k;
                int a = dp[rt][x][0];
                int b = dp[rt][x][1];

                int c = dp[son][k][0];
                int d = dp[son][k][1];

                re[s][0] = min(re[s][0], a + c);        //rt属于0集合,son属于0集合,合并时的边rt->son不产生冲突
                re[s][0] = min(re[s][0], a + d + 1);    //rt属于0集合,son属于1集合,合并时的边rt->son产生冲突

                re[s][1] = min(re[s][1], b + c + 1);    //rt属于1集合,son属于0集合,合并时的边rt->son产生冲突
                re[s][1] = min(re[s][1], b + d);        //rt属于1集合,son属于1集合,合并时的边rt->son不产生冲突
            }
        }

        c[rt] = lim;
        for(int j = 0; j <= lim; j++)
        {
            dp[rt][j][0] = re[j][0];
            dp[rt][j][1] = re[j][1];
        }
    }
}

int solve()
{
    if(2 == n)
    {
        return 1;
    }

    int rt = 1;
    while(deg[rt] <= 1)
    {
        rt ++;
    }

    dfs(rt, -1);

    int ans = dp[rt][c[rt] / 2][0];
    return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("581F.in", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif

    while(~scanf("%d", &n))
    {
        init();
        int u, v;
        for(int i = 1; i < n; i++)
        {
            scanf("%d %d", &u, &v);
            addEdge(u, v);
            addEdge(v, u);
            deg[u] ++;
            deg[v] ++;
        }

        printf("%d\n", solve());
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值