답안 #1042974

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1042974 2024-08-03T16:26:15 Z juicy Unique Cities (JOI19_ho_t5) C++17
100 / 100
238 ms 41652 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], pos[N];
vector<int> g[N];

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];
      }
    }
  }
}

int top;
int st[N];
vector<array<int, 2>> his;

void pop(int k) {
  int l = 1, r = top, p = 0;
  while (l <= r) {
    int md = (l + r) / 2;
    if (dep[st[md]] < k) {
      p = md;
      l = md + 1;
    } else {
      r = md - 1;
    }
  }
  his.push_back({top, st[p]});
  top = p;
}

void push(int k) {
  his.push_back({top, st[top + 1]});
  st[++top] = k;
  pos[k] = top;
}

void roll() {
  assert(his.size());
  auto [a, b] = his.back(); his.pop_back();
  st[top] = b;
  top = a;
}

bool check(int k) {
  return pos[k] <= top && st[pos[k]] == k;
}

void DFS(int u, int p) {
  int prv = -1;
  pop(dep[u] - l[u]);
  if (u ^ p) {
    if (!lst[c[p]] || !check(lst[c[p]])) {
      prv = lst[c[p]];
      lst[c[p]] = p;
      push(p);
    }
  }
  int h = dep[mx[u]] - dep[u];
  pop(dep[u] - h);
  res[u] = max(res[u], top);
  roll();
  {
    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;
    roll();
  }
  roll();
  l[u] = 0;
}

void solve(int u) {
  dep[u] = 1;
  dfs(u, u);
  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 1 ms 6488 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6704 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 1 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 1 ms 6748 KB Output is correct
11 Correct 2 ms 6704 KB Output is correct
12 Correct 2 ms 5724 KB Output is correct
13 Correct 2 ms 5980 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 2 ms 5784 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 3 ms 5436 KB Output is correct
18 Correct 2 ms 5436 KB Output is correct
19 Correct 2 ms 5724 KB Output is correct
20 Correct 3 ms 6036 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 3 ms 5856 KB Output is correct
26 Correct 3 ms 5724 KB Output is correct
27 Correct 3 ms 5472 KB Output is correct
28 Correct 3 ms 5848 KB Output is correct
29 Correct 3 ms 5804 KB Output is correct
30 Correct 2 ms 5724 KB Output is correct
31 Correct 3 ms 5492 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 13400 KB Output is correct
2 Correct 70 ms 24308 KB Output is correct
3 Correct 16 ms 9824 KB Output is correct
4 Correct 103 ms 20228 KB Output is correct
5 Correct 238 ms 37316 KB Output is correct
6 Correct 116 ms 28620 KB Output is correct
7 Correct 97 ms 20176 KB Output is correct
8 Correct 100 ms 22000 KB Output is correct
9 Correct 101 ms 21332 KB Output is correct
10 Correct 110 ms 21452 KB Output is correct
11 Correct 70 ms 21448 KB Output is correct
12 Correct 120 ms 30916 KB Output is correct
13 Correct 109 ms 28756 KB Output is correct
14 Correct 114 ms 28216 KB Output is correct
15 Correct 64 ms 21396 KB Output is correct
16 Correct 106 ms 32716 KB Output is correct
17 Correct 110 ms 29132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 18516 KB Output is correct
2 Correct 182 ms 40120 KB Output is correct
3 Correct 18 ms 10452 KB Output is correct
4 Correct 105 ms 21656 KB Output is correct
5 Correct 168 ms 41652 KB Output is correct
6 Correct 148 ms 31684 KB Output is correct
7 Correct 98 ms 22608 KB Output is correct
8 Correct 122 ms 25808 KB Output is correct
9 Correct 116 ms 24660 KB Output is correct
10 Correct 119 ms 23456 KB Output is correct
11 Correct 120 ms 22492 KB Output is correct
12 Correct 163 ms 38112 KB Output is correct
13 Correct 137 ms 30408 KB Output is correct
14 Correct 153 ms 30788 KB Output is correct
15 Correct 65 ms 22464 KB Output is correct
16 Correct 138 ms 36672 KB Output is correct
17 Correct 128 ms 31944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6704 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 1 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 1 ms 6748 KB Output is correct
11 Correct 2 ms 6704 KB Output is correct
12 Correct 2 ms 5724 KB Output is correct
13 Correct 2 ms 5980 KB Output is correct
14 Correct 3 ms 5212 KB Output is correct
15 Correct 2 ms 5784 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 3 ms 5436 KB Output is correct
18 Correct 2 ms 5436 KB Output is correct
19 Correct 2 ms 5724 KB Output is correct
20 Correct 3 ms 6036 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 3 ms 5856 KB Output is correct
26 Correct 3 ms 5724 KB Output is correct
27 Correct 3 ms 5472 KB Output is correct
28 Correct 3 ms 5848 KB Output is correct
29 Correct 3 ms 5804 KB Output is correct
30 Correct 2 ms 5724 KB Output is correct
31 Correct 3 ms 5492 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 60 ms 13400 KB Output is correct
34 Correct 70 ms 24308 KB Output is correct
35 Correct 16 ms 9824 KB Output is correct
36 Correct 103 ms 20228 KB Output is correct
37 Correct 238 ms 37316 KB Output is correct
38 Correct 116 ms 28620 KB Output is correct
39 Correct 97 ms 20176 KB Output is correct
40 Correct 100 ms 22000 KB Output is correct
41 Correct 101 ms 21332 KB Output is correct
42 Correct 110 ms 21452 KB Output is correct
43 Correct 70 ms 21448 KB Output is correct
44 Correct 120 ms 30916 KB Output is correct
45 Correct 109 ms 28756 KB Output is correct
46 Correct 114 ms 28216 KB Output is correct
47 Correct 64 ms 21396 KB Output is correct
48 Correct 106 ms 32716 KB Output is correct
49 Correct 110 ms 29132 KB Output is correct
50 Correct 78 ms 18516 KB Output is correct
51 Correct 182 ms 40120 KB Output is correct
52 Correct 18 ms 10452 KB Output is correct
53 Correct 105 ms 21656 KB Output is correct
54 Correct 168 ms 41652 KB Output is correct
55 Correct 148 ms 31684 KB Output is correct
56 Correct 98 ms 22608 KB Output is correct
57 Correct 122 ms 25808 KB Output is correct
58 Correct 116 ms 24660 KB Output is correct
59 Correct 119 ms 23456 KB Output is correct
60 Correct 120 ms 22492 KB Output is correct
61 Correct 163 ms 38112 KB Output is correct
62 Correct 137 ms 30408 KB Output is correct
63 Correct 153 ms 30788 KB Output is correct
64 Correct 65 ms 22464 KB Output is correct
65 Correct 138 ms 36672 KB Output is correct
66 Correct 128 ms 31944 KB Output is correct
67 Correct 13 ms 8540 KB Output is correct
68 Correct 59 ms 20788 KB Output is correct
69 Correct 76 ms 21184 KB Output is correct
70 Correct 108 ms 20024 KB Output is correct
71 Correct 158 ms 37612 KB Output is correct
72 Correct 126 ms 28616 KB Output is correct
73 Correct 96 ms 20308 KB Output is correct
74 Correct 108 ms 23500 KB Output is correct
75 Correct 102 ms 21712 KB Output is correct
76 Correct 120 ms 21712 KB Output is correct
77 Correct 88 ms 20940 KB Output is correct
78 Correct 124 ms 33992 KB Output is correct
79 Correct 120 ms 31036 KB Output is correct
80 Correct 122 ms 27428 KB Output is correct
81 Correct 62 ms 21232 KB Output is correct
82 Correct 119 ms 32704 KB Output is correct
83 Correct 107 ms 29300 KB Output is correct
84 Correct 103 ms 20308 KB Output is correct
85 Correct 147 ms 37976 KB Output is correct
86 Correct 147 ms 29140 KB Output is correct
87 Correct 96 ms 20624 KB Output is correct
88 Correct 108 ms 24276 KB Output is correct
89 Correct 106 ms 22740 KB Output is correct
90 Correct 105 ms 22732 KB Output is correct
91 Correct 94 ms 21452 KB Output is correct
92 Correct 146 ms 37564 KB Output is correct
93 Correct 117 ms 26700 KB Output is correct
94 Correct 104 ms 25036 KB Output is correct
95 Correct 74 ms 21960 KB Output is correct
96 Correct 135 ms 33084 KB Output is correct
97 Correct 119 ms 29652 KB Output is correct
98 Correct 120 ms 21564 KB Output is correct
99 Correct 184 ms 38592 KB Output is correct
100 Correct 135 ms 30996 KB Output is correct
101 Correct 99 ms 21192 KB Output is correct
102 Correct 111 ms 24252 KB Output is correct
103 Correct 116 ms 23140 KB Output is correct
104 Correct 111 ms 22704 KB Output is correct
105 Correct 82 ms 21704 KB Output is correct
106 Correct 146 ms 32084 KB Output is correct
107 Correct 143 ms 31812 KB Output is correct
108 Correct 123 ms 27844 KB Output is correct
109 Correct 72 ms 22468 KB Output is correct
110 Correct 138 ms 34364 KB Output is correct
111 Correct 149 ms 31188 KB Output is correct