답안 #1042963

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1042963 2024-08-03T15:56:30 Z juicy Unique Cities (JOI19_ho_t5) C++17
100 / 100
526 ms 41524 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 2e5 + 5;

int n, m;
int c[N], dep[N], res[N], sf[N], mx[N], l[N], lst[N], s[4 * N], lz[4 * N], cnt[4 * N];
vector<int> g[N];

void pul(int id) {
  s[id] = min(s[id * 2], s[id * 2 + 1]);
  cnt[id] = 0;
  for (auto i : {id * 2, id * 2 + 1}) {
    if (s[id] == s[i]) {
      cnt[id] += cnt[i];
    }
  }
}

void bld(int id = 1, int l = 1, int r = n) {
  lz[id] = 0;
  if (l == r) {
    s[id] = 1;
    cnt[id] = 1;
    return;
  }
  int md = (l + r) / 2;
  bld(id * 2, l, md);
  bld(id * 2 + 1, md + 1, r);
  pul(id); 
}

void app(int id, int x) {
  s[id] += x;
  lz[id] += x;
}

void psh(int id) {
  if (lz[id]) {
    app(id * 2, lz[id]);
    app(id * 2 + 1, lz[id]);
    lz[id] = 0;
  }
}

void upd(int u, int v, int x, int id = 1, int l = 1, int r = n) {
  if (u <= l && r <= v) {
    app(id, x);
    return;
  }
  psh(id);
  int md = (l + r) / 2;
  if (u <= md) {
    upd(u, v, x, id * 2, l, md);
  }
  if (md < v) {
    upd(u, v, x, id * 2 + 1, md + 1, r);
  }
  pul(id);
}

int qry(int i, int id = 1, int l = 1, int r = n) {
  if (l == r) {
    return s[id];
  }
  psh(id);
  int md = (l + r) / 2;
  return i <= md ? qry(i, id * 2, l, md) : qry(i, id * 2 + 1, md + 1, r);
}

void dfs(int u, int p) {
  mx[u] = u;
  for (int v : g[u]) {
    if (v != p) {
      dep[v] = dep[u] + 1;
      dfs(v, u);
      if (dep[mx[v]] > dep[mx[u]]) {
        mx[u] = mx[v];
      }
    }
  }
}

void DFS(int u, int p) {
  int prv = -1;
  if (l[u] && dep[u] > 2) {
    upd(max(1, dep[u] - l[u]), dep[u] - 2, 1);
  }
  if (u ^ p) {
    if (!lst[c[p]] || qry(dep[lst[c[p]]])) {
      prv = lst[c[p]];
      lst[c[p]] = p;
      upd(dep[p], dep[p], -1);
    }
  }
  int h = dep[mx[u]] - dep[u];
  upd(max(1, dep[u] - h), dep[u], 1); 
  if (s[1] == 0) {
    res[u] = max(res[u], cnt[1]);
  }
  upd(max(1, dep[u] - h), dep[u], -1);
  {
    int k = g[u].size();
    sf[k] = 0;
    for (int i = k - 1; i >= 0; --i) {
      sf[i] = sf[i + 1];
      int v = g[u][i];
      if (v != p) {
        sf[i] = max(sf[i], dep[mx[v]] - dep[u] + 1);
      }
    }
    int pf = 0;
    for (int i = 0; i < k; ++i) {
      int v = g[u][i];
      if (v != p) {
        l[v] = max(pf, sf[i + 1]);
        pf = max(pf, dep[mx[v]] - dep[u] + 1);
      }
    }
    for (int v : g[u]) {
      if (v != p) {
        DFS(v, u);
      }
    }
  }
  if (~prv) {
    lst[c[p]] = prv;
    upd(dep[p], dep[p], 1);
  }
  if (l[u] && dep[u] > 2) {
    upd(max(1, dep[u] - l[u]), dep[u] - 2, -1);
  }
  l[u] = 0;
}

void solve(int u) {
  dep[u] = 1;
  dfs(u, u);
  bld();
  DFS(u, u);
}

int findbest(int r) {
  dep[r] = 0;
  dfs(r, r);
  return mx[r];
}

array<int, 2> diam() {
  int u = findbest(1);
  return {u, findbest(u)};
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n >> m;
  for (int i = 1; i < n; ++i) {
    int u, v; cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }  
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
  }
  auto [a, b] = diam();
  solve(a);
  solve(b);
  for (int i = 1; i <= n; ++i) {
    cout << res[i] << "\n";
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 4 ms 5392 KB Output is correct
3 Correct 4 ms 5212 KB Output is correct
4 Correct 4 ms 5212 KB Output is correct
5 Correct 4 ms 5208 KB Output is correct
6 Correct 4 ms 5468 KB Output is correct
7 Correct 4 ms 5212 KB Output is correct
8 Correct 4 ms 5252 KB Output is correct
9 Correct 5 ms 5212 KB Output is correct
10 Correct 5 ms 5404 KB Output is correct
11 Correct 4 ms 5212 KB Output is correct
12 Correct 3 ms 5176 KB Output is correct
13 Correct 4 ms 5720 KB Output is correct
14 Correct 4 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 4 ms 5468 KB Output is correct
18 Correct 4 ms 5252 KB Output is correct
19 Correct 5 ms 5212 KB Output is correct
20 Correct 5 ms 5468 KB Output is correct
21 Correct 4 ms 5472 KB Output is correct
22 Correct 4 ms 5212 KB Output is correct
23 Correct 6 ms 5212 KB Output is correct
24 Correct 4 ms 5212 KB Output is correct
25 Correct 4 ms 5180 KB Output is correct
26 Correct 4 ms 5212 KB Output is correct
27 Correct 4 ms 5476 KB Output is correct
28 Correct 5 ms 5472 KB Output is correct
29 Correct 5 ms 5260 KB Output is correct
30 Correct 4 ms 5208 KB Output is correct
31 Correct 4 ms 5468 KB Output is correct
32 Correct 4 ms 5260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 178 ms 15452 KB Output is correct
2 Correct 168 ms 24736 KB Output is correct
3 Correct 43 ms 8792 KB Output is correct
4 Correct 333 ms 24872 KB Output is correct
5 Correct 398 ms 40564 KB Output is correct
6 Correct 333 ms 32540 KB Output is correct
7 Correct 325 ms 24996 KB Output is correct
8 Correct 406 ms 26452 KB Output is correct
9 Correct 391 ms 25940 KB Output is correct
10 Correct 340 ms 25888 KB Output is correct
11 Correct 310 ms 26308 KB Output is correct
12 Correct 455 ms 34388 KB Output is correct
13 Correct 382 ms 32716 KB Output is correct
14 Correct 349 ms 32204 KB Output is correct
15 Correct 318 ms 26052 KB Output is correct
16 Correct 323 ms 35648 KB Output is correct
17 Correct 339 ms 32992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 299 ms 22100 KB Output is correct
2 Correct 401 ms 40016 KB Output is correct
3 Correct 55 ms 9552 KB Output is correct
4 Correct 411 ms 24656 KB Output is correct
5 Correct 448 ms 41136 KB Output is correct
6 Correct 410 ms 32792 KB Output is correct
7 Correct 403 ms 24916 KB Output is correct
8 Correct 408 ms 28096 KB Output is correct
9 Correct 426 ms 26960 KB Output is correct
10 Correct 465 ms 25948 KB Output is correct
11 Correct 350 ms 25420 KB Output is correct
12 Correct 454 ms 37972 KB Output is correct
13 Correct 458 ms 32060 KB Output is correct
14 Correct 450 ms 31952 KB Output is correct
15 Correct 305 ms 25724 KB Output is correct
16 Correct 398 ms 36020 KB Output is correct
17 Correct 415 ms 33040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 4 ms 5392 KB Output is correct
3 Correct 4 ms 5212 KB Output is correct
4 Correct 4 ms 5212 KB Output is correct
5 Correct 4 ms 5208 KB Output is correct
6 Correct 4 ms 5468 KB Output is correct
7 Correct 4 ms 5212 KB Output is correct
8 Correct 4 ms 5252 KB Output is correct
9 Correct 5 ms 5212 KB Output is correct
10 Correct 5 ms 5404 KB Output is correct
11 Correct 4 ms 5212 KB Output is correct
12 Correct 3 ms 5176 KB Output is correct
13 Correct 4 ms 5720 KB Output is correct
14 Correct 4 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 4 ms 5468 KB Output is correct
18 Correct 4 ms 5252 KB Output is correct
19 Correct 5 ms 5212 KB Output is correct
20 Correct 5 ms 5468 KB Output is correct
21 Correct 4 ms 5472 KB Output is correct
22 Correct 4 ms 5212 KB Output is correct
23 Correct 6 ms 5212 KB Output is correct
24 Correct 4 ms 5212 KB Output is correct
25 Correct 4 ms 5180 KB Output is correct
26 Correct 4 ms 5212 KB Output is correct
27 Correct 4 ms 5476 KB Output is correct
28 Correct 5 ms 5472 KB Output is correct
29 Correct 5 ms 5260 KB Output is correct
30 Correct 4 ms 5208 KB Output is correct
31 Correct 4 ms 5468 KB Output is correct
32 Correct 4 ms 5260 KB Output is correct
33 Correct 178 ms 15452 KB Output is correct
34 Correct 168 ms 24736 KB Output is correct
35 Correct 43 ms 8792 KB Output is correct
36 Correct 333 ms 24872 KB Output is correct
37 Correct 398 ms 40564 KB Output is correct
38 Correct 333 ms 32540 KB Output is correct
39 Correct 325 ms 24996 KB Output is correct
40 Correct 406 ms 26452 KB Output is correct
41 Correct 391 ms 25940 KB Output is correct
42 Correct 340 ms 25888 KB Output is correct
43 Correct 310 ms 26308 KB Output is correct
44 Correct 455 ms 34388 KB Output is correct
45 Correct 382 ms 32716 KB Output is correct
46 Correct 349 ms 32204 KB Output is correct
47 Correct 318 ms 26052 KB Output is correct
48 Correct 323 ms 35648 KB Output is correct
49 Correct 339 ms 32992 KB Output is correct
50 Correct 299 ms 22100 KB Output is correct
51 Correct 401 ms 40016 KB Output is correct
52 Correct 55 ms 9552 KB Output is correct
53 Correct 411 ms 24656 KB Output is correct
54 Correct 448 ms 41136 KB Output is correct
55 Correct 410 ms 32792 KB Output is correct
56 Correct 403 ms 24916 KB Output is correct
57 Correct 408 ms 28096 KB Output is correct
58 Correct 426 ms 26960 KB Output is correct
59 Correct 465 ms 25948 KB Output is correct
60 Correct 350 ms 25420 KB Output is correct
61 Correct 454 ms 37972 KB Output is correct
62 Correct 458 ms 32060 KB Output is correct
63 Correct 450 ms 31952 KB Output is correct
64 Correct 305 ms 25724 KB Output is correct
65 Correct 398 ms 36020 KB Output is correct
66 Correct 415 ms 33040 KB Output is correct
67 Correct 42 ms 7772 KB Output is correct
68 Correct 143 ms 21332 KB Output is correct
69 Correct 223 ms 21880 KB Output is correct
70 Correct 389 ms 24920 KB Output is correct
71 Correct 394 ms 40232 KB Output is correct
72 Correct 356 ms 32592 KB Output is correct
73 Correct 350 ms 25064 KB Output is correct
74 Correct 372 ms 27796 KB Output is correct
75 Correct 359 ms 26192 KB Output is correct
76 Correct 402 ms 26004 KB Output is correct
77 Correct 324 ms 25572 KB Output is correct
78 Correct 408 ms 36148 KB Output is correct
79 Correct 352 ms 34512 KB Output is correct
80 Correct 408 ms 31184 KB Output is correct
81 Correct 320 ms 26056 KB Output is correct
82 Correct 347 ms 35616 KB Output is correct
83 Correct 374 ms 32876 KB Output is correct
84 Correct 418 ms 25168 KB Output is correct
85 Correct 360 ms 41040 KB Output is correct
86 Correct 370 ms 33128 KB Output is correct
87 Correct 360 ms 25168 KB Output is correct
88 Correct 384 ms 28496 KB Output is correct
89 Correct 407 ms 26964 KB Output is correct
90 Correct 411 ms 26704 KB Output is correct
91 Correct 324 ms 26060 KB Output is correct
92 Correct 359 ms 40620 KB Output is correct
93 Correct 393 ms 30920 KB Output is correct
94 Correct 421 ms 29292 KB Output is correct
95 Correct 336 ms 26564 KB Output is correct
96 Correct 412 ms 36192 KB Output is correct
97 Correct 348 ms 33492 KB Output is correct
98 Correct 423 ms 26396 KB Output is correct
99 Correct 353 ms 41524 KB Output is correct
100 Correct 419 ms 34128 KB Output is correct
101 Correct 358 ms 26060 KB Output is correct
102 Correct 413 ms 28496 KB Output is correct
103 Correct 414 ms 27480 KB Output is correct
104 Correct 404 ms 26964 KB Output is correct
105 Correct 371 ms 26304 KB Output is correct
106 Correct 526 ms 35116 KB Output is correct
107 Correct 413 ms 35544 KB Output is correct
108 Correct 457 ms 31440 KB Output is correct
109 Correct 306 ms 27076 KB Output is correct
110 Correct 380 ms 37100 KB Output is correct
111 Correct 431 ms 34396 KB Output is correct