답안 #587714

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
587714 2022-07-02T09:17:47 Z MilosMilutinovic Janjetina (COCI21_janjetina) C++14
110 / 110
279 ms 19964 KB
/**
 *    author:  wxhtzdy
 *    created: 02.07.2022 11:01:49
**/
#include <bits/stdc++.h>

using namespace std;
             
template <typename T>
class fenwick {
  public:
  vector<T> fenw;
  int n;
  fenwick(int _n) : n(_n) {
    fenw.resize(n);
  }
  void modify(int x, T v) {
    while (x < n) {
      fenw[x] += v;
      x |= (x + 1);
    }
  }
  T get(int x) {
    T v{};
    while (x >= 0) {
      v += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
};

long long Calc(vector<pair<int, int>> v, int k) {
  int n = (int) v.size();
  sort(v.begin(), v.end());
  fenwick<int> fenw(n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int d = min(v[i].first - v[i].second - k, n - 1);
    ans += fenw.get(d);
    fenw.modify(v[i].second, +1);
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, k;
  cin >> n >> k;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    --u; --v;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  long long ans = 0;
  vector<int> sz(n);
  vector<bool> was(n);
  function<void(int, int)> Dfs = [&](int v, int pr) {
    sz[v] = 1;
    for (auto& e : g[v]) {
      int u = e.first;
      if (was[u] || u == pr) {
        continue;
      }
      Dfs(u, v);
      sz[v] += sz[u];
    }
  };
  function<int(int, int, int)> Find = [&](int v, int pr, int n) {
    for (auto& e : g[v]) {
      int u = e.first;
      if (was[u] || u == pr || sz[u] * 2 < n) {
        continue;
      }
      return Find(u, v, n);
    }
    return v;
  };                                          
  vector<pair<int, int>> vec;
  function<void(int, int, int, int)> Go = [&](int v, int pr, int mx, int len) {
    vec.emplace_back(mx, len);
    for (auto& e : g[v]) {
      int u = e.first;
      int w = e.second;
      if (!was[u] && u != pr) {
        Go(u, v, max(mx, w), len + 1);
      }
    }
  };
  function<void(int)> Solve = [&](int v) {
    Dfs(v, v);
    v = Find(v, v, sz[v]);
    was[v] = true;
    vector<pair<int, int>> f;
    f.emplace_back(0, 0);                        
    for (auto& e : g[v]) {
      int u = e.first;
      int w = e.second;
      if (!was[u]) {
        Go(u, v, w, 1);  
        for (auto& p : vec) {
          f.push_back(p);
        }
        ans -= Calc(vec, k);
        vec.clear();
      }
    }
    ans += Calc(f, k);
    for (auto& e : g[v]) {
      int u = e.first;
      if (!was[u]) {
        Solve(u);
      }
    }
  };    
  Solve(0);
  cout << ans * 2 << '\n';                                            
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 456 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 324 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 18 ms 2260 KB Output is correct
6 Correct 108 ms 10012 KB Output is correct
7 Correct 262 ms 19408 KB Output is correct
8 Correct 252 ms 19912 KB Output is correct
9 Correct 257 ms 19588 KB Output is correct
10 Correct 261 ms 19864 KB Output is correct
11 Correct 258 ms 19388 KB Output is correct
12 Correct 266 ms 19944 KB Output is correct
13 Correct 251 ms 19512 KB Output is correct
14 Correct 267 ms 19880 KB Output is correct
15 Correct 259 ms 19716 KB Output is correct
16 Correct 236 ms 19816 KB Output is correct
17 Correct 247 ms 19792 KB Output is correct
18 Correct 233 ms 19768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 456 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 324 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 18 ms 2260 KB Output is correct
28 Correct 108 ms 10012 KB Output is correct
29 Correct 262 ms 19408 KB Output is correct
30 Correct 252 ms 19912 KB Output is correct
31 Correct 257 ms 19588 KB Output is correct
32 Correct 261 ms 19864 KB Output is correct
33 Correct 258 ms 19388 KB Output is correct
34 Correct 266 ms 19944 KB Output is correct
35 Correct 251 ms 19512 KB Output is correct
36 Correct 267 ms 19880 KB Output is correct
37 Correct 259 ms 19716 KB Output is correct
38 Correct 236 ms 19816 KB Output is correct
39 Correct 247 ms 19792 KB Output is correct
40 Correct 233 ms 19768 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 255 ms 19376 KB Output is correct
43 Correct 271 ms 19964 KB Output is correct
44 Correct 257 ms 19516 KB Output is correct
45 Correct 257 ms 19876 KB Output is correct
46 Correct 241 ms 19484 KB Output is correct
47 Correct 279 ms 19896 KB Output is correct
48 Correct 247 ms 19516 KB Output is correct
49 Correct 268 ms 19772 KB Output is correct
50 Correct 251 ms 19664 KB Output is correct
51 Correct 241 ms 19772 KB Output is correct
52 Correct 118 ms 10656 KB Output is correct
53 Correct 138 ms 11032 KB Output is correct
54 Correct 90 ms 10624 KB Output is correct
55 Correct 120 ms 10948 KB Output is correct
56 Correct 123 ms 10856 KB Output is correct
57 Correct 248 ms 10696 KB Output is correct
58 Correct 216 ms 10796 KB Output is correct
59 Correct 216 ms 11164 KB Output is correct
60 Correct 261 ms 11008 KB Output is correct
61 Correct 241 ms 10956 KB Output is correct
62 Correct 224 ms 10440 KB Output is correct
63 Correct 276 ms 10760 KB Output is correct
64 Correct 226 ms 11032 KB Output is correct
65 Correct 8 ms 852 KB Output is correct
66 Correct 0 ms 320 KB Output is correct