Submission #381914

# Submission time Handle Problem Language Result Execution time Memory
381914 2021-03-26T06:46:58 Z kartel Janjetina (COCI21_janjetina) C++14
110 / 110
445 ms 17920 KB
#include <bits/stdc++.h>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
//#include <time.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
//#define M ll(1e9 + 7)
#define M ll(998244353)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e18)
#define el '\n'
#define pii pair <int, int>
#define all(x) (x).begin(), (x).end()
#define arr_all(x, n) (x + 1), (x + 1 + n)
#define vi vector<int>
#define eps (ld)1e-9
using namespace std;
typedef long long ll;
//using namespace __gnu_pbds;
//typedef tree <ll, null_type, less_equal <ll> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef double ld;
typedef unsigned long long ull;
typedef short int si;

const int N = 1e5 + 500;

vector <pii> g[N], vec, vc;

int siz[N], to[N], cntall, n, k, root, f[N];
ll ans;

void upd(int v, int val) {for (; v < N; v = (v | (v + 1))) f[v] += val;}
int get(int v) {int res = 0; for (; v >= 0; v = (v & (v + 1)) - 1) res += f[v]; return res;}

void calc_sizes(int v, int pr) {
    siz[v] = 1;

    for (auto u : g[v]) {
        if (to[u.F] || u.F == pr) {
            continue;
        }

        calc_sizes(u.F, v);
        siz[v] += siz[u.F];
    }
}

int fnd_cent(int v, int pr) {
    int cent = -1;

    for (auto u : g[v]) {
        if (to[u.F] || u.F == pr) {
            continue;
        }

        if (siz[u.F] > siz[root] / 2 && (cent == -1 || siz[cent] < siz[u.F])) {
            cent = u.F;
        }
    }

    if (cent == -1) {
        return v;
    }
    else {
        return fnd_cent(cent, v);
    }
}

void calc(int v, int pr, int mx) {
    if (mx > k) {
        ans += cntall + 1;
    }

    for (auto u : g[v]) {
        if (to[u.F] || u.F == pr) {
            continue;
        }

        calc(u.F, v, max(mx, u.S));
    }
}

void add(int v, int pr, int mx, int l) {
    vc.pb({mx, l});
    vec.pb({mx, l});

    for (auto u : g[v]) {
        if (to[u.F] || u.F == pr) {
            continue;
        }

        add(u.F, v, max(mx, u.S), l + 1);
    }
}

void go(int v, int pr) {
    root = v;
    calc_sizes(v, pr);

    int cent = fnd_cent(v, pr);
    to[cent] = 1;

    vec.clear();

    cntall = 0;
    for (auto u : g[cent]) {
        if (to[u.F]) {
            continue;
        }

        vc.clear();

        add(u.F, cent, u.S, 1);

        sort(all(vc));

        for (auto x : vc) {
            int mx = x.F;
            int l = x.S;

            if (mx - l - k >= 0) {
                ans -= get(mx - l - k);
            }
             upd(l, 1);
        }

        for (auto x : vc) {
            upd(x.S, -1);
        }
    }

    sort(all(vec));

    for (int i = 0; i < sz(vec); i++) {
        int w1 = vec[i].F;
        int l1 = vec[i].S;

        if (w1 - l1 >= k) {
            ans++;
        }

        if (w1 - l1 - k >= 0) {
            ans += get(w1 - l1 - k);
        }

        upd(l1, 1);
    }
    for (int i = 0; i < sz(vec); i++) {
        upd(vec[i].S, -1);
    }

    for (auto u : g[cent]) {
        if (to[u.F]) {
            continue;
        }

        go(u.F, v);
    }
}

int main()
{
//    mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());;
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//	in("toys.in");
//	out("toys.out");
//    in("input.txt");
//    out("output.txt");
//    cerr.precision(9); cerr << fixed;

//    clock_t tStart = clock();

    cin >> n >> k;

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

        g[v].pb({u, w});
        g[u].pb({v, w});
    }

    go(1, -1);

    cout << ans * 2ll;
}


/*

7
4 6 7 2 3 1 5
*/
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Correct 3 ms 2796 KB Output is correct
3 Correct 3 ms 2796 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 5 ms 2924 KB Output is correct
6 Correct 5 ms 2924 KB Output is correct
7 Correct 5 ms 2924 KB Output is correct
8 Correct 5 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 4 ms 2796 KB Output is correct
13 Correct 4 ms 2796 KB Output is correct
14 Correct 4 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 4 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 4 ms 2796 KB Output is correct
20 Correct 4 ms 2796 KB Output is correct
21 Correct 5 ms 2796 KB Output is correct
22 Correct 4 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 4 ms 2796 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 29 ms 4204 KB Output is correct
6 Correct 190 ms 9492 KB Output is correct
7 Correct 337 ms 16092 KB Output is correct
8 Correct 386 ms 16092 KB Output is correct
9 Correct 349 ms 16092 KB Output is correct
10 Correct 418 ms 16092 KB Output is correct
11 Correct 344 ms 16096 KB Output is correct
12 Correct 434 ms 16004 KB Output is correct
13 Correct 370 ms 16068 KB Output is correct
14 Correct 398 ms 15988 KB Output is correct
15 Correct 430 ms 15980 KB Output is correct
16 Correct 426 ms 16092 KB Output is correct
17 Correct 445 ms 16076 KB Output is correct
18 Correct 401 ms 15868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Correct 3 ms 2796 KB Output is correct
3 Correct 3 ms 2796 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 5 ms 2924 KB Output is correct
6 Correct 5 ms 2924 KB Output is correct
7 Correct 5 ms 2924 KB Output is correct
8 Correct 5 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 4 ms 2796 KB Output is correct
13 Correct 4 ms 2796 KB Output is correct
14 Correct 4 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 4 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 4 ms 2796 KB Output is correct
20 Correct 4 ms 2796 KB Output is correct
21 Correct 5 ms 2796 KB Output is correct
22 Correct 4 ms 2796 KB Output is correct
23 Correct 2 ms 2668 KB Output is correct
24 Correct 2 ms 2796 KB Output is correct
25 Correct 4 ms 2796 KB Output is correct
26 Correct 5 ms 2924 KB Output is correct
27 Correct 29 ms 4204 KB Output is correct
28 Correct 190 ms 9492 KB Output is correct
29 Correct 337 ms 16092 KB Output is correct
30 Correct 386 ms 16092 KB Output is correct
31 Correct 349 ms 16092 KB Output is correct
32 Correct 418 ms 16092 KB Output is correct
33 Correct 344 ms 16096 KB Output is correct
34 Correct 434 ms 16004 KB Output is correct
35 Correct 370 ms 16068 KB Output is correct
36 Correct 398 ms 15988 KB Output is correct
37 Correct 430 ms 15980 KB Output is correct
38 Correct 426 ms 16092 KB Output is correct
39 Correct 445 ms 16076 KB Output is correct
40 Correct 401 ms 15868 KB Output is correct
41 Correct 2 ms 2764 KB Output is correct
42 Correct 338 ms 17444 KB Output is correct
43 Correct 407 ms 17884 KB Output is correct
44 Correct 387 ms 17500 KB Output is correct
45 Correct 410 ms 17852 KB Output is correct
46 Correct 345 ms 17492 KB Output is correct
47 Correct 417 ms 17920 KB Output is correct
48 Correct 387 ms 17524 KB Output is correct
49 Correct 400 ms 17884 KB Output is correct
50 Correct 413 ms 17656 KB Output is correct
51 Correct 418 ms 17756 KB Output is correct
52 Correct 179 ms 10212 KB Output is correct
53 Correct 194 ms 10680 KB Output is correct
54 Correct 139 ms 10020 KB Output is correct
55 Correct 190 ms 10608 KB Output is correct
56 Correct 184 ms 10432 KB Output is correct
57 Correct 382 ms 10076 KB Output is correct
58 Correct 379 ms 10084 KB Output is correct
59 Correct 353 ms 10460 KB Output is correct
60 Correct 370 ms 10332 KB Output is correct
61 Correct 378 ms 10332 KB Output is correct
62 Correct 309 ms 9948 KB Output is correct
63 Correct 322 ms 10332 KB Output is correct
64 Correct 362 ms 10332 KB Output is correct
65 Correct 13 ms 3180 KB Output is correct
66 Correct 2 ms 2796 KB Output is correct