답안 #879012

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
879012 2023-11-26T05:39:47 Z _anhxinloi_ Janjetina (COCI21_janjetina) C++14
110 / 110
352 ms 59884 KB
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

const int maxn = 2e5 + 5;
typedef pair<int, int> pii;

int n, k, pa[maxn], child[maxn], ans;
vector<pii> adj[maxn], g[maxn];
int bit[2*maxn];
bool check[maxn];

void update(int idx, int val){
    for(++idx; idx <= 100000; idx += idx&(-idx))
        bit[idx] += val;
}
int get(int idx){
    int ret = 0;
    for(++idx; idx > 0; idx -= idx&(-idx))
        ret += bit[idx];
    return ret;
}

int dfs_sz(int u, int par){
    child[u] = 1;
    for(auto z : adj[u]){
        auto [w, v] = z;
        if(v == par || check[v])
            continue;
        dfs_sz(v, u);
        child[u] += child[v];
    }
    return child[u];
}

//int centroid(int u, int par, int sz){
//    for(auto z : adj[u]){
//        auto [w, v] = z;
//        if(v == par || check[v])
//            continue;
//        if(child[v] * 2 >= sz)
//            return centroid(v, u, sz);
//    }
//    return u;
//}

int centroid(int v, int prt, int tot){
    for(auto u : adj[v])
        if(u.se != prt && !check[u.se] && child[u.se]*2 >= tot)
            return centroid(u.se, v, tot);
    return v;
}

void dfs_work(int u, int par, int val, int dist, int top){
    g[top].push_back(make_pair(val, dist));
    for(auto z : adj[u]){
        auto [w, v] = z;
        if(v == par || check[v])
            continue;
        dfs_work(v, u, max(val, w), dist+1, top);
    }
}

void build(int u, int par){
    int sz = dfs_sz(u, par);
    int center = centroid(u, par, sz);
//    cout << center << endl;
    pa[center] = u;

    vector<pii> vec = {};
    for(auto z : adj[center]){
        auto [w, v] = z;
        if(check[v])
            continue;
        dfs_work(v, center, w, 1, v);
        sort(g[v].begin(), g[v].end());

        for(auto z : g[v]){
            auto [val, dist] = z;
            int cur = val - dist - k;
//            cout << cur << endl;
            if(cur >= 0){
                ++ans;
                ans -= get(cur);
            }
            update(dist, 1);
        }

        for(auto z : g[v]){
            auto [val, dist] = z;
            update(dist, -1);
            vec.push_back(z);
        }
    }

    sort(vec.begin(), vec.end());
    for(auto z : vec){
        auto [val, dist] = z;
        int cur = val - dist - k;
        if(cur >= 0){
            ans += get(cur);
        }
        update(dist, 1);
    }

    for(auto z : vec){
        auto [val, dist] = z;
        update(dist, -1);
    }

    for(auto z : adj[center]){
        auto [w, v] = z;
        g[v].clear();
    }

    check[center] = true;
    for(auto z : adj[center]){
        auto [w, v] = z;
        if(!check[v])
            build(v, center);
    }
}

signed main(){
//    freopen("love.inp" , "r" , stdin);
//    freopen("love.out" , "w" , stdout);

    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    cin >> n >> k;
    for(int i=1 ; i<n ; ++i){
       int u, v, w;
       cin >> u >> v >> w;
       adj[u].push_back({w, v});
       adj[v].push_back({w, u});
    }

    build(1, -1);
    cout << ans * 2;
}

Compilation message

Main.cpp: In function 'long long int dfs_sz(long long int, long long int)':
Main.cpp:30:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   30 |         auto [w, v] = z;
      |              ^
Main.cpp: In function 'void dfs_work(long long int, long long int, long long int, long long int, long long int)':
Main.cpp:60:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   60 |         auto [w, v] = z;
      |              ^
Main.cpp: In function 'void build(long long int, long long int)':
Main.cpp:75:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   75 |         auto [w, v] = z;
      |              ^
Main.cpp:82:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   82 |             auto [val, dist] = z;
      |                  ^
Main.cpp:93:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   93 |             auto [val, dist] = z;
      |                  ^
Main.cpp:101:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  101 |         auto [val, dist] = z;
      |              ^
Main.cpp:110:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  110 |         auto [val, dist] = z;
      |              ^
Main.cpp:115:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  115 |         auto [w, v] = z;
      |              ^
Main.cpp:121:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  121 |         auto [w, v] = z;
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 4 ms 12892 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 4 ms 12892 KB Output is correct
7 Correct 4 ms 15068 KB Output is correct
8 Correct 4 ms 12892 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 4 ms 12888 KB Output is correct
14 Correct 5 ms 12892 KB Output is correct
15 Correct 4 ms 14936 KB Output is correct
16 Correct 5 ms 12892 KB Output is correct
17 Correct 4 ms 14952 KB Output is correct
18 Correct 4 ms 12892 KB Output is correct
19 Correct 4 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 14936 KB Output is correct
22 Correct 4 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 4 ms 12892 KB Output is correct
5 Correct 22 ms 18636 KB Output is correct
6 Correct 153 ms 34660 KB Output is correct
7 Correct 241 ms 59488 KB Output is correct
8 Correct 284 ms 58352 KB Output is correct
9 Correct 257 ms 59680 KB Output is correct
10 Correct 310 ms 58096 KB Output is correct
11 Correct 240 ms 59716 KB Output is correct
12 Correct 277 ms 58140 KB Output is correct
13 Correct 253 ms 59684 KB Output is correct
14 Correct 299 ms 57860 KB Output is correct
15 Correct 300 ms 58048 KB Output is correct
16 Correct 325 ms 59580 KB Output is correct
17 Correct 291 ms 58464 KB Output is correct
18 Correct 288 ms 57812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12632 KB Output is correct
4 Correct 4 ms 12892 KB Output is correct
5 Correct 4 ms 14940 KB Output is correct
6 Correct 4 ms 12892 KB Output is correct
7 Correct 4 ms 15068 KB Output is correct
8 Correct 4 ms 12892 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 4 ms 12888 KB Output is correct
14 Correct 5 ms 12892 KB Output is correct
15 Correct 4 ms 14936 KB Output is correct
16 Correct 5 ms 12892 KB Output is correct
17 Correct 4 ms 14952 KB Output is correct
18 Correct 4 ms 12892 KB Output is correct
19 Correct 4 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 14936 KB Output is correct
22 Correct 4 ms 14940 KB Output is correct
23 Correct 2 ms 12632 KB Output is correct
24 Correct 3 ms 12636 KB Output is correct
25 Correct 2 ms 12632 KB Output is correct
26 Correct 4 ms 12892 KB Output is correct
27 Correct 22 ms 18636 KB Output is correct
28 Correct 153 ms 34660 KB Output is correct
29 Correct 241 ms 59488 KB Output is correct
30 Correct 284 ms 58352 KB Output is correct
31 Correct 257 ms 59680 KB Output is correct
32 Correct 310 ms 58096 KB Output is correct
33 Correct 240 ms 59716 KB Output is correct
34 Correct 277 ms 58140 KB Output is correct
35 Correct 253 ms 59684 KB Output is correct
36 Correct 299 ms 57860 KB Output is correct
37 Correct 300 ms 58048 KB Output is correct
38 Correct 325 ms 59580 KB Output is correct
39 Correct 291 ms 58464 KB Output is correct
40 Correct 288 ms 57812 KB Output is correct
41 Correct 3 ms 12632 KB Output is correct
42 Correct 248 ms 59884 KB Output is correct
43 Correct 273 ms 58232 KB Output is correct
44 Correct 253 ms 59632 KB Output is correct
45 Correct 301 ms 57948 KB Output is correct
46 Correct 240 ms 59588 KB Output is correct
47 Correct 290 ms 58348 KB Output is correct
48 Correct 250 ms 59636 KB Output is correct
49 Correct 291 ms 57924 KB Output is correct
50 Correct 352 ms 59508 KB Output is correct
51 Correct 292 ms 58052 KB Output is correct
52 Correct 104 ms 32204 KB Output is correct
53 Correct 118 ms 30408 KB Output is correct
54 Correct 97 ms 32176 KB Output is correct
55 Correct 122 ms 30144 KB Output is correct
56 Correct 130 ms 32320 KB Output is correct
57 Correct 227 ms 42604 KB Output is correct
58 Correct 228 ms 38664 KB Output is correct
59 Correct 232 ms 40672 KB Output is correct
60 Correct 242 ms 39884 KB Output is correct
61 Correct 263 ms 40244 KB Output is correct
62 Correct 182 ms 37964 KB Output is correct
63 Correct 201 ms 35588 KB Output is correct
64 Correct 266 ms 38616 KB Output is correct
65 Correct 11 ms 13660 KB Output is correct
66 Correct 2 ms 12636 KB Output is correct