题解:CF1399E1 Weights Division (easy version)

· · 题解

CF1399E1 Weights Division (easy version) 题解

看到有的题解把这道题的贪心分解的比较细致,看上去比较复杂。其实这道题的思路非常的直接。 题意不过多赘述,首先要对树有一个大概的了解。树上是无环的,所以根据这个性质,我们只要确定根节点,是可以对于树有一个关于点深度方面的明确的定义的(从根节点经过几条边的简单路径可以到达该节点,根节点深度为 0)。以及一棵树中,每个父节点有儿子节点,没有儿子节点的是叶子节点

根据树的特殊的性质,结合这道题可知,从根节点走向所有的叶子节点的路径总权值之和就是每条边的权值 × 该边被多少条“根-叶子”路径覆盖的总和(简单的 \sum 推导)。我们把“边被覆盖的路径数”定义为这条边的贡献系数,记为 cnt_ee 表示某条边)。

举个简单的例子。若边 e 连接父节点 u 和子节点 v,且以 v 为根的子树中有 k 个叶子节点,那么所有从根到这 k 个叶子的路径都会经过边 e,因此 cnt_e = k。初始总权值和 tot 可表示为:

tot = \sum_{e \in edges} w_e \times cnt_e

其中 w_e 是边 e 的初始权值。

贪心操作

题目要求通过“将边权除以二(向下取整)”的操作,让 tot \le S,且操作次数最少。思路就很直接:操作次数最少代表每一步选择操作的边对于总权值和 tot 的影响值(减小值)一定要最多。注意题目中的权值需要向下取整,而有下取整操作的运算是不可以和直接做除法等价的。尤其是这里有一个对于边权的乘法操作,就无法保证对于原数的大小关系可以直接被除后并下取整的新数继承。形式上:

x, y \in \mathbb{R},\; p \in \mathbb{R}^+,\; i \in \mathbb{R}^+ x > y \nRightarrow \left\lfloor \frac{x}{p} \right\rfloor > \left\lfloor \frac{y}{p} \right\rfloor x > y \Rightarrow \left\lfloor \frac{x}{p} \right\rfloor \ge \left\lfloor \frac{y}{p} \right\rfloor x > y \nRightarrow \left\lfloor \frac{x}{p} \right\rfloor \times i \ge \left\lfloor \frac{y}{p} \right\rfloor \times i

所以这里的贪心策略直接按照删除边之后减少的边权乘以出现次数,得到对于这条边进行操作之后对于总数 tot 的减少值为关键字,使用 priority_queue 每次取出队首做一遍操作即可。

对于这道题:

对于边 e,假设当前权值为 w,贡献系数为 cnt_e

这意味着:对同一条边的多次操作,收益是“先大后小”的;而不同边之间,我们只需每次选当前收益最大的操作执行,就能保证总操作次数最少。

具体实现

通过一次 DFSBFS 遍历树即可完成。需要注意的是本题不保证按照父到子的顺序输入边,所以必须建双向边并在函数中判父节点跑,边数组开两倍。

priority_queue 默认大根堆。但是我的代码实现传了三个参,所以用了结构体并重载了 < 运算符(太菜)

AC Code


#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
// typedef long long ll;
#define int long long
#define mp(a, b) make_pair(a, b)

const int N = 1e5 + 10;
int e[N << 1], ne[N << 1], h[N], w[N << 1], idx;
int tot = 0;

void add(int u, int v, int w1){
    e[idx] = v, ne[idx] = h[u], w[idx] = w1, h[u] = idx++;
}
int n, S;
int dp[N];

struct node{
    int times, w1, score;
    bool operator <(const node &W)const{
        return score < W.score;
    }
};
priority_queue<node> heap;

void dfs(int u, int fa){
    bool flag = 0;
    for(int i=h[u];~i;i=ne[i]){
        int j = e[i];
        if(j == fa) continue;
        flag = 1;
        dfs(j, u);
        tot += w[i] * dp[j];//计算加入总边权
        heap.push({dp[j], w[i], (w[i] - w[i] / 2) * dp[j]});
        dp[u] += dp[j];
    }
    if(!flag){
        dp[u] = 1;
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while(T --){
        memset(h, -1, sizeof h);
        memset(dp, 0, sizeof dp);
        idx = 0;
        while(!heap.empty()) heap.pop();
        tot = 0;
        cin >> n >> S;
        for(int i=1;i<n;i++){
            int u, v, w;
            cin >> u >> v >> w;
            add(u, v, w);
            add(v, u, w);
        }
        dfs(1, -1);
        int ans = 0;
        while(tot > S){
            ans ++;
            auto t = heap.top();
            heap.pop();
            int times = t.times, w1 = t.w1, score = t.score;
            tot -= t.score;
            int w1_ = w1 / 2;
            heap.push({times, w1_, (w1_ - w1_ / 2) * times});
        }
        cout << ans << "\n";
    }
    return 0;
}