답안 #787118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
787118 2023-07-18T20:45:38 Z MISM06 Janjetina (COCI21_janjetina) C++14
110 / 110
231 ms 18344 KB
//0 1 1 0 1
//0 1 0 0 1
//1 0 0 1 1
//0 1 1 0 1
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx2")

using namespace std;

#define F 			first
#define S 			second
#define pb 			push_back
#define sze			size()
#define	all(x)		x.begin() , x.end()
#define wall__		cout << "--------------------------------------\n";
#define kids		int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1
#define file_io		freopen("input.cpp", "r", stdin); freopen("output.cpp", "w", stdout);

typedef long long ll;
typedef long double dl;
typedef pair < int , int > pii;
typedef pair < int , ll > pil;
typedef pair < ll , int > pli;
typedef pair < ll , ll > pll;
typedef pair < int , pii > piii;
typedef pair < ll, pll > plll;


const ll N = 2e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = 2e16;
const ll INF = 1e9 + 10;
const ll lg = 32;

int n, k;
vector < pii > g[N], ord;

int sub[N], flag[N], bit[N];
pii h[N];
ll ans = 0;

void pre_calc (int v, int p) {
    sub[v] = 1;
    for (auto e : g[v]) {
        int u = e.F;
        if (flag[u] || u == p) continue;
        pre_calc(u, v);
        sub[v] += sub[u];
    }
}
int find_cen (int v, int p, int s) {
    for (auto e : g[v]) {
        int u = e.F;
        if (u == p || flag[u]) continue;
        if (sub[u] > s / 2) return find_cen(u, v, s);
    }
    return v;
}
void dfs (int v, int p) {
    sub[v] = 1;
    if (p != 0) ord.pb({h[v].S, h[v].F});
    for (auto e : g[v]) {
        int u = e.F;
        if (flag[u] || u == p) continue;
        h[u] = {h[v].F + 1, max(h[v].S, e.S)};
        dfs(u, v);
        sub[v] += sub[u];
    }
}

inline ll read (int idx) {
    ll res = 0;
    for (; idx > 0; idx -= (idx & -idx)) res += bit[idx];
    return res;
}
inline void upd (int idx, int val, int s) {
    for (; idx <= s; idx += (idx & -idx)) bit[idx] += val;
}
ll calc (int root, int t) {
    // cout << root << " , " << t << " : \n";
    ord.clear();
    int pp = 0;
    if (t) pp = -1;
    dfs (root, pp);
    // cout << "s = " << sub[root] << '\n';
    for (int i = 0; i <= sub[root]; i++) bit[i] = 0;
    sort(all(ord));
    ll res = 0;
    for (auto e : ord) {
        // cout << e.F << "*" << e.S << '\n';
        int w = e.F, l = e.S;
        int l2 = w - k - l;
        l2 = min(l2, sub[root]);
        if (l2 >= 0) {
            int x = read(l2);
            // cout << x << '\n';
            res += x;
            if (t == 0) ++res;
        }
        if (l > 0) upd(l, 1, sub[root]);
    }
    // cout << '\n';
    // cout << "res = " << res << '\n';
    return res;
}
void centree (int v) {
    pre_calc(v, 0);
    int cen = find_cen(v, 0, sub[v]);
    h[cen] = {0, 0};
    ans += calc(cen, 0);
    flag[cen] = 1;
    for (auto e : g[cen]) {
        int u = e.F;
        if (flag[u]) continue;
        h[u] = {1, e.S};
        ans -= calc(u, 1);
    }
    for (auto e : g[cen]) {
        int u = e.F;
        if (flag[u]) continue;
        centree(u);
    }
}

void solve () {

	cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int v, u, w; cin >> v >> u >> w;
        g[v].pb({u, w});
        g[u].pb({v, w}); 
    }
    centree(1);
    cout << ans * 2ll << '\n';
}


int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t = 1;
	// cin >> t;
	while (t--) {solve();}
    return 0;
}
/*
3 0
1 2 2
2 3 3
*/
//shrek is love;
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 4 ms 5156 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 4 ms 5036 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5036 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5108 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 16 ms 6356 KB Output is correct
6 Correct 107 ms 11620 KB Output is correct
7 Correct 181 ms 17856 KB Output is correct
8 Correct 195 ms 18236 KB Output is correct
9 Correct 190 ms 17828 KB Output is correct
10 Correct 203 ms 18120 KB Output is correct
11 Correct 182 ms 17816 KB Output is correct
12 Correct 195 ms 18264 KB Output is correct
13 Correct 195 ms 17948 KB Output is correct
14 Correct 202 ms 18240 KB Output is correct
15 Correct 203 ms 18060 KB Output is correct
16 Correct 194 ms 18252 KB Output is correct
17 Correct 192 ms 18164 KB Output is correct
18 Correct 193 ms 18040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 4 ms 5156 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 4 ms 5036 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5040 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5036 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5108 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 2 ms 4948 KB Output is correct
24 Correct 2 ms 4948 KB Output is correct
25 Correct 2 ms 4948 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 16 ms 6356 KB Output is correct
28 Correct 107 ms 11620 KB Output is correct
29 Correct 181 ms 17856 KB Output is correct
30 Correct 195 ms 18236 KB Output is correct
31 Correct 190 ms 17828 KB Output is correct
32 Correct 203 ms 18120 KB Output is correct
33 Correct 182 ms 17816 KB Output is correct
34 Correct 195 ms 18264 KB Output is correct
35 Correct 195 ms 17948 KB Output is correct
36 Correct 202 ms 18240 KB Output is correct
37 Correct 203 ms 18060 KB Output is correct
38 Correct 194 ms 18252 KB Output is correct
39 Correct 192 ms 18164 KB Output is correct
40 Correct 193 ms 18040 KB Output is correct
41 Correct 2 ms 5032 KB Output is correct
42 Correct 187 ms 17848 KB Output is correct
43 Correct 205 ms 18344 KB Output is correct
44 Correct 190 ms 17820 KB Output is correct
45 Correct 231 ms 18180 KB Output is correct
46 Correct 181 ms 17852 KB Output is correct
47 Correct 198 ms 18240 KB Output is correct
48 Correct 226 ms 17836 KB Output is correct
49 Correct 204 ms 18136 KB Output is correct
50 Correct 202 ms 17968 KB Output is correct
51 Correct 193 ms 18180 KB Output is correct
52 Correct 77 ms 13388 KB Output is correct
53 Correct 80 ms 13668 KB Output is correct
54 Correct 77 ms 13252 KB Output is correct
55 Correct 84 ms 13632 KB Output is correct
56 Correct 88 ms 13636 KB Output is correct
57 Correct 178 ms 13012 KB Output is correct
58 Correct 182 ms 13368 KB Output is correct
59 Correct 165 ms 13464 KB Output is correct
60 Correct 186 ms 13348 KB Output is correct
61 Correct 206 ms 13312 KB Output is correct
62 Correct 147 ms 12992 KB Output is correct
63 Correct 156 ms 13256 KB Output is correct
64 Correct 173 ms 13344 KB Output is correct
65 Correct 8 ms 5460 KB Output is correct
66 Correct 2 ms 5032 KB Output is correct