Submission #368115

# Submission time Handle Problem Language Result Execution time Memory
368115 2021-02-19T14:48:12 Z egekabas Janjetina (COCI21_janjetina) C++14
110 / 110
504 ms 47968 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
int n, k;

int bit[100009];

void upd(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;
}

vector<pii> g[100009];
int subsz[100009];
int block[100009];
void getsz(int v, int prt){
    subsz[v] = 1;
    for(auto u : g[v])
        if(u.ff != prt && !block[u.ff]){
            getsz(u.ff, v);
            subsz[v] += subsz[u.ff];
        }
}
int getcentroid(int v, int prt, int tot){
    for(auto u : g[v])
        if(u.ff != prt && !block[u.ff] && subsz[u.ff]*2 >= tot)
            return getcentroid(u.ff, v, tot);
    return v;
}
vector<pll> child[100009];
void dfs(int v, int prt,int d, int curmax, int top){
    child[top].pb({curmax, d});
    for(auto u : g[v])
        if(u.ff != prt && !block[u.ff])
            dfs(u.ff, v, d+1, max(curmax, u.ss), top);
}
vector<int> rel;
ll mx = 1e5;
ll ans;

void getans(int v){
    getsz(v, 0);
    v = getcentroid(v, 0, subsz[v]);
    vector<pii> vec;
    for(auto u : g[v]){
        if(block[u.ff]) continue;
        dfs(u.ff, v, 1, u.ss, u.ff);
        sort(all(child[u.ff]));

        for(auto u : child[u.ff]){
            if(u.ff-u.ss-k >= 0){
                ans += 2;
            }
            ans -= 2*get(u.ff-u.ss-k);
            upd(u.ss, 1);
        }
        for(auto u : child[u.ff]){
            upd(u.ss, -1);
            vec.pb(u);
        }
    }
    sort(all(vec));
    for(auto u : vec){
        ans += 2*get(u.ff-u.ss-k);
        upd(u.ss, 1);
    }
    for(auto u : vec){
        upd(u.ss, -1);
    }
    for(auto u : g[v])
        child[u.ff].clear();
    
    block[v] = 1;
    for(auto u : g[v])
        if(!block[u.ff])
            getans(u.ff);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    cin >> n >> k;
    for(int i = 0; i < n-1; ++i){
        int x, y, z;
        cin >> x >> y >> z;
        g[x].pb({y, z});
        g[y].pb({x, z});
    }
    getans(1);
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 6 ms 5356 KB Output is correct
5 Correct 6 ms 5356 KB Output is correct
6 Correct 6 ms 5356 KB Output is correct
7 Correct 6 ms 5376 KB Output is correct
8 Correct 6 ms 5356 KB Output is correct
9 Correct 5 ms 5228 KB Output is correct
10 Correct 5 ms 5228 KB Output is correct
11 Correct 5 ms 5228 KB Output is correct
12 Correct 6 ms 5228 KB Output is correct
13 Correct 5 ms 5228 KB Output is correct
14 Correct 7 ms 5228 KB Output is correct
15 Correct 5 ms 5228 KB Output is correct
16 Correct 5 ms 5228 KB Output is correct
17 Correct 5 ms 5228 KB Output is correct
18 Correct 6 ms 5228 KB Output is correct
19 Correct 6 ms 5228 KB Output is correct
20 Correct 5 ms 5228 KB Output is correct
21 Correct 6 ms 5228 KB Output is correct
22 Correct 6 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 6 ms 5356 KB Output is correct
5 Correct 34 ms 9068 KB Output is correct
6 Correct 208 ms 25448 KB Output is correct
7 Correct 405 ms 47604 KB Output is correct
8 Correct 436 ms 47668 KB Output is correct
9 Correct 417 ms 47608 KB Output is correct
10 Correct 440 ms 47712 KB Output is correct
11 Correct 431 ms 47712 KB Output is correct
12 Correct 441 ms 47584 KB Output is correct
13 Correct 406 ms 47668 KB Output is correct
14 Correct 463 ms 47712 KB Output is correct
15 Correct 454 ms 47584 KB Output is correct
16 Correct 487 ms 47756 KB Output is correct
17 Correct 437 ms 47708 KB Output is correct
18 Correct 488 ms 47632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 6 ms 5356 KB Output is correct
5 Correct 6 ms 5356 KB Output is correct
6 Correct 6 ms 5356 KB Output is correct
7 Correct 6 ms 5376 KB Output is correct
8 Correct 6 ms 5356 KB Output is correct
9 Correct 5 ms 5228 KB Output is correct
10 Correct 5 ms 5228 KB Output is correct
11 Correct 5 ms 5228 KB Output is correct
12 Correct 6 ms 5228 KB Output is correct
13 Correct 5 ms 5228 KB Output is correct
14 Correct 7 ms 5228 KB Output is correct
15 Correct 5 ms 5228 KB Output is correct
16 Correct 5 ms 5228 KB Output is correct
17 Correct 5 ms 5228 KB Output is correct
18 Correct 6 ms 5228 KB Output is correct
19 Correct 6 ms 5228 KB Output is correct
20 Correct 5 ms 5228 KB Output is correct
21 Correct 6 ms 5228 KB Output is correct
22 Correct 6 ms 5228 KB Output is correct
23 Correct 4 ms 5100 KB Output is correct
24 Correct 4 ms 5100 KB Output is correct
25 Correct 4 ms 5100 KB Output is correct
26 Correct 6 ms 5356 KB Output is correct
27 Correct 34 ms 9068 KB Output is correct
28 Correct 208 ms 25448 KB Output is correct
29 Correct 405 ms 47604 KB Output is correct
30 Correct 436 ms 47668 KB Output is correct
31 Correct 417 ms 47608 KB Output is correct
32 Correct 440 ms 47712 KB Output is correct
33 Correct 431 ms 47712 KB Output is correct
34 Correct 441 ms 47584 KB Output is correct
35 Correct 406 ms 47668 KB Output is correct
36 Correct 463 ms 47712 KB Output is correct
37 Correct 454 ms 47584 KB Output is correct
38 Correct 487 ms 47756 KB Output is correct
39 Correct 437 ms 47708 KB Output is correct
40 Correct 488 ms 47632 KB Output is correct
41 Correct 3 ms 5100 KB Output is correct
42 Correct 381 ms 47712 KB Output is correct
43 Correct 504 ms 47584 KB Output is correct
44 Correct 394 ms 47584 KB Output is correct
45 Correct 486 ms 47712 KB Output is correct
46 Correct 398 ms 47840 KB Output is correct
47 Correct 464 ms 47688 KB Output is correct
48 Correct 405 ms 47968 KB Output is correct
49 Correct 443 ms 47840 KB Output is correct
50 Correct 451 ms 47712 KB Output is correct
51 Correct 446 ms 47768 KB Output is correct
52 Correct 188 ms 19768 KB Output is correct
53 Correct 196 ms 19808 KB Output is correct
54 Correct 177 ms 19808 KB Output is correct
55 Correct 229 ms 19808 KB Output is correct
56 Correct 197 ms 19936 KB Output is correct
57 Correct 395 ms 28836 KB Output is correct
58 Correct 387 ms 27364 KB Output is correct
59 Correct 399 ms 28904 KB Output is correct
60 Correct 488 ms 28172 KB Output is correct
61 Correct 423 ms 28384 KB Output is correct
62 Correct 353 ms 24544 KB Output is correct
63 Correct 373 ms 24648 KB Output is correct
64 Correct 416 ms 25308 KB Output is correct
65 Correct 16 ms 5868 KB Output is correct
66 Correct 3 ms 5100 KB Output is correct