Submission #480273

# Submission time Handle Problem Language Result Execution time Memory
480273 2021-10-15T12:41:29 Z Jarif_Rahman Janjetina (COCI21_janjetina) C++17
110 / 110
390 ms 17544 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

struct BIT{
    int n;
    vector<ll> sm;
    BIT(int _n){
        n = _n;
        sm.resize(n);
    }
    void add(int in, int x){
        in++;
        while(in <= n) sm[in-1]+=x, in+=in&-in;
    }
    ll sum(int in){
        in++;
        ll s = 0;
        while(in >= 1) s+=sm[in-1], in-=in&-in;
        return s;
    }
    ll sum(int l, int r){
        return sum(r)-(l == 0? 0:sum(l-1));
    }
};

int n, k;
vector<vector<pair<int, int>>> v;
vector<int> sz;

void dfs0(int nd, int ss){
    for(auto [x, w]: v[nd]) if(x != ss) dfs0(x, nd);
    for(auto [x, w]: v[nd]) if(x != ss) sz[nd]+=sz[x];
}

int dfs1(int nd, int ss, int sz0){
    for(auto [x, w]: v[nd]) if(x != ss){
        int rt = dfs1(x, nd, sz0);
        if(rt != -1) return rt;
    }

    bool bl = 1;
    for(auto [x, w]: v[nd]) if(x != ss) if(sz[x] > sz0/2) bl = 0;
    if(sz0-sz[nd] > sz0/2) bl = 0;
    
    return bl ? nd : -1;
}

vector<pair<int, int>> sth;

ll cnt(){
    sort(sth.rbegin(), sth.rend());
    BIT bit(sth.size()+3);
    ll cur = 0;
    for(int i = (int)sth.size() - 1; i >= 0; i--){
        int lim = sth[i].f-sth[i].sc-k;
        lim = min(lim, (int)sth.size() + 2);
        if(lim >= 0) cur+=bit.sum(0, lim);
        bit.add(sth[i].sc, 1);
    }
    return cur;
}

void dfs2(int nd, int ss, int dis, int mx){
    sth.pb({mx, dis});
    for(auto [x, w]: v[nd]) if(x != ss) dfs2(x, nd, dis+1, max(mx, w));
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    v.resize(n);
    for(int i = 0; i < n-1; i++){
        int a, b, w; cin >> a >> b >> w; a--, b--;
        v[a].pb({b, w});
        v[b].pb({a, w});
    }

    stack<int> st;
    st.push(0);

    ll ans = 0;

    while(!st.empty()){
        stack<int> st0;
        sz.assign(n, 1);
        while(!st.empty()){
            int nd = st.top();
            st.pop();
            sth.clear();
            dfs2(nd, -1, 0, 0);
            ans+=cnt();
            for(auto [x, w]: v[nd]){
                sth.clear();
                dfs2(x, nd, 1, w);
                ans-=cnt();
            }

            for(auto [x, w]: v[nd]){
                v[x].erase(find(v[x].begin(), v[x].end(), make_pair(nd, w)));
                dfs0(x, -1);
                st0.push(dfs1(x, -1, sz[x]));
            }
        }
        swap(st, st0);
    }

    ans*=2LL;

    cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 3 ms 332 KB Output is correct
13 Correct 2 ms 316 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 ms 412 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 22 ms 2052 KB Output is correct
6 Correct 142 ms 8932 KB Output is correct
7 Correct 288 ms 16960 KB Output is correct
8 Correct 317 ms 17416 KB Output is correct
9 Correct 359 ms 17080 KB Output is correct
10 Correct 337 ms 17380 KB Output is correct
11 Correct 305 ms 16952 KB Output is correct
12 Correct 308 ms 17464 KB Output is correct
13 Correct 318 ms 17096 KB Output is correct
14 Correct 334 ms 17400 KB Output is correct
15 Correct 337 ms 17208 KB Output is correct
16 Correct 312 ms 17412 KB Output is correct
17 Correct 316 ms 17372 KB Output is correct
18 Correct 309 ms 17336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 3 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 3 ms 332 KB Output is correct
13 Correct 2 ms 316 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 ms 412 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 22 ms 2052 KB Output is correct
28 Correct 142 ms 8932 KB Output is correct
29 Correct 288 ms 16960 KB Output is correct
30 Correct 317 ms 17416 KB Output is correct
31 Correct 359 ms 17080 KB Output is correct
32 Correct 337 ms 17380 KB Output is correct
33 Correct 305 ms 16952 KB Output is correct
34 Correct 308 ms 17464 KB Output is correct
35 Correct 318 ms 17096 KB Output is correct
36 Correct 334 ms 17400 KB Output is correct
37 Correct 337 ms 17208 KB Output is correct
38 Correct 312 ms 17412 KB Output is correct
39 Correct 316 ms 17372 KB Output is correct
40 Correct 309 ms 17336 KB Output is correct
41 Correct 0 ms 312 KB Output is correct
42 Correct 297 ms 16992 KB Output is correct
43 Correct 318 ms 17456 KB Output is correct
44 Correct 324 ms 17084 KB Output is correct
45 Correct 339 ms 17428 KB Output is correct
46 Correct 294 ms 16952 KB Output is correct
47 Correct 320 ms 17544 KB Output is correct
48 Correct 316 ms 17084 KB Output is correct
49 Correct 334 ms 17336 KB Output is correct
50 Correct 333 ms 17212 KB Output is correct
51 Correct 311 ms 17336 KB Output is correct
52 Correct 131 ms 10180 KB Output is correct
53 Correct 162 ms 10552 KB Output is correct
54 Correct 133 ms 10132 KB Output is correct
55 Correct 141 ms 10552 KB Output is correct
56 Correct 153 ms 10504 KB Output is correct
57 Correct 365 ms 9908 KB Output is correct
58 Correct 366 ms 10304 KB Output is correct
59 Correct 342 ms 10296 KB Output is correct
60 Correct 376 ms 10172 KB Output is correct
61 Correct 390 ms 10240 KB Output is correct
62 Correct 316 ms 9872 KB Output is correct
63 Correct 318 ms 10196 KB Output is correct
64 Correct 347 ms 10120 KB Output is correct
65 Correct 9 ms 716 KB Output is correct
66 Correct 0 ms 204 KB Output is correct