Submission #486233

# Submission time Handle Problem Language Result Execution time Memory
486233 2021-11-11T02:54:32 Z jalsol Janjetina (COCI21_janjetina) C++17
110 / 110
347 ms 14284 KB
#ifdef LOCAL
#include "local.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#define DB(...)
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif

using namespace std;

static const bool __initialization = []() {
    cin.tie(nullptr)->sync_with_stdio(false);
#define TASK "JANJETINA"
    if (fopen(TASK".inp", "r")) {
        (void)(!freopen(TASK".inp", "r", stdin));
        (void)(!freopen(TASK".out", "w", stdout)); }
    return true;
}();

using ll = long long;
using ld = long double;
#define fi first
#define se second

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)
#define Fe(...) for (auto __VA_ARGS__)

template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); }
template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; }
template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; }

constexpr ld eps = 1e-9;
constexpr int inf = 1e9;
constexpr ll linf = 1e18;

// =============================================================================

constexpr int maxn = 1e5 + 5;

struct Fen {
    int fen[maxn];

    Fen() {
        memset(fen, 0, sizeof(fen));
    }

    void update(int i, int x) {
        for (; i < maxn; i += (i & -i)) {
            fen[i] += x;
        }
    }

    int get(int i) {
        int ret = 0;
        for (; i > 0; i -= (i & -i)) {
            ret += fen[i];
        }
        return ret;
    }
};

struct Link { int v, w; };

int n, k;
vector<Link> g[maxn];

int sz[maxn];
bool rem[maxn];

int getSz(int u, int p = -1) {
    sz[u] = 1;

    Fe (&[v, _] : g[u]) {
        if (!rem[v] && v != p) {
            sz[u] += getSz(v, u);
        }
    }

    return sz[u];
}

int getCen(int target, int u, int p = -1) {
    Fe (&[v, _] : g[u]) {
        if (rem[v] || v == p) continue;

        if (sz[v] * 2 > target) {
            return getCen(target, v, u);
        }
    }

    return u;
}

vector<pair<int, int>> other;
Fen fen;
ll ans = 0;

void findSub(int u, int p, int maxw, int dep) {
    other.emplace_back(maxw, dep);

    Fe (&[v, w] : g[u]) {
        if (!rem[v] && v != p) {
            findSub(v, u, max(maxw, w), dep + 1);
        }
    }
}

ll calc() {
    ll ret = 0;
    sort(all(other));

    // max(w1, w2) - (l1 + l2) >= k

    // max(w1, w2) = w2
    // <=> w2 - l1 - l2 >= k
    // <=> w2 - l2 >= k + l1

    Fe (&[maxw, dep] : other) {
        ret += fen.get(maxw - dep);
        fen.update(dep + k, 1);
    }

    Fe (&[maxw, dep] : other) {
        fen.update(dep + k, -1);
    }

    return ret;
}

void solve(int u) {
    u = getCen(getSz(u), u);
    rem[u] = true;

    other.clear();
    findSub(u, -1, 0, 0);
    ans += calc();

    // only consider paths on different subtrees
    // => eliminate paths on the same subtree
    Fe (&[v, w] : g[u]) {
        if (!rem[v]) {
            other.clear();
            findSub(v, u, w, 1);
            ans -= calc();
        }
    }

    Fe (&[v, _] : g[u]) {
        if (!rem[v]) {
            solve(v);
        }
    }
}

signed main() {
    cin >> n >> k;
    Rep (_, n - 1) {
        int u, v, w; cin >> u >> v >> w;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }

    solve(1);

    cout << ans * 2 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3020 KB Output is correct
2 Correct 1 ms 3020 KB Output is correct
3 Correct 2 ms 3020 KB Output is correct
4 Correct 3 ms 3148 KB Output is correct
5 Correct 4 ms 3148 KB Output is correct
6 Correct 5 ms 3148 KB Output is correct
7 Correct 5 ms 3148 KB Output is correct
8 Correct 4 ms 3148 KB Output is correct
9 Correct 3 ms 3020 KB Output is correct
10 Correct 2 ms 3020 KB Output is correct
11 Correct 3 ms 3020 KB Output is correct
12 Correct 3 ms 3120 KB Output is correct
13 Correct 3 ms 3020 KB Output is correct
14 Correct 3 ms 3020 KB Output is correct
15 Correct 3 ms 3020 KB Output is correct
16 Correct 2 ms 3020 KB Output is correct
17 Correct 3 ms 3020 KB Output is correct
18 Correct 3 ms 3020 KB Output is correct
19 Correct 3 ms 3020 KB Output is correct
20 Correct 3 ms 3020 KB Output is correct
21 Correct 3 ms 3020 KB Output is correct
22 Correct 3 ms 3020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3020 KB Output is correct
2 Correct 1 ms 3020 KB Output is correct
3 Correct 2 ms 3020 KB Output is correct
4 Correct 5 ms 3148 KB Output is correct
5 Correct 25 ms 4096 KB Output is correct
6 Correct 139 ms 8552 KB Output is correct
7 Correct 182 ms 13756 KB Output is correct
8 Correct 340 ms 14236 KB Output is correct
9 Correct 201 ms 13780 KB Output is correct
10 Correct 347 ms 14140 KB Output is correct
11 Correct 275 ms 13692 KB Output is correct
12 Correct 294 ms 14196 KB Output is correct
13 Correct 315 ms 13904 KB Output is correct
14 Correct 338 ms 14148 KB Output is correct
15 Correct 329 ms 13960 KB Output is correct
16 Correct 309 ms 14140 KB Output is correct
17 Correct 299 ms 14140 KB Output is correct
18 Correct 308 ms 14064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3020 KB Output is correct
2 Correct 1 ms 3020 KB Output is correct
3 Correct 2 ms 3020 KB Output is correct
4 Correct 3 ms 3148 KB Output is correct
5 Correct 4 ms 3148 KB Output is correct
6 Correct 5 ms 3148 KB Output is correct
7 Correct 5 ms 3148 KB Output is correct
8 Correct 4 ms 3148 KB Output is correct
9 Correct 3 ms 3020 KB Output is correct
10 Correct 2 ms 3020 KB Output is correct
11 Correct 3 ms 3020 KB Output is correct
12 Correct 3 ms 3120 KB Output is correct
13 Correct 3 ms 3020 KB Output is correct
14 Correct 3 ms 3020 KB Output is correct
15 Correct 3 ms 3020 KB Output is correct
16 Correct 2 ms 3020 KB Output is correct
17 Correct 3 ms 3020 KB Output is correct
18 Correct 3 ms 3020 KB Output is correct
19 Correct 3 ms 3020 KB Output is correct
20 Correct 3 ms 3020 KB Output is correct
21 Correct 3 ms 3020 KB Output is correct
22 Correct 3 ms 3020 KB Output is correct
23 Correct 1 ms 3020 KB Output is correct
24 Correct 1 ms 3020 KB Output is correct
25 Correct 2 ms 3020 KB Output is correct
26 Correct 5 ms 3148 KB Output is correct
27 Correct 25 ms 4096 KB Output is correct
28 Correct 139 ms 8552 KB Output is correct
29 Correct 182 ms 13756 KB Output is correct
30 Correct 340 ms 14236 KB Output is correct
31 Correct 201 ms 13780 KB Output is correct
32 Correct 347 ms 14140 KB Output is correct
33 Correct 275 ms 13692 KB Output is correct
34 Correct 294 ms 14196 KB Output is correct
35 Correct 315 ms 13904 KB Output is correct
36 Correct 338 ms 14148 KB Output is correct
37 Correct 329 ms 13960 KB Output is correct
38 Correct 309 ms 14140 KB Output is correct
39 Correct 299 ms 14140 KB Output is correct
40 Correct 308 ms 14064 KB Output is correct
41 Correct 2 ms 3060 KB Output is correct
42 Correct 183 ms 13756 KB Output is correct
43 Correct 337 ms 14284 KB Output is correct
44 Correct 206 ms 13756 KB Output is correct
45 Correct 346 ms 14152 KB Output is correct
46 Correct 269 ms 13672 KB Output is correct
47 Correct 320 ms 14176 KB Output is correct
48 Correct 311 ms 13916 KB Output is correct
49 Correct 312 ms 14168 KB Output is correct
50 Correct 317 ms 13932 KB Output is correct
51 Correct 311 ms 14148 KB Output is correct
52 Correct 99 ms 10044 KB Output is correct
53 Correct 103 ms 10360 KB Output is correct
54 Correct 76 ms 10024 KB Output is correct
55 Correct 125 ms 10484 KB Output is correct
56 Correct 109 ms 10436 KB Output is correct
57 Correct 241 ms 9752 KB Output is correct
58 Correct 236 ms 10052 KB Output is correct
59 Correct 255 ms 10236 KB Output is correct
60 Correct 265 ms 10052 KB Output is correct
61 Correct 264 ms 10060 KB Output is correct
62 Correct 209 ms 9720 KB Output is correct
63 Correct 202 ms 9996 KB Output is correct
64 Correct 236 ms 9952 KB Output is correct
65 Correct 10 ms 3404 KB Output is correct
66 Correct 2 ms 3020 KB Output is correct