답안 #366951

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
366951 2021-02-15T19:43:41 Z alrad Janjetina (COCI21_janjetina) C++17
110 / 110
597 ms 16232 KB
#include <bits/stdc++.h>

using namespace std;

using ld = long double;
using ull = unsigned long long;

/*
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/

template <class T> inline T gcd(T a , T b) { return !a ? b : gcd(b % a , a); }
template <class T> inline T lcm(T a , T b) {return (a * b) / gcd(a , b) ; }

mt19937 rnd(time(0));

#define all(x) x.begin(), x.end()
#define debug(x) { cerr << #x << " = " << x << endl; }

const int N = 1e5 + 2;

int n, k, sub[N], depth[N];
long long fen[N], ans = 0ll;
vector<bool> used(N, false);

vector<pair<int, int>> cnt;
vector<vector<pair<int, int>>> g(N, vector<pair<int, int>>());

void calc(int v, int par = -1) {
  sub[v] = 1;
  if (par != -1) {
    depth[v] = depth[par] + 1;
  } else {
    depth[v] = 0;
  }
  for (auto edge : g[v]) {
    int to = edge.first;
    if (to != par && !used[to]) {
      calc(to, v);
      sub[v] += sub[to];
    }
  }
  return;
}

int getCentroid(int v, int par, int subSize) {
  for (auto edge : g[v]) {
    int to = edge.first;
    if (sub[to] > (subSize / 2) && !used[to] && to != par) {
      return getCentroid(to, v, subSize);
    }
  }
  return v;
}

void dfs1(int v, int par, int maxEdge) {
  cnt.push_back({v, maxEdge});
  for (auto edge : g[v]) {
    int to = edge.first, wei = edge.second;
    if (!used[to] && to != par) {
      dfs1(to, v, max(maxEdge, wei));
    }
  }
  return;
}

bool comp(const pair<int, int> &p1, const pair<int, int> &p2) {
  if (p1.second != p2.second) {
    return p1.second < p2.second;
  }
  return p1.first < p2.first;
}

long long query(int R) {
  long long sum = 0ll;
  R++;
  for (; R >= 0; R = (R & (R + 1)) - 1) {
    sum += fen[R];
  }
  return sum;
}

void upd(int i, long long value) {
  i++;
  for (; i < N - 1; i = (i | (i + 1))) {
    fen[i] += value;
  }
  return;
}

void decompose(int v) {
  calc(v);
  used[v] = true;
  dfs1(v, -1, 0);
  sort(all(cnt), comp);
  for (auto temp : cnt) {
    int ver = temp.first, wei = temp.second;
    ans += query(wei - k - depth[ver]);
    upd(depth[ver], +1);
  }
  for (auto temp : cnt) {
    int ver = temp.first;
    upd(depth[ver], -1);
  }
  cnt.clear();
  for (auto edge : g[v]) {
    int to = edge.first;
    if (!used[to]) {
      dfs1(to, v, edge.second);
      sort(all(cnt), comp);
      for (auto temp : cnt) {
        int ver = temp.first, wei = temp.second;
        ans -= query(wei - k - depth[ver]);
        upd(depth[ver], +1);
      }
      for (auto temp : cnt) {
        int ver = temp.first;
        upd(depth[ver], -1);
      }
      cnt.clear();
    }
  }
  for (auto edge : g[v]) {
    int to = edge.first;
    if (!used[to]) {
      decompose(getCentroid(to, v, sub[to]));
    }
  }
  return;
}

void solve() {
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int u, v, wei;
    cin >> u >> v >> wei;
    g[u].push_back({v, wei});
    g[v].push_back({u, wei});
  }
  decompose(getCentroid(1, -1, n));
  cout << 2ll * ans << '\n';
  return;
}

signed main() {
  ios_base :: sync_with_stdio(0);
  cin.tie(0) , cout.tie(0);
  int t = 1;
  while (t-- > 0) {
    solve();
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 4 ms 2924 KB Output is correct
6 Correct 5 ms 2924 KB Output is correct
7 Correct 5 ms 2924 KB Output is correct
8 Correct 5 ms 2924 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 4 ms 2796 KB Output is correct
13 Correct 4 ms 2796 KB Output is correct
14 Correct 5 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 4 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 4 ms 2796 KB Output is correct
20 Correct 4 ms 2796 KB Output is correct
21 Correct 5 ms 2796 KB Output is correct
22 Correct 4 ms 2796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 40 ms 4204 KB Output is correct
6 Correct 256 ms 9448 KB Output is correct
7 Correct 493 ms 16104 KB Output is correct
8 Correct 529 ms 16104 KB Output is correct
9 Correct 558 ms 16104 KB Output is correct
10 Correct 585 ms 16104 KB Output is correct
11 Correct 468 ms 16104 KB Output is correct
12 Correct 523 ms 16232 KB Output is correct
13 Correct 526 ms 16232 KB Output is correct
14 Correct 576 ms 16232 KB Output is correct
15 Correct 582 ms 16104 KB Output is correct
16 Correct 564 ms 16104 KB Output is correct
17 Correct 575 ms 16100 KB Output is correct
18 Correct 564 ms 16104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 5 ms 2924 KB Output is correct
5 Correct 4 ms 2924 KB Output is correct
6 Correct 5 ms 2924 KB Output is correct
7 Correct 5 ms 2924 KB Output is correct
8 Correct 5 ms 2924 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 4 ms 2796 KB Output is correct
13 Correct 4 ms 2796 KB Output is correct
14 Correct 5 ms 2796 KB Output is correct
15 Correct 4 ms 2796 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 4 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 4 ms 2796 KB Output is correct
20 Correct 4 ms 2796 KB Output is correct
21 Correct 5 ms 2796 KB Output is correct
22 Correct 4 ms 2796 KB Output is correct
23 Correct 2 ms 2796 KB Output is correct
24 Correct 2 ms 2796 KB Output is correct
25 Correct 2 ms 2796 KB Output is correct
26 Correct 5 ms 2924 KB Output is correct
27 Correct 40 ms 4204 KB Output is correct
28 Correct 256 ms 9448 KB Output is correct
29 Correct 493 ms 16104 KB Output is correct
30 Correct 529 ms 16104 KB Output is correct
31 Correct 558 ms 16104 KB Output is correct
32 Correct 585 ms 16104 KB Output is correct
33 Correct 468 ms 16104 KB Output is correct
34 Correct 523 ms 16232 KB Output is correct
35 Correct 526 ms 16232 KB Output is correct
36 Correct 576 ms 16232 KB Output is correct
37 Correct 582 ms 16104 KB Output is correct
38 Correct 564 ms 16104 KB Output is correct
39 Correct 575 ms 16100 KB Output is correct
40 Correct 564 ms 16104 KB Output is correct
41 Correct 2 ms 2796 KB Output is correct
42 Correct 471 ms 16104 KB Output is correct
43 Correct 517 ms 16104 KB Output is correct
44 Correct 549 ms 16104 KB Output is correct
45 Correct 579 ms 16104 KB Output is correct
46 Correct 478 ms 16232 KB Output is correct
47 Correct 551 ms 16104 KB Output is correct
48 Correct 533 ms 16104 KB Output is correct
49 Correct 578 ms 16104 KB Output is correct
50 Correct 593 ms 16104 KB Output is correct
51 Correct 552 ms 16104 KB Output is correct
52 Correct 212 ms 8804 KB Output is correct
53 Correct 232 ms 8932 KB Output is correct
54 Correct 193 ms 8804 KB Output is correct
55 Correct 236 ms 8804 KB Output is correct
56 Correct 219 ms 8932 KB Output is correct
57 Correct 578 ms 8420 KB Output is correct
58 Correct 573 ms 8420 KB Output is correct
59 Correct 572 ms 8420 KB Output is correct
60 Correct 597 ms 8420 KB Output is correct
61 Correct 573 ms 8420 KB Output is correct
62 Correct 416 ms 8420 KB Output is correct
63 Correct 480 ms 8420 KB Output is correct
64 Correct 481 ms 8420 KB Output is correct
65 Correct 15 ms 3052 KB Output is correct
66 Correct 2 ms 2796 KB Output is correct