답안 #747909

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
747909 2023-05-25T07:31:57 Z FulopMate Janjetina (COCI21_janjetina) C++17
110 / 110
657 ms 19700 KB
#include <bits/stdc++.h>

// #define MULTI_TEST_CASE
// #define TEST_TEXT

using namespace std;

#define ll long long
#define MAX(a, b) (a) = max((a), (b))
#define MIN(a, b) (a) = min((a), (b))
#define all(a) (a).begin(), (a).end()
#define sortedpair(a, b) {min((a), (b)), max((a), (b))}

const ll MOD = 1e9+7;

struct edge {
    int to, w;
};

int n, k;
vector<vector<edge>> v;
ll ans = 0;
vector<bool> voltcentroid;
vector<int> siz;
const int st_size = 200001;
vector<ll> st;

void add(int d, int diff = 1) {
    d += st_size;
    st[d] += diff;
    for (d /= 2; d >= 1; d /= 2) {
        st[d] = st[2*d] + st[2*d+1];
    }
}

ll query(int l, int r) {
    l += st_size; r += st_size;
    ll res = 0;
    while (l <= r) {
        if (l % 2 == 1) res += st[l++];
        if (r % 2 == 0) res += st[r--];
        l /= 2; r /= 2;
    }
    return res;
}

// void add(vector<ll> &st, int d, int dif = 1, int ind = 1, int l = 0, int r = st_size-1){
//     if(l == r){
//         st[ind]+=dif;
//         return;
//     }
//     int mid = (l+r)/2;
//     if(d <= mid) add(st, d, dif, ind*2, l, mid);
//     else add(st, d, dif, ind*2+1, mid+1, r);
//     st[ind] = st[ind*2] + st[ind*2+1];
// }

// ll query(vector<ll> &st, int ql, int qr, int ind = 1, int l = 0, int r = st_size-1){
//     if(qr < ql)return 0;
//     if(r < ql || l > qr)return 0;
//     if(ql <= l && r <= qr) return st[ind];
//     int mid = (l+r)/2;
//     return query(st, ql, qr, ind*2, l, mid) + query(st, ql, qr, ind*2+1, mid+1, r);
// }

int c_szamoz(int x, int p = -1){
    siz[x] = 1;
    for(edge&i : v[x]){
        if(i.to != p && !voltcentroid[i.to])
            siz[x] += c_szamoz(i.to, x);
    }
    return siz[x];
}

int c_keres(int x, int meret, int p = -1){
    for(edge&i : v[x]){
        if(i.to != p && !voltcentroid[i.to]){
            if(siz[i.to] > meret/2){
                return c_keres(i.to, meret, x);
            }
        }
    }
    return x;
}

struct c_ut {
    int w, x, d;
    bool operator<(const c_ut & other) const {
        return -w < -other.w;
    }
};

vector<int> inserts;

void reset_st(){
    while(inserts.size()){
        add(inserts.back(), -1);
        inserts.pop_back();
    }
}

void megold(int x = 0){
    int meret = c_szamoz(x);
    int centroid = c_keres(x, meret);

    // st_ut_max.assign(st_size*2, 0);

    add(0, 1); inserts.push_back(0);

    vector<bool> seen(n, 0);
    seen[centroid] = 1;
    priority_queue<c_ut> q;
    for(edge&i : v[centroid]){
        if(!voltcentroid[i.to]){
            q.push({i.w, i.to, 1});
        }
    }
    while(!q.empty()){
        c_ut c = q.top();
        q.pop();
        seen[c.x] = 1;
        ans += query(0, c.w-c.d-k);
        add(c.d, 1); inserts.push_back(c.d);
        for(edge&i : v[c.x]){
            if(!seen[i.to] && !voltcentroid[i.to]){
                q.push({max(c.w, i.w), i.to, c.d+1});
            }
        }
    }

    reset_st();
    seen.assign(n, 0);
    for(edge&i : v[centroid]){
        if(!voltcentroid[i.to]){
            seen[centroid] = 1;
            priority_queue<c_ut> q;
            q.push({i.w, i.to, 1});
            while(!q.empty()){
                c_ut c = q.top();
                q.pop();
                if(seen[c.x])continue;
                seen[c.x] = 1;
                ans -= query(0, c.w-c.d-k);
                add(c.d, 1); inserts.push_back(c.d);
                for(edge&i : v[c.x]){
                    if(!seen[i.to] && !voltcentroid[i.to]){
                        q.push({max(c.w, i.w), i.to, c.d+1});
                    }
                }
            }
            reset_st();
        }
    }


    // add(st_ut, 0);

    // for(edge&i : v[centroid]){
    //     if(!voltcentroid[i.to]){
    //         c_szamol(i.to, i.w, 1, centroid);
    //         c_frissit(i.to, i.w, 1, centroid);
    //     }
    // }


    voltcentroid[centroid] = true;
    for(edge&i : v[centroid]){
        if(!voltcentroid[i.to]){
            megold(i.to);
        }
    }
}

void solve(){
    cin>>n>>k;
    v.assign(n, {});
    voltcentroid.assign(n, 0);
    st.assign(2*st_size, 0);
    siz.assign(n, 0);
    for(int i = 0; i < n-1; i++){
        int a, b, c; cin>>a>>b>>c; a--; b--;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }
    megold();
    cout<<ans*2<<endl;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int _t = 1;
#ifdef MULTI_TEST_CASE
    cin >> _t;
#endif
    for(int _i = 0; _i < _t; _i++){
        #ifdef TEST_TEXT
        cout<<"Case #"<<_i+1<<": ";
        #endif
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 4 ms 3540 KB Output is correct
5 Correct 4 ms 3608 KB Output is correct
6 Correct 4 ms 3540 KB Output is correct
7 Correct 4 ms 3540 KB Output is correct
8 Correct 4 ms 3540 KB Output is correct
9 Correct 3 ms 3540 KB Output is correct
10 Correct 3 ms 3540 KB Output is correct
11 Correct 3 ms 3540 KB Output is correct
12 Correct 3 ms 3412 KB Output is correct
13 Correct 4 ms 3488 KB Output is correct
14 Correct 4 ms 3540 KB Output is correct
15 Correct 3 ms 3540 KB Output is correct
16 Correct 3 ms 3540 KB Output is correct
17 Correct 4 ms 3412 KB Output is correct
18 Correct 4 ms 3540 KB Output is correct
19 Correct 3 ms 3540 KB Output is correct
20 Correct 3 ms 3412 KB Output is correct
21 Correct 4 ms 3540 KB Output is correct
22 Correct 4 ms 3540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 5 ms 3604 KB Output is correct
5 Correct 33 ms 4820 KB Output is correct
6 Correct 263 ms 10644 KB Output is correct
7 Correct 385 ms 19300 KB Output is correct
8 Correct 544 ms 19700 KB Output is correct
9 Correct 403 ms 19236 KB Output is correct
10 Correct 573 ms 19576 KB Output is correct
11 Correct 390 ms 19152 KB Output is correct
12 Correct 546 ms 19652 KB Output is correct
13 Correct 406 ms 19212 KB Output is correct
14 Correct 580 ms 19536 KB Output is correct
15 Correct 564 ms 19384 KB Output is correct
16 Correct 602 ms 19528 KB Output is correct
17 Correct 651 ms 19528 KB Output is correct
18 Correct 638 ms 19432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 4 ms 3540 KB Output is correct
5 Correct 4 ms 3608 KB Output is correct
6 Correct 4 ms 3540 KB Output is correct
7 Correct 4 ms 3540 KB Output is correct
8 Correct 4 ms 3540 KB Output is correct
9 Correct 3 ms 3540 KB Output is correct
10 Correct 3 ms 3540 KB Output is correct
11 Correct 3 ms 3540 KB Output is correct
12 Correct 3 ms 3412 KB Output is correct
13 Correct 4 ms 3488 KB Output is correct
14 Correct 4 ms 3540 KB Output is correct
15 Correct 3 ms 3540 KB Output is correct
16 Correct 3 ms 3540 KB Output is correct
17 Correct 4 ms 3412 KB Output is correct
18 Correct 4 ms 3540 KB Output is correct
19 Correct 3 ms 3540 KB Output is correct
20 Correct 3 ms 3412 KB Output is correct
21 Correct 4 ms 3540 KB Output is correct
22 Correct 4 ms 3540 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3412 KB Output is correct
25 Correct 2 ms 3412 KB Output is correct
26 Correct 5 ms 3604 KB Output is correct
27 Correct 33 ms 4820 KB Output is correct
28 Correct 263 ms 10644 KB Output is correct
29 Correct 385 ms 19300 KB Output is correct
30 Correct 544 ms 19700 KB Output is correct
31 Correct 403 ms 19236 KB Output is correct
32 Correct 573 ms 19576 KB Output is correct
33 Correct 390 ms 19152 KB Output is correct
34 Correct 546 ms 19652 KB Output is correct
35 Correct 406 ms 19212 KB Output is correct
36 Correct 580 ms 19536 KB Output is correct
37 Correct 564 ms 19384 KB Output is correct
38 Correct 602 ms 19528 KB Output is correct
39 Correct 651 ms 19528 KB Output is correct
40 Correct 638 ms 19432 KB Output is correct
41 Correct 2 ms 3412 KB Output is correct
42 Correct 461 ms 19152 KB Output is correct
43 Correct 657 ms 19640 KB Output is correct
44 Correct 431 ms 19212 KB Output is correct
45 Correct 601 ms 19532 KB Output is correct
46 Correct 418 ms 19168 KB Output is correct
47 Correct 568 ms 19640 KB Output is correct
48 Correct 450 ms 19136 KB Output is correct
49 Correct 627 ms 19520 KB Output is correct
50 Correct 593 ms 19408 KB Output is correct
51 Correct 645 ms 19536 KB Output is correct
52 Correct 223 ms 12532 KB Output is correct
53 Correct 279 ms 12840 KB Output is correct
54 Correct 211 ms 12312 KB Output is correct
55 Correct 400 ms 12792 KB Output is correct
56 Correct 288 ms 12832 KB Output is correct
57 Correct 394 ms 12056 KB Output is correct
58 Correct 469 ms 12360 KB Output is correct
59 Correct 445 ms 12484 KB Output is correct
60 Correct 554 ms 12312 KB Output is correct
61 Correct 559 ms 12456 KB Output is correct
62 Correct 357 ms 12148 KB Output is correct
63 Correct 467 ms 12376 KB Output is correct
64 Correct 498 ms 12372 KB Output is correct
65 Correct 17 ms 3924 KB Output is correct
66 Correct 2 ms 3412 KB Output is correct