Submission #922124

# Submission time Handle Problem Language Result Execution time Memory
922124 2024-02-05T06:00:20 Z 406 Janjetina (COCI21_janjetina) C++17
110 / 110
353 ms 25012 KB
    #include <bits/stdc++.h>
    using namespace std;
    #define FOR(i, a, b) for (int i = (a); i < (b); i++) 
    #define int long long
    using ar = array<int, 2>;
    const int64_t INF = 1ll << 60;
    const int N = 1e5 + 5;
    vector<bool> del(N);
    int sub[N], ptr, M[N], h[N], k, n, bit[N], ans;
    vector<ar> adj[N], VV, V[N];
     
    void add(int r, int d) {
            r++;
            for (; r < N; r += r & -r) bit[r] += d;
    }
    int get(int r) {
            r++;
            int re = 0;
            for (; r > 0; r &= (r - 1)) re += bit[r];
            return re;
    }
     
    void find_sub(int v, int p) {
            sub[v] = 1;
            for (auto P: adj[v]) {
                    int u = P[0];
                    if (del[u] || u == p) continue;
                    find_sub(u, v);
                    sub[v] += sub[u];
            }
    }
    int find_center(int v, int p, int kol) {
            for (auto P: adj[v]) {
                    int u = P[0];
                    if (del[u] || u == p || sub[u] <= (kol / 2)) continue;
                    return find_center(u, v, kol);
            }
            return v;
    }
    void dfs(int v, int p) {
            for (auto P: adj[v]) {
                    int u = P[0], w = P[1];
                    if (u == p || del[u]) continue;
                    M[u] = max(M[v], w);
                    h[u] = h[v] + 1;
                    dfs(u, v);
            }
            V[ptr].push_back({M[v], h[v]});
            VV.push_back({M[v], h[v]});
    }
     
    int solve(vector<ar> &a) {
            sort(a.begin(), a.end());
            int ans = 0;
            for (auto P: a) {
                    int m = P[0], h = P[1];
                    ans += get(m - h - k);
                    add(h, +1);
            }
            for (auto P: a)
                    add(P[1], -1);
            return ans;
    }
     
    void rec(int x) {
            find_sub(x, x);
            int c = find_center(x, x, sub[x]);
            del[c] = true;
            for (auto P: adj[c]) {
                    int u = P[0], w = P[1];
                    if (del[u]) continue;
                    h[u] = 1, M[u] = w;
                    dfs(u, c);
                    ptr++;
            }
            FOR(i, 0, ptr) {
                    ans -= solve(V[i]);
                    V[i].clear();
            }
            VV.push_back({-INF, 0});
            ans += solve(VV);
            VV.clear();
            ptr = 0;
     
            for (auto P: adj[c]) {
                    int u = P[0], w = P[1];
                    if (del[u]) continue;
                    rec(u);
            }
    }
    signed main() {
            ios::sync_with_stdio(0), cin.tie(0);
            cin >> n >> k;
            FOR(i, 1, n) {
                    int u, v, w; 
                    cin >> u >> v >> w;
                    u--, v--;
                    adj[v].push_back({u, w});
                    adj[u].push_back({v, w});
            }
            rec(0);
            cout << 2ll * ans << '\n';
    }
    //20:55:05

Compilation message

Main.cpp: In function 'void rec(long long int)':
Main.cpp:86:35: warning: unused variable 'w' [-Wunused-variable]
   86 |                     int u = P[0], w = P[1];
      |                                   ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 4 ms 5212 KB Output is correct
5 Correct 4 ms 5212 KB Output is correct
6 Correct 4 ms 5188 KB Output is correct
7 Correct 4 ms 5464 KB Output is correct
8 Correct 4 ms 5212 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 5 ms 5212 KB Output is correct
13 Correct 4 ms 5180 KB Output is correct
14 Correct 4 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 4 ms 5212 KB Output is correct
18 Correct 4 ms 5212 KB Output is correct
19 Correct 4 ms 5212 KB Output is correct
20 Correct 4 ms 5212 KB Output is correct
21 Correct 4 ms 5212 KB Output is correct
22 Correct 4 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 3 ms 5216 KB Output is correct
4 Correct 5 ms 5180 KB Output is correct
5 Correct 21 ms 7004 KB Output is correct
6 Correct 142 ms 14984 KB Output is correct
7 Correct 297 ms 24572 KB Output is correct
8 Correct 351 ms 24884 KB Output is correct
9 Correct 295 ms 24488 KB Output is correct
10 Correct 348 ms 24796 KB Output is correct
11 Correct 293 ms 24444 KB Output is correct
12 Correct 344 ms 24984 KB Output is correct
13 Correct 288 ms 24476 KB Output is correct
14 Correct 340 ms 24784 KB Output is correct
15 Correct 328 ms 24504 KB Output is correct
16 Correct 310 ms 25012 KB Output is correct
17 Correct 302 ms 24772 KB Output is correct
18 Correct 306 ms 24692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 4 ms 5212 KB Output is correct
5 Correct 4 ms 5212 KB Output is correct
6 Correct 4 ms 5188 KB Output is correct
7 Correct 4 ms 5464 KB Output is correct
8 Correct 4 ms 5212 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 5 ms 5212 KB Output is correct
13 Correct 4 ms 5180 KB Output is correct
14 Correct 4 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 4 ms 5212 KB Output is correct
18 Correct 4 ms 5212 KB Output is correct
19 Correct 4 ms 5212 KB Output is correct
20 Correct 4 ms 5212 KB Output is correct
21 Correct 4 ms 5212 KB Output is correct
22 Correct 4 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 3 ms 5216 KB Output is correct
26 Correct 5 ms 5180 KB Output is correct
27 Correct 21 ms 7004 KB Output is correct
28 Correct 142 ms 14984 KB Output is correct
29 Correct 297 ms 24572 KB Output is correct
30 Correct 351 ms 24884 KB Output is correct
31 Correct 295 ms 24488 KB Output is correct
32 Correct 348 ms 24796 KB Output is correct
33 Correct 293 ms 24444 KB Output is correct
34 Correct 344 ms 24984 KB Output is correct
35 Correct 288 ms 24476 KB Output is correct
36 Correct 340 ms 24784 KB Output is correct
37 Correct 328 ms 24504 KB Output is correct
38 Correct 310 ms 25012 KB Output is correct
39 Correct 302 ms 24772 KB Output is correct
40 Correct 306 ms 24692 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 297 ms 24392 KB Output is correct
43 Correct 353 ms 24880 KB Output is correct
44 Correct 288 ms 24252 KB Output is correct
45 Correct 348 ms 24764 KB Output is correct
46 Correct 306 ms 24340 KB Output is correct
47 Correct 349 ms 25012 KB Output is correct
48 Correct 298 ms 24488 KB Output is correct
49 Correct 331 ms 24764 KB Output is correct
50 Correct 331 ms 24508 KB Output is correct
51 Correct 302 ms 24764 KB Output is correct
52 Correct 119 ms 17360 KB Output is correct
53 Correct 145 ms 17860 KB Output is correct
54 Correct 99 ms 17360 KB Output is correct
55 Correct 126 ms 17860 KB Output is correct
56 Correct 141 ms 17932 KB Output is correct
57 Correct 260 ms 18720 KB Output is correct
58 Correct 273 ms 18584 KB Output is correct
59 Correct 295 ms 18968 KB Output is correct
60 Correct 285 ms 18352 KB Output is correct
61 Correct 305 ms 18948 KB Output is correct
62 Correct 223 ms 17600 KB Output is correct
63 Correct 238 ms 18504 KB Output is correct
64 Correct 271 ms 18480 KB Output is correct
65 Correct 11 ms 5724 KB Output is correct
66 Correct 2 ms 5212 KB Output is correct