刷题计划——树上问题

A. Parsa’s Humongous Tree

题目概述:
一颗树,树的每个节点可以取 [ a , b ] [a,b] [a,b] 之间的值, 对每个节点取值后,求相连的节点的差的绝对值总和的最大值。

思路:

  1. 对于每个点,不用取中间值,只需取左右端点值就可得到最后答案,这是因为如果对于该点左边的点做差后少了 k k k ,在与后面的点做差时必然会多 k k k.
  2. 接下来只需树形Dp做一下 f ( i , 0 ) f(i, 0) f(i,0) 表示 点 i i i 取左端点, f ( i , 1 ) f(i, 1) f(i,1) 表示点 j j j 取右端点。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 2e5 + 10, M = N * 2;
int n;
int e[M], ne[M], h[N], idx;
ll f[N][2]; //f[i][0] 表示选左端点 f[i][1] 表示选右端点

struct Node {
    ll a, b;
}p[N];

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void dfs(int u, int fa) {
     for(int i = h[u]; ~i; i = ne[i]) {
         int j = e[i];
         if(j == fa) continue;
         dfs(j, u);
         f[u][0] += max(abs(p[u].a - p[j].a) + f[j][0], abs(p[u].a - p[j].b) + f[j][1]);
         f[u][1] += max(abs(p[u].b - p[j].a) + f[j][0], abs(p[u].b - p[j].b) + f[j][1]);
     }
}

int main() {
   ios::sync_with_stdio(false); 
   cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
   freopen("D:/Cpp/program/Test.in", "r", stdin);
   freopen("D:/Cpp/program/Test.out", "w", stdout);
#endif
    int t;
    cin >> t;
    while(t --) {
        idx = 0;
        memset(f, 0, sizeof f);
        memset(h, -1, sizeof h);
        cin >> n;
        for(int i = 1; i <= n; i ++) cin >> p[i].a >> p[i].b;

        for(int i = 1; i < n; i ++) {
            int a, b;
            cin >> a >> b;
            add(a, b), add(b, a);
        }

        dfs(1, -1);
        cout << max(f[1][0], f[1][1]) << '\n';
    }
    return 0;
}

F. Towers

参考巨巨题解:
Codeforces Global Round 19 A~F

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> PII;
const int N = 2e5 + 10, M = N * 2;
int e[M], ne[M], h[N], idx;
ll d[N], g[N];
ll ans = 0;
int n, root;

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

ll dfs(int u, int fa) {
    ll maxn = 0;
    if(u != root) {
        for(int i = h[u]; ~i; i = ne[i]) {
            int j = e[i];
            if(j == fa) continue;
            maxn = max(maxn, dfs(j, u));
        }
        //当子树最大高度小于当前点时,更新最大高度,更新答案
        if(maxn < g[u]) ans += g[u] - maxn;
        return max(maxn, g[u]);
    } else {
        if(d[u] == 1) { //最大值在叶子节点上
            //将自己更新为 g[root],并且将另一个最大的叶子节点也设置为 g[root]
            for(int i = h[u]; ~i; i = ne[i]) {
                int j = e[i];
                if(j == fa) continue;
                maxn = max(maxn, dfs(j, u));
            }
            ans += 2 * g[u] - maxn;
        } else { //最大节点在中间位置
            //取两个最大的叶子节点为 g[root]
            multiset<ll> s;
            for(int i = h[u]; ~i; i = ne[i]) {
                int j = e[i];
                if(j == fa) continue;
                s.insert(dfs(j, u));
            }
            auto it = -- s.end();
            ans += g[u] - *it;
            it --;
            ans += g[u] - *it;
        }
        return 0ll;
    }
}

int main() {
   ios::sync_with_stdio(false); 
   cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
   freopen("D:/Cpp/program/Test.in", "r", stdin);
   freopen("D:/Cpp/program/Test.out", "w", stdout);
#endif
    cin >> n;
    for(int i = 1; i <= n; i ++) cin >> g[i];
    memset(h, -1, sizeof h);
    for(int i = 1; i < n; i ++) {
        int a, b;
        cin >> a >> b;
        add(a, b), add(b, a);
        d[a] ++, d[b] ++;
    }

    root = max_element(g + 1, g + n + 1) - g;
    dfs(root, -1);
    cout << ans << '\n';
}

E. Spanning Tree Queries

贴大佬博客:Educational Codeforces Round 122 (Rated for Div. 2) A~E

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> PII;
const int N = 55, M = 310 * 2;
int n, m;
ll fa[N];
struct Edge {
    ll a, b, w;
}e[M];

void init() {
    for(int i = 1; i <= n; i ++) fa[i] = i;
}

int find(int x) {
    if(x != fa[x]) fa[x] = find(fa[x]);
    return fa[x];
}

int main() {
   ios::sync_with_stdio(false); 
   cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
   freopen("D:/Cpp/program/Test.in", "r", stdin);
   freopen("D:/Cpp/program/Test.out", "w", stdout);
#endif
    cin >> n >> m;
    for(int i = 1; i <= m; i ++) {
        int a, b, w;
        cin >> a >> b >> w;
        e[i] = {a, b, w};
    }
    set<ll> s; //会对最小生成树相对大小关系产生影响的节点
    for(int i = 1; i <= m; i ++) 
        for(int j = i + 1; j <= m; j ++) 
            s.insert(abs(e[i].w + e[j].w) / 2 + 1);

    for(int i = 1; i <= m; i ++) s.insert(e[i].w);

    ll p, k, a, b, c;
    cin >> p >> k >> a >> b >> c;
    vector<ll> query(k);
    for(int i = 0; i < p; i ++) {
        cin >> query[i];
    }
    for(int i = p; i < k; i ++) {
        query[i] = (query[i - 1] * a + b) % c; 
    }
    s.insert(0); //初始需要求的最小生成树
    sort(query.begin(), query.end());
    ll ans = 0, res = 0, pre_x = 0, num1 = 0, num2 = 0;
    for(auto x : query) {
        if(s.size() and *s.begin() <= x) {
            while(s.size() and *s.begin() <= x) s.erase(s.begin());

            init();
            sort(e + 1, e + 1 + m, [&](auto e1, auto e2) {
                return abs(e1.w - x) < abs(e2.w - x);
            });

            res = 0, num1 = 0, num2 = 0;
            for(int i = 1; i <= m; i ++) {
                int pa = find(e[i].a), pb = find(e[i].b);
                if(pa != pb) {
                    res += abs(e[i].w - x);
                    fa[pa] = pb;
                    if(e[i].w <= x) num1 ++;
                    else num2 ++;
                }
            }
        } else {
            //如果不对整棵树产生改变,则树的权值变化为:
            res += (x - pre_x) * (num1 - num2);
        }
        ans ^= res;
        pre_x = x;
    }
    cout << ans << '\n';
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值