답안 #878995

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878995 2023-11-26T04:25:42 Z _anhxinloi_ Janjetina (COCI21_janjetina) C++14
110 / 110
323 ms 49508 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]);
//    cout << v << endl;
    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 += 1;
                ans -= 1*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){
         if(u.ff-u.ss-k >= 0){
            ans += 1*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 * 2 << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5720 KB Output is correct
2 Correct 1 ms 5720 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 3 ms 6236 KB Output is correct
5 Correct 3 ms 6232 KB Output is correct
6 Correct 3 ms 6236 KB Output is correct
7 Correct 3 ms 6236 KB Output is correct
8 Correct 3 ms 6236 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 6084 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 3 ms 5980 KB Output is correct
13 Correct 3 ms 5976 KB Output is correct
14 Correct 3 ms 6096 KB Output is correct
15 Correct 3 ms 5980 KB Output is correct
16 Correct 3 ms 5980 KB Output is correct
17 Correct 3 ms 5980 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 3 ms 5980 KB Output is correct
20 Correct 3 ms 5980 KB Output is correct
21 Correct 3 ms 5904 KB Output is correct
22 Correct 3 ms 5980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5724 KB Output is correct
2 Correct 2 ms 5724 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 3 ms 6236 KB Output is correct
5 Correct 21 ms 9804 KB Output is correct
6 Correct 136 ms 26592 KB Output is correct
7 Correct 244 ms 48972 KB Output is correct
8 Correct 266 ms 49480 KB Output is correct
9 Correct 258 ms 49060 KB Output is correct
10 Correct 302 ms 49376 KB Output is correct
11 Correct 238 ms 48964 KB Output is correct
12 Correct 269 ms 49408 KB Output is correct
13 Correct 256 ms 49116 KB Output is correct
14 Correct 294 ms 49480 KB Output is correct
15 Correct 323 ms 49224 KB Output is correct
16 Correct 286 ms 49324 KB Output is correct
17 Correct 291 ms 49508 KB Output is correct
18 Correct 299 ms 49476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5720 KB Output is correct
2 Correct 1 ms 5720 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 3 ms 6236 KB Output is correct
5 Correct 3 ms 6232 KB Output is correct
6 Correct 3 ms 6236 KB Output is correct
7 Correct 3 ms 6236 KB Output is correct
8 Correct 3 ms 6236 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 6084 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 3 ms 5980 KB Output is correct
13 Correct 3 ms 5976 KB Output is correct
14 Correct 3 ms 6096 KB Output is correct
15 Correct 3 ms 5980 KB Output is correct
16 Correct 3 ms 5980 KB Output is correct
17 Correct 3 ms 5980 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 3 ms 5980 KB Output is correct
20 Correct 3 ms 5980 KB Output is correct
21 Correct 3 ms 5904 KB Output is correct
22 Correct 3 ms 5980 KB Output is correct
23 Correct 2 ms 5724 KB Output is correct
24 Correct 2 ms 5724 KB Output is correct
25 Correct 2 ms 5980 KB Output is correct
26 Correct 3 ms 6236 KB Output is correct
27 Correct 21 ms 9804 KB Output is correct
28 Correct 136 ms 26592 KB Output is correct
29 Correct 244 ms 48972 KB Output is correct
30 Correct 266 ms 49480 KB Output is correct
31 Correct 258 ms 49060 KB Output is correct
32 Correct 302 ms 49376 KB Output is correct
33 Correct 238 ms 48964 KB Output is correct
34 Correct 269 ms 49408 KB Output is correct
35 Correct 256 ms 49116 KB Output is correct
36 Correct 294 ms 49480 KB Output is correct
37 Correct 323 ms 49224 KB Output is correct
38 Correct 286 ms 49324 KB Output is correct
39 Correct 291 ms 49508 KB Output is correct
40 Correct 299 ms 49476 KB Output is correct
41 Correct 2 ms 5724 KB Output is correct
42 Correct 244 ms 49112 KB Output is correct
43 Correct 273 ms 49392 KB Output is correct
44 Correct 260 ms 49100 KB Output is correct
45 Correct 300 ms 49420 KB Output is correct
46 Correct 240 ms 48860 KB Output is correct
47 Correct 275 ms 49484 KB Output is correct
48 Correct 272 ms 48996 KB Output is correct
49 Correct 293 ms 49484 KB Output is correct
50 Correct 296 ms 49280 KB Output is correct
51 Correct 300 ms 49224 KB Output is correct
52 Correct 96 ms 21140 KB Output is correct
53 Correct 104 ms 21444 KB Output is correct
54 Correct 83 ms 20932 KB Output is correct
55 Correct 116 ms 21320 KB Output is correct
56 Correct 107 ms 21444 KB Output is correct
57 Correct 220 ms 30136 KB Output is correct
58 Correct 223 ms 29060 KB Output is correct
59 Correct 220 ms 30588 KB Output is correct
60 Correct 245 ms 29772 KB Output is correct
61 Correct 245 ms 30220 KB Output is correct
62 Correct 196 ms 26140 KB Output is correct
63 Correct 203 ms 26436 KB Output is correct
64 Correct 225 ms 26952 KB Output is correct
65 Correct 9 ms 6744 KB Output is correct
66 Correct 2 ms 5724 KB Output is correct