답안 #929056

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
929056 2024-02-17T15:29:32 Z Amirreza_Fakhri Janjetina (COCI21_janjetina) C++17
110 / 110
303 ms 17952 KB
// In the name of the God
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;

const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 1e5+5;

int n, k, sz[maxn], fen[maxn];
bool mark[maxn];
vector <pii> adj[maxn], vec1, vec2;

void dfs1(int v, int p) {
    sz[v] = 1;
    for (pii e : adj[v]) {
        int u = e.F;
        if (!mark[u] and u != p) {
            dfs1(u, v); sz[v] += sz[u];
        }
    }
}

int fcen(int v, int p, int s) {
    for (pii e : adj[v]) {
        int u = e.F;
        if (!mark[u] and u != p and sz[u]*2 > s) return fcen(u, v, s);
    }
    return v;
}

void dfs2(int v, int p, int mx, int h) {
    vec1.pb(mp(mx, h)), vec2.pb(mp(mx, h));
    for (pii e : adj[v]) {
        int u = e.F;
        if (!mark[u] and u != p) dfs2(u, v, max(mx, e.S), h+1);
    }
}

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

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

int relax(vector <pii> &vec) {
    int res = 0;
    sort(all(vec));
    for (pii p : vec) {
        int j = p.F-k-p.S;
        if (j >= 0) res += get(min(j, n-1));
        upd(p.S, 1);
    }
    for (pii p : vec) upd(p.S, -1);
    vec.clear();
    return res;
}

int cen(int v) {
    dfs1(v, v);
    v = fcen(v, v, sz[v]);
    int ans = 0;
    vec2.pb(mp(0, 0));
    for (pii e : adj[v]) {
        int u = e.F;
        if (!mark[u]) {
            dfs2(u, v, e.S, 1);
            ans -= relax(vec1);
        }
    }
    ans += relax(vec2);
    mark[v] = 1;
    for (pii e : adj[v]) {
        int u = e.F;
        if (!mark[u]) ans += cen(u);
    }
    return ans;
}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int v, u, w; cin >> v >> u >> w;
        adj[--v].pb(mp(--u, w));
        adj[u].pb(mp(v, w));
    }
    cout << cen(0)*2 << '\n';
    return 0;
}































# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 3 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 3 ms 2908 KB Output is correct
7 Correct 2 ms 2904 KB Output is correct
8 Correct 2 ms 2904 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
12 Correct 2 ms 2908 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 2 ms 2908 KB Output is correct
15 Correct 1 ms 2908 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2904 KB Output is correct
18 Correct 2 ms 2924 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 2 ms 2920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 17 ms 4444 KB Output is correct
6 Correct 118 ms 10448 KB Output is correct
7 Correct 223 ms 17780 KB Output is correct
8 Correct 269 ms 17772 KB Output is correct
9 Correct 241 ms 17952 KB Output is correct
10 Correct 277 ms 17776 KB Output is correct
11 Correct 227 ms 17776 KB Output is correct
12 Correct 263 ms 17784 KB Output is correct
13 Correct 240 ms 17908 KB Output is correct
14 Correct 283 ms 17776 KB Output is correct
15 Correct 281 ms 17772 KB Output is correct
16 Correct 266 ms 17772 KB Output is correct
17 Correct 284 ms 17708 KB Output is correct
18 Correct 303 ms 17776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 3 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 3 ms 2908 KB Output is correct
7 Correct 2 ms 2904 KB Output is correct
8 Correct 2 ms 2904 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
12 Correct 2 ms 2908 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 2 ms 2908 KB Output is correct
15 Correct 1 ms 2908 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2904 KB Output is correct
18 Correct 2 ms 2924 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 2 ms 2920 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2648 KB Output is correct
26 Correct 2 ms 2908 KB Output is correct
27 Correct 17 ms 4444 KB Output is correct
28 Correct 118 ms 10448 KB Output is correct
29 Correct 223 ms 17780 KB Output is correct
30 Correct 269 ms 17772 KB Output is correct
31 Correct 241 ms 17952 KB Output is correct
32 Correct 277 ms 17776 KB Output is correct
33 Correct 227 ms 17776 KB Output is correct
34 Correct 263 ms 17784 KB Output is correct
35 Correct 240 ms 17908 KB Output is correct
36 Correct 283 ms 17776 KB Output is correct
37 Correct 281 ms 17772 KB Output is correct
38 Correct 266 ms 17772 KB Output is correct
39 Correct 284 ms 17708 KB Output is correct
40 Correct 303 ms 17776 KB Output is correct
41 Correct 1 ms 2648 KB Output is correct
42 Correct 238 ms 17876 KB Output is correct
43 Correct 292 ms 17776 KB Output is correct
44 Correct 235 ms 17772 KB Output is correct
45 Correct 277 ms 17772 KB Output is correct
46 Correct 223 ms 17780 KB Output is correct
47 Correct 279 ms 17784 KB Output is correct
48 Correct 272 ms 17776 KB Output is correct
49 Correct 277 ms 17776 KB Output is correct
50 Correct 295 ms 17796 KB Output is correct
51 Correct 302 ms 17772 KB Output is correct
52 Correct 114 ms 11136 KB Output is correct
53 Correct 119 ms 11312 KB Output is correct
54 Correct 80 ms 11264 KB Output is correct
55 Correct 141 ms 11124 KB Output is correct
56 Correct 114 ms 11252 KB Output is correct
57 Correct 203 ms 11712 KB Output is correct
58 Correct 236 ms 11360 KB Output is correct
59 Correct 213 ms 11708 KB Output is correct
60 Correct 264 ms 11456 KB Output is correct
61 Correct 236 ms 11644 KB Output is correct
62 Correct 176 ms 11456 KB Output is correct
63 Correct 217 ms 11628 KB Output is correct
64 Correct 219 ms 11552 KB Output is correct
65 Correct 7 ms 3280 KB Output is correct
66 Correct 1 ms 2652 KB Output is correct