Submission #381946

# Submission time Handle Problem Language Result Execution time Memory
381946 2021-03-26T07:29:36 Z VEGAnn Janjetina (COCI21_janjetina) C++14
110 / 110
1010 ms 21540 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) ((int)x.size())
#define PB push_back
#define i2 array<int,2>
#define i3 array<int,3>
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
const int MX = int(1e7) + 10;
const int oo = 2e9;
const int N = 100100;
vector<i2> g[N], vc;
ordered_set<i2> st;
int n, k, siz[N], tt = 0;
bool mrk_cd[N];
ll ans;

void build_sz(int v, int p){
    siz[v] = 1;

    for (i2 U : g[v]){
        int u = U[0];

        if (u == p || mrk_cd[u]) continue;

        build_sz(u, v);

        siz[v] += siz[u];
    }
}

int search(int v, int p, int SZ){
    for (i2 U : g[v]){
        int u = U[0];

        if (u == p || mrk_cd[u]) continue;

        if (siz[u] > SZ / 2)
            return search(u, v, SZ);
    }

    return v;
}


void add(int v, int p, int mx, int len){
    vc.PB({mx, len});

    for (i2 u : g[v]){
        if (u[0] == p || mrk_cd[u[0]]) continue;

        add(u[0], v, max(mx, u[1]), len + 1);
    }
}

void build_cd(int v, int p){
    build_sz(v, -1);

    v = search(v, -1, siz[v]);

    mrk_cd[v] = 1;

    vc.clear();

    vc.PB({0, 0});

    for (i2 U : g[v]){
        int u = U[0];

        if (u == p || mrk_cd[u]) continue;

        add(u, v, U[1], 1);
    }

    sort(all(vc));
    st.clear();

    for (i2 cr : vc){
        int bd = cr[0] - k - cr[1];

        ans += st.order_of_key({bd, oo});

        st.insert({cr[1], ++tt});
    }

    for (i2 U : g[v]){
        int u = U[0];

        if (u == p || mrk_cd[u]) continue;

        vc.clear();

        add(u, v, U[1], 1);

        sort(all(vc));

        st.clear();

        for (i2 cr : vc){
            int bd = cr[0] - k - cr[1];

            ans -= st.order_of_key({bd, oo});

            st.insert({cr[1], ++tt});
        }
    }

    for (i2 U : g[v]){
        int u = U[0];

        if (u == p || mrk_cd[u]) continue;

        build_cd(u, v);
    }
}

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

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> n >> k;

    for (int i = 1; i < n; i++){
        int x, y, w; cin >> x >> y >> w;

        x--; y--;

        g[x].PB({y, w});
        g[y].PB({x, w});
    }

    build_cd(0, -1);

    cout << ans * 2;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 3 ms 2668 KB Output is correct
4 Correct 6 ms 2924 KB Output is correct
5 Correct 6 ms 2924 KB Output is correct
6 Correct 6 ms 2924 KB Output is correct
7 Correct 6 ms 2924 KB Output is correct
8 Correct 7 ms 2924 KB Output is correct
9 Correct 4 ms 2796 KB Output is correct
10 Correct 4 ms 2796 KB Output is correct
11 Correct 4 ms 2796 KB Output is correct
12 Correct 5 ms 2796 KB Output is correct
13 Correct 7 ms 2940 KB Output is correct
14 Correct 6 ms 2796 KB Output is correct
15 Correct 5 ms 2796 KB Output is correct
16 Correct 5 ms 2796 KB Output is correct
17 Correct 5 ms 2796 KB Output is correct
18 Correct 5 ms 2796 KB Output is correct
19 Correct 5 ms 2796 KB Output is correct
20 Correct 5 ms 2796 KB Output is correct
21 Correct 5 ms 2796 KB Output is correct
22 Correct 5 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 3 ms 2668 KB Output is correct
3 Correct 3 ms 2668 KB Output is correct
4 Correct 6 ms 2924 KB Output is correct
5 Correct 57 ms 4588 KB Output is correct
6 Correct 410 ms 12136 KB Output is correct
7 Correct 908 ms 21420 KB Output is correct
8 Correct 1010 ms 21344 KB Output is correct
9 Correct 901 ms 21472 KB Output is correct
10 Correct 991 ms 21472 KB Output is correct
11 Correct 909 ms 21472 KB Output is correct
12 Correct 1010 ms 21344 KB Output is correct
13 Correct 898 ms 21216 KB Output is correct
14 Correct 988 ms 21344 KB Output is correct
15 Correct 992 ms 21540 KB Output is correct
16 Correct 926 ms 21472 KB Output is correct
17 Correct 923 ms 21216 KB Output is correct
18 Correct 927 ms 21472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 3 ms 2668 KB Output is correct
4 Correct 6 ms 2924 KB Output is correct
5 Correct 6 ms 2924 KB Output is correct
6 Correct 6 ms 2924 KB Output is correct
7 Correct 6 ms 2924 KB Output is correct
8 Correct 7 ms 2924 KB Output is correct
9 Correct 4 ms 2796 KB Output is correct
10 Correct 4 ms 2796 KB Output is correct
11 Correct 4 ms 2796 KB Output is correct
12 Correct 5 ms 2796 KB Output is correct
13 Correct 7 ms 2940 KB Output is correct
14 Correct 6 ms 2796 KB Output is correct
15 Correct 5 ms 2796 KB Output is correct
16 Correct 5 ms 2796 KB Output is correct
17 Correct 5 ms 2796 KB Output is correct
18 Correct 5 ms 2796 KB Output is correct
19 Correct 5 ms 2796 KB Output is correct
20 Correct 5 ms 2796 KB Output is correct
21 Correct 5 ms 2796 KB Output is correct
22 Correct 5 ms 2796 KB Output is correct
23 Correct 2 ms 2668 KB Output is correct
24 Correct 3 ms 2668 KB Output is correct
25 Correct 3 ms 2668 KB Output is correct
26 Correct 6 ms 2924 KB Output is correct
27 Correct 57 ms 4588 KB Output is correct
28 Correct 410 ms 12136 KB Output is correct
29 Correct 908 ms 21420 KB Output is correct
30 Correct 1010 ms 21344 KB Output is correct
31 Correct 901 ms 21472 KB Output is correct
32 Correct 991 ms 21472 KB Output is correct
33 Correct 909 ms 21472 KB Output is correct
34 Correct 1010 ms 21344 KB Output is correct
35 Correct 898 ms 21216 KB Output is correct
36 Correct 988 ms 21344 KB Output is correct
37 Correct 992 ms 21540 KB Output is correct
38 Correct 926 ms 21472 KB Output is correct
39 Correct 923 ms 21216 KB Output is correct
40 Correct 927 ms 21472 KB Output is correct
41 Correct 2 ms 2668 KB Output is correct
42 Correct 910 ms 21464 KB Output is correct
43 Correct 1008 ms 21216 KB Output is correct
44 Correct 903 ms 21216 KB Output is correct
45 Correct 1004 ms 21320 KB Output is correct
46 Correct 911 ms 21216 KB Output is correct
47 Correct 1006 ms 21472 KB Output is correct
48 Correct 919 ms 21472 KB Output is correct
49 Correct 1006 ms 21472 KB Output is correct
50 Correct 994 ms 21140 KB Output is correct
51 Correct 948 ms 21216 KB Output is correct
52 Correct 297 ms 14432 KB Output is correct
53 Correct 323 ms 14432 KB Output is correct
54 Correct 296 ms 14432 KB Output is correct
55 Correct 325 ms 14432 KB Output is correct
56 Correct 324 ms 14560 KB Output is correct
57 Correct 797 ms 14056 KB Output is correct
58 Correct 772 ms 14056 KB Output is correct
59 Correct 914 ms 13928 KB Output is correct
60 Correct 911 ms 13956 KB Output is correct
61 Correct 957 ms 13936 KB Output is correct
62 Correct 716 ms 13920 KB Output is correct
63 Correct 747 ms 13920 KB Output is correct
64 Correct 967 ms 14048 KB Output is correct
65 Correct 22 ms 3308 KB Output is correct
66 Correct 3 ms 2668 KB Output is correct