首页 > 其他分享 >Codeforces 1646 D. Weight the Tree

Codeforces 1646 D. Weight the Tree

时间:2022-11-17 21:33:10浏览次数:47  
标签:1646 int 好点 Tree Codeforces else down fa 节点

题意

给你n个节点的树,让你给每个节点进行赋值,并且赋的值需要为正整数;
同时当一个节点的值等于所有邻居节点的值的和时,这个点为好点;
求出一组赋值情况,满足树的好点个数最大化的同时,所有节点赋值的总和最小;

思路

1. 显然无法存在两个好点相邻存在的情况(除非只有两个节点); 2. 对于坏点直接赋值为1即可; 3. 可以树形dp解决,f[x][0/1][0/1],第一维代表以x为根,第二维代表是否为好点,第三维代表是好点的个数/子树节点值的总和

代码

#include<bits/stdc++.h>

using namespace std;
vector<int> g[200005];
int f[200005][2][2];
long long ans[200005];
int cnt[200005];

void dp(int x, int fa) {
    f[x][1][0] = 1;//好点个数
    f[x][1][1] = g[x].size();//累计
    f[x][0][0] = 0;
    f[x][0][1] = 1;
    for (auto k: g[x]) {
        if (k == fa)continue;
        dp(k, x);
    }
    for (auto k: g[x]) {
        if (k == fa)continue;
        f[x][1][0] += (f[k][0][0]);
        f[x][1][1] += (f[k][0][1]);
    }
    for (auto k: g[x]) {
        if (k == fa)continue;
        if (f[k][0][0] > f[k][1][0]) {
            f[x][0][0] += f[k][0][0];
            f[x][0][1] += f[k][0][1];
        } else if (f[k][0][0] == f[k][1][0]) {
            f[x][0][0] += f[k][0][0];
            f[x][0][1] += min(f[k][0][1], f[k][1][1]);
        } else {
            f[x][0][0] += f[k][1][0];
            f[x][0][1] += f[k][1][1];
        }
    }
}

void down(int x, int fa, int now) {
    ans[x] = now;
    if (now == 1) {
        for (auto k: g[x]) {
            if (k == fa)continue;
            down(k, x, 0);
        }
    } else {
        for (auto k: g[x]) {
            if (k == fa)continue;
            if (f[k][0][0] > f[k][1][0]) {
                down(k, x, 0);
            } else if (f[k][0][0] == f[k][1][0]) {
                if (f[k][0][1] < f[k][1][1])
                    down(k, x, 0);
                else
                    down(k, x, 1);
            } else {
                down(k, x, 1);
            }
        }
    }
}

void run() {
    int n;
    cin >> n;
//    for (int i = 1; i <= n; i++)g[i].clear(), f[i] = 0, ans[i] = 0;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].emplace_back(y);
        g[y].emplace_back(x);
    }
    if (n == 2) {
        cout << 2 << ' ' << 2 << '\n';
        cout << 1 << ' ' << 1 << '\n';
        return;
    }
    dp(1, 0);
    bool flag = 1;
    if (f[1][0][0] > f[1][1][0])flag = 0;
    if (f[1][0][0] == f[1][1][0] && f[1][0][1] < f[1][1][1])flag = 0;
    down(1, 0, flag);
    cout << f[1][flag][0] << ' ' << f[1][flag][1] << '\n';
    for (int i = 1; i <= n; i++) {
        if (ans[i])cout << g[i].size() << ' ';
        else cout << 1 << ' ';
    }
}

int main() {
    run();
    return 0;
}

标签:1646,int,好点,Tree,Codeforces,else,down,fa,节点
From: https://www.cnblogs.com/4VDP/p/16901071.html

相关文章

  • CodeForces - 372C Watching Fireworks is Fun
    题意:有n个点,其中m个要放烟花。每个放烟花的点有属性b[i],放的时间t[i]和位置a[i]。假设放烟花的时候你在位置x,那么可以获得快乐b[i]-|x-a[i]|。你走来走去地看烟花,起始位置......
  • Codeforces Round #833 (Div. 2)-B
    B题目链接:https://codeforces.com/contest/1748/problem/B Whatisthemaximumpossiblelengthadiversestring?100!10个数字每个出现10次暴力n*102下见代码#......
  • el-tree 初始加载中状态
    问题二次封装了一个el-tree组件MenuTree,想要在树形数据nodeData传递之前,树显示为loading加载中的状态。原代码是在MenuTree中监听nodeData,data中声明treeLo......
  • Codeforces Gym 100958 E Cellular Automaton (Makoto rng_58 Soejima contest) 题解
    题目链接其实"序列中1的数量有限"是一个非常重要的条件。这意味着我们可以找到序列中的第一个1和最后一个1。考虑这样一件事情:初始时我们把一个长度为\(2^{2w+1}\)的"滑......
  • el-tree 的 setCheckedKeys() 无效
    问题打开对话框时,加载里面的树,并且勾选默认节点。在对话框中监听dialogOpen为true时就调用el-tree的setCheckedKeys()方法。但是总是报错无法读取树。试过$next......
  • node-v18.11.0-x64.msi安装npm时卡在sill idealTree buildDeps
    造成上述问题的原因是因为node的默认安装环境在国外,因此我们只需要修改下镜像的地址即可npmconfigsetregistryhttps://registry.npm.taobao.org查看是否安装成功:......
  • Java 中Map接口及其实现子类HashMap,Hashtable,Properties,TreeMap类的详解
    前言:对应的代码如下publicclassMap_{publicstaticvoidmain(String[]args){//Map接口实现类的特点,使用实现类HashMap//1.Map与Collection并列......
  • Codeforces Round #833 (Div. 2) D
    D.ConstructOR转化题意a|x=k1db|x=k2d我们考虑k1k2同样就只用让x包含a|b对于a|b的每一位我们用d的最后一位来填补然后在线的要是a|b这里有我们的x没有显然要让......
  • CodeForces - 212E IT Restaurants
    题意:给一棵树的结点染色,每个结点可以染红色、蓝色或不染色。相邻两个结点必须染同一种颜色,或者一个染色一个不染色。求整棵树染色结点最多,且在整棵树至少有一个结点染红色,......
  • From CodeForces Catlogs
    2022/11/1https://codeforces.com/blog/entry/106346On"isthisgreedyorDP",forcingandrubberbandsreadingotherpeople'sthoughtprocessesTheylookatth......