Submission #486222

# Submission time Handle Problem Language Result Execution time Memory
486222 2021-11-11T02:46:16 Z jalsol Janjetina (COCI21_janjetina) C++17
15 / 110
1500 ms 4744 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) {
    int cen = getCen(getSz(u) / 2, u);
    rem[cen] = 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 2 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 84 ms 3224 KB Output is correct
5 Correct 73 ms 3220 KB Output is correct
6 Correct 82 ms 3344 KB Output is correct
7 Correct 79 ms 3148 KB Output is correct
8 Correct 88 ms 3224 KB Output is correct
9 Correct 3 ms 3076 KB Output is correct
10 Correct 2 ms 3068 KB Output is correct
11 Correct 2 ms 3020 KB Output is correct
12 Correct 7 ms 3068 KB Output is correct
13 Correct 6 ms 3076 KB Output is correct
14 Correct 7 ms 3020 KB Output is correct
15 Correct 3 ms 3020 KB Output is correct
16 Correct 3 ms 3020 KB Output is correct
17 Correct 3 ms 3068 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 4 ms 3020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3020 KB Output is correct
2 Correct 2 ms 3056 KB Output is correct
3 Correct 2 ms 3020 KB Output is correct
4 Correct 85 ms 3220 KB Output is correct
5 Execution timed out 1588 ms 4744 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 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 84 ms 3224 KB Output is correct
5 Correct 73 ms 3220 KB Output is correct
6 Correct 82 ms 3344 KB Output is correct
7 Correct 79 ms 3148 KB Output is correct
8 Correct 88 ms 3224 KB Output is correct
9 Correct 3 ms 3076 KB Output is correct
10 Correct 2 ms 3068 KB Output is correct
11 Correct 2 ms 3020 KB Output is correct
12 Correct 7 ms 3068 KB Output is correct
13 Correct 6 ms 3076 KB Output is correct
14 Correct 7 ms 3020 KB Output is correct
15 Correct 3 ms 3020 KB Output is correct
16 Correct 3 ms 3020 KB Output is correct
17 Correct 3 ms 3068 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 4 ms 3020 KB Output is correct
23 Correct 2 ms 3020 KB Output is correct
24 Correct 2 ms 3056 KB Output is correct
25 Correct 2 ms 3020 KB Output is correct
26 Correct 85 ms 3220 KB Output is correct
27 Execution timed out 1588 ms 4744 KB Time limit exceeded
28 Halted 0 ms 0 KB -