Submission #977501

# Submission time Handle Problem Language Result Execution time Memory
977501 2024-05-08T05:42:49 Z tnknguyen_ Janjetina (COCI21_janjetina) C++14
110 / 110
381 ms 16740 KB
/**
 * @file main.cpp
 * @author tnknguyen_ ([email protected])
 * @date 08-05-2024
 * 
 * @copyright Copyright (c) 2024
 * https://oj.vnoi.info/problem/coci2021_r4_janjetina
 */
#include <bits/stdc++.h>
using namespace std;
#define endl '\n' 
#define ll long long 
#define len(s) (int)s.size() 
#define pii pair<int, int>
//#define int long long 

struct BIT{
    int n;
    vector<ll> f;

    BIT(int N = 1e5): n(N+5), f(N+5, 0) {};

    void update(int i, int val){
        ++i;
        while(i < n){
            f[i] += val;
            i += (i & -i);
        }
    }

    ll get(int i){
        ll ans = 0;
        ++i;
        while(i > 0){
            ans += f[i];
            i -= (i & -i);
        }
        return ans;
    }

    ll get(int l, int r){
        return get(r) - get(l-1);
    }
};

const int MX = 1e5 + 5;
vector<pii> gr[MX];
int del[MX], sz[MX];
int n, k;
BIT bit;
ll ans = 0;

int dfs(int u, int p){
    sz[u] = 1;
    for(pii e : gr[u]){
        int v = e.first;
        if(v != p && !del[v]){
            sz[u] += dfs(v, u);
        }
    }
    return sz[u];
}

int centroid(int u, int p, const int &n){
    for(pii e : gr[u]){
        int v = e.first;
        if(v != p && !del[v] && sz[v]*2 > n){
            return centroid(v, u, n);
        }
    }
    return u;
}

vector<pii> vals;
void calc(int u, int p, int len, int _max){ // Calculates vals[].
    vals.push_back({_max, len});
    for(pii e : gr[u]){
        int v, c;
        tie(v, c) = e;
        if(v != p && !del[v]){
            calc(v, u, len + 1, max(_max, c));
        }
    }
}

void solve(const int &op){ //update answer
    sort(vals.begin(), vals.end());

    for(pii p : vals){
        int w, l;
        tie(w, l) = p;
        int need = w - l - k;
        ans += (bit.get(need) * op);
        bit.update(l, 1);
    }

    for(pii p : vals){
        bit.update(p.second, -1);
    }
}

void decompose(int u){ // Centroid Decomposition
    dfs(u, 0);
    u = centroid(u, 0, sz[u]);
    del[u] = 1;

    vals.clear();
    calc(u, 0, 0, 0);
    solve(1);

    for(pii e : gr[u]){
        int v, c;
        tie(v, c) = e;
        if(!del[v]){
            vals.clear();
            calc(v, 0, 1, c);
            solve(-1);
        }
    }

    for(pii e : gr[u]){
        int v = e.first;
        if(!del[v]){
            decompose(v);
        }
    }
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

//    freopen("main.inp","r",stdin);
//    freopen("main.out","w",stdout);

    cin>>n>>k;

    for(int i=1; i<n; ++i){
        int u, v, w;
        cin>>u>>v>>w;
        gr[u].push_back({v, w});
        gr[v].push_back({u, w});
    }

    decompose(1);
    cout<<ans*2;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4188 KB Output is correct
2 Correct 2 ms 4024 KB Output is correct
3 Correct 1 ms 4184 KB Output is correct
4 Correct 3 ms 4440 KB Output is correct
5 Correct 3 ms 4188 KB Output is correct
6 Correct 3 ms 4188 KB Output is correct
7 Correct 3 ms 4188 KB Output is correct
8 Correct 4 ms 4188 KB Output is correct
9 Correct 3 ms 4184 KB Output is correct
10 Correct 2 ms 4188 KB Output is correct
11 Correct 3 ms 4188 KB Output is correct
12 Correct 3 ms 4188 KB Output is correct
13 Correct 3 ms 4188 KB Output is correct
14 Correct 3 ms 4184 KB Output is correct
15 Correct 3 ms 4188 KB Output is correct
16 Correct 2 ms 4188 KB Output is correct
17 Correct 3 ms 4188 KB Output is correct
18 Correct 3 ms 4188 KB Output is correct
19 Correct 3 ms 4188 KB Output is correct
20 Correct 2 ms 4188 KB Output is correct
21 Correct 3 ms 4188 KB Output is correct
22 Correct 2 ms 4184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4188 KB Output is correct
2 Correct 1 ms 4184 KB Output is correct
3 Correct 1 ms 4188 KB Output is correct
4 Correct 3 ms 4188 KB Output is correct
5 Correct 20 ms 5396 KB Output is correct
6 Correct 139 ms 10460 KB Output is correct
7 Correct 245 ms 16264 KB Output is correct
8 Correct 320 ms 16596 KB Output is correct
9 Correct 261 ms 16228 KB Output is correct
10 Correct 326 ms 16596 KB Output is correct
11 Correct 247 ms 16072 KB Output is correct
12 Correct 324 ms 16584 KB Output is correct
13 Correct 258 ms 16332 KB Output is correct
14 Correct 308 ms 16584 KB Output is correct
15 Correct 355 ms 16332 KB Output is correct
16 Correct 314 ms 16472 KB Output is correct
17 Correct 317 ms 16588 KB Output is correct
18 Correct 317 ms 16588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4188 KB Output is correct
2 Correct 2 ms 4024 KB Output is correct
3 Correct 1 ms 4184 KB Output is correct
4 Correct 3 ms 4440 KB Output is correct
5 Correct 3 ms 4188 KB Output is correct
6 Correct 3 ms 4188 KB Output is correct
7 Correct 3 ms 4188 KB Output is correct
8 Correct 4 ms 4188 KB Output is correct
9 Correct 3 ms 4184 KB Output is correct
10 Correct 2 ms 4188 KB Output is correct
11 Correct 3 ms 4188 KB Output is correct
12 Correct 3 ms 4188 KB Output is correct
13 Correct 3 ms 4188 KB Output is correct
14 Correct 3 ms 4184 KB Output is correct
15 Correct 3 ms 4188 KB Output is correct
16 Correct 2 ms 4188 KB Output is correct
17 Correct 3 ms 4188 KB Output is correct
18 Correct 3 ms 4188 KB Output is correct
19 Correct 3 ms 4188 KB Output is correct
20 Correct 2 ms 4188 KB Output is correct
21 Correct 3 ms 4188 KB Output is correct
22 Correct 2 ms 4184 KB Output is correct
23 Correct 1 ms 4188 KB Output is correct
24 Correct 1 ms 4184 KB Output is correct
25 Correct 1 ms 4188 KB Output is correct
26 Correct 3 ms 4188 KB Output is correct
27 Correct 20 ms 5396 KB Output is correct
28 Correct 139 ms 10460 KB Output is correct
29 Correct 245 ms 16264 KB Output is correct
30 Correct 320 ms 16596 KB Output is correct
31 Correct 261 ms 16228 KB Output is correct
32 Correct 326 ms 16596 KB Output is correct
33 Correct 247 ms 16072 KB Output is correct
34 Correct 324 ms 16584 KB Output is correct
35 Correct 258 ms 16332 KB Output is correct
36 Correct 308 ms 16584 KB Output is correct
37 Correct 355 ms 16332 KB Output is correct
38 Correct 314 ms 16472 KB Output is correct
39 Correct 317 ms 16588 KB Output is correct
40 Correct 317 ms 16588 KB Output is correct
41 Correct 1 ms 4184 KB Output is correct
42 Correct 256 ms 16020 KB Output is correct
43 Correct 321 ms 16596 KB Output is correct
44 Correct 261 ms 16148 KB Output is correct
45 Correct 330 ms 16584 KB Output is correct
46 Correct 247 ms 16076 KB Output is correct
47 Correct 381 ms 16740 KB Output is correct
48 Correct 266 ms 16332 KB Output is correct
49 Correct 300 ms 16592 KB Output is correct
50 Correct 350 ms 16332 KB Output is correct
51 Correct 318 ms 16592 KB Output is correct
52 Correct 100 ms 11216 KB Output is correct
53 Correct 139 ms 11212 KB Output is correct
54 Correct 86 ms 10740 KB Output is correct
55 Correct 136 ms 11212 KB Output is correct
56 Correct 132 ms 11304 KB Output is correct
57 Correct 232 ms 10736 KB Output is correct
58 Correct 290 ms 10960 KB Output is correct
59 Correct 276 ms 10960 KB Output is correct
60 Correct 288 ms 10960 KB Output is correct
61 Correct 283 ms 10964 KB Output is correct
62 Correct 191 ms 10440 KB Output is correct
63 Correct 242 ms 10952 KB Output is correct
64 Correct 269 ms 10956 KB Output is correct
65 Correct 18 ms 4440 KB Output is correct
66 Correct 1 ms 4184 KB Output is correct