915F - Imbalance Value of a Tree - CodeForces Solution


data structures dsu graphs trees *2400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

long long n, mx, mn, u, v, w[1000007];
pair<long long, long long> a[1000007];
vector<long long> adj[1000007];

struct DSU{
    vector<int> par, sz;

    void make_set(int n){
        par.resize(n + 1);
        sz.resize(n + 1);
        for(int i = 1; i <= n; i++){
            par[i] = i;
            sz[i] = 1;
        }
    }

    int find_set(int v){
        if(par[v] == v) return v;
        return par[v] = find_set(par[v]);
    }

    bool union_sets(int u, int v){
        u = find_set(u);
        v = find_set(v);
        if(u == v) return false;
        if(sz[u] < sz[v]) swap(u, v);
        par[v] = u;
        sz[u] += sz[v];
        return true;
    }
} dsu;

void solveMax(){
    dsu.make_set(n);
    sort(a + 1, a + n + 1);
    for(int i = 1; i <= n; i++){
        int u = a[i].second;
        long long last = 0, szu = dsu.sz[dsu.find_set(u)];
        for(int j = 0; j < (int) adj[u].size(); j++){
            int v = adj[u][j];
            if(w[v] <= w[u]){
                long long szv = dsu.sz[dsu.find_set(v)];
                if(dsu.union_sets(u, v)){
                    mx += szu * szv * a[i].first;
                    mx += last * szv * a[i].first;
                    last += szv;
                }
            }
        }
    }
}

bool cmp(const pair<long long, long long> &a, const pair<long long, long long> &b){
    return a.first > b.first;
}

void solveMin(){
    dsu.make_set(n);
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= n; i++){
        int u = a[i].second;
        long long last = 0, szu = dsu.sz[dsu.find_set(u)];
        for(int j = 0; j < (int) adj[u].size(); j++){
            int v = adj[u][j];
            if(w[v] >= w[u]){
                long long szv = dsu.sz[dsu.find_set(v)];
                if(dsu.union_sets(u, v)){
                    mn += szu * szv * a[i].first;
                    mn += last * szv * a[i].first;
                    last += szv;
                }
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //freopen("marisa.inp", "r", stdin);
    //freopen("marisa.out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i].first;
        a[i].second = i;
        w[i] = a[i].first;
    }
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    solveMax();
    solveMin();

    cout << mx - mn << endl;
}


Comments

Submit
0 Comments
More Questions

979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits