답안 #898225

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
898225 2024-01-04T11:46:09 Z NeroZein 수도 (JOI20_capital_city) C++17
100 / 100
609 ms 42256 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int N = 2e5 + 5;

int pr[N];
int sz[N];
int tot[N];
int color[N];
bool viscol[N];
bool blocked[N];
vector<int> g[N];
vector<int> ofcolor[N];

void find_sizes(int v, int p, int flag) {
  sz[v] = 1;
  pr[v] = p;
  if (flag == 1) {
    ofcolor[color[v]].push_back(v);     
  } else if (flag == -1) {
    ofcolor[color[v]].pop_back();
  }
  for (int u : g[v]) {
    if (!blocked[u] && u != p) {
      find_sizes(u, v, flag);
      sz[v] += sz[u];
    }
  }
}

int find_centroid(int v, int p, int x) {
  for (int u : g[v]) {
    if (!blocked[u] && u != p && sz[u] > x / 2) {
      return find_centroid(u, v, x); 
    }
  }
  return v;
}

int decompose(int root) {
  find_sizes(root, root, 0); 
  int centroid = find_centroid(root, root, sz[root]); 
  find_sizes(centroid, centroid, 1); 
  queue<int> que;
  viscol[color[centroid]] = true; 
  vector<int> used = {color[centroid]}; 
  for (int i : ofcolor[color[centroid]]) {
    que.push(i); 
  }
  while (!que.empty()) {
    int v = que.front(); 
    que.pop();
    if (!viscol[color[pr[v]]]) { 
      used.push_back(color[pr[v]]); 
      viscol[color[pr[v]]] = true;
      for (int i : ofcolor[color[pr[v]]]) {
        que.push(i); 
      }
    }
  }
  bool ok = true; 
  for (int i : used) {  
    viscol[i] = false; 
    ok &= ((int) ofcolor[i].size() == tot[i]);
  }
  find_sizes(centroid, centroid, -1); 
  int ret = (ok ? (int) used.size() : N);
  blocked[centroid] = true; 
  for (int u : g[centroid]) {
    if (!blocked[u]) {
      ret = min(ret, decompose(u)); 
    }
  }
  return ret; 
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  for(int i = 0; i < n - 1; ++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 >> color[i];
    tot[color[i]]++; 
  }
  int ans = decompose(1);
  cout << ans - 1 << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12948 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 2 ms 12892 KB Output is correct
9 Correct 3 ms 12980 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12948 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 2 ms 12892 KB Output is correct
9 Correct 3 ms 12980 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 4 ms 12960 KB Output is correct
13 Correct 4 ms 12892 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 5 ms 12892 KB Output is correct
16 Correct 4 ms 12892 KB Output is correct
17 Correct 4 ms 13040 KB Output is correct
18 Correct 4 ms 12904 KB Output is correct
19 Correct 3 ms 13148 KB Output is correct
20 Correct 4 ms 12892 KB Output is correct
21 Correct 4 ms 13148 KB Output is correct
22 Correct 3 ms 13036 KB Output is correct
23 Correct 5 ms 13148 KB Output is correct
24 Correct 4 ms 12892 KB Output is correct
25 Correct 4 ms 13148 KB Output is correct
26 Correct 4 ms 13236 KB Output is correct
27 Correct 5 ms 13148 KB Output is correct
28 Correct 4 ms 13148 KB Output is correct
29 Correct 4 ms 13144 KB Output is correct
30 Correct 4 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 409 ms 41576 KB Output is correct
2 Correct 175 ms 42068 KB Output is correct
3 Correct 359 ms 41044 KB Output is correct
4 Correct 184 ms 42256 KB Output is correct
5 Correct 389 ms 38392 KB Output is correct
6 Correct 176 ms 42072 KB Output is correct
7 Correct 330 ms 38808 KB Output is correct
8 Correct 191 ms 41848 KB Output is correct
9 Correct 520 ms 37472 KB Output is correct
10 Correct 463 ms 34956 KB Output is correct
11 Correct 499 ms 37744 KB Output is correct
12 Correct 544 ms 40392 KB Output is correct
13 Correct 487 ms 34304 KB Output is correct
14 Correct 509 ms 40676 KB Output is correct
15 Correct 507 ms 40272 KB Output is correct
16 Correct 503 ms 35320 KB Output is correct
17 Correct 482 ms 35776 KB Output is correct
18 Correct 493 ms 36108 KB Output is correct
19 Correct 551 ms 39276 KB Output is correct
20 Correct 512 ms 41384 KB Output is correct
21 Correct 3 ms 12888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 3 ms 12948 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 2 ms 12892 KB Output is correct
9 Correct 3 ms 12980 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 4 ms 12960 KB Output is correct
13 Correct 4 ms 12892 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 5 ms 12892 KB Output is correct
16 Correct 4 ms 12892 KB Output is correct
17 Correct 4 ms 13040 KB Output is correct
18 Correct 4 ms 12904 KB Output is correct
19 Correct 3 ms 13148 KB Output is correct
20 Correct 4 ms 12892 KB Output is correct
21 Correct 4 ms 13148 KB Output is correct
22 Correct 3 ms 13036 KB Output is correct
23 Correct 5 ms 13148 KB Output is correct
24 Correct 4 ms 12892 KB Output is correct
25 Correct 4 ms 13148 KB Output is correct
26 Correct 4 ms 13236 KB Output is correct
27 Correct 5 ms 13148 KB Output is correct
28 Correct 4 ms 13148 KB Output is correct
29 Correct 4 ms 13144 KB Output is correct
30 Correct 4 ms 13148 KB Output is correct
31 Correct 409 ms 41576 KB Output is correct
32 Correct 175 ms 42068 KB Output is correct
33 Correct 359 ms 41044 KB Output is correct
34 Correct 184 ms 42256 KB Output is correct
35 Correct 389 ms 38392 KB Output is correct
36 Correct 176 ms 42072 KB Output is correct
37 Correct 330 ms 38808 KB Output is correct
38 Correct 191 ms 41848 KB Output is correct
39 Correct 520 ms 37472 KB Output is correct
40 Correct 463 ms 34956 KB Output is correct
41 Correct 499 ms 37744 KB Output is correct
42 Correct 544 ms 40392 KB Output is correct
43 Correct 487 ms 34304 KB Output is correct
44 Correct 509 ms 40676 KB Output is correct
45 Correct 507 ms 40272 KB Output is correct
46 Correct 503 ms 35320 KB Output is correct
47 Correct 482 ms 35776 KB Output is correct
48 Correct 493 ms 36108 KB Output is correct
49 Correct 551 ms 39276 KB Output is correct
50 Correct 512 ms 41384 KB Output is correct
51 Correct 3 ms 12888 KB Output is correct
52 Correct 270 ms 25532 KB Output is correct
53 Correct 259 ms 25416 KB Output is correct
54 Correct 304 ms 25412 KB Output is correct
55 Correct 307 ms 25548 KB Output is correct
56 Correct 254 ms 25436 KB Output is correct
57 Correct 279 ms 25424 KB Output is correct
58 Correct 267 ms 28748 KB Output is correct
59 Correct 285 ms 29008 KB Output is correct
60 Correct 348 ms 28324 KB Output is correct
61 Correct 381 ms 27732 KB Output is correct
62 Correct 175 ms 41848 KB Output is correct
63 Correct 172 ms 42064 KB Output is correct
64 Correct 176 ms 41760 KB Output is correct
65 Correct 187 ms 42096 KB Output is correct
66 Correct 211 ms 33740 KB Output is correct
67 Correct 229 ms 33992 KB Output is correct
68 Correct 254 ms 33740 KB Output is correct
69 Correct 210 ms 33740 KB Output is correct
70 Correct 225 ms 33996 KB Output is correct
71 Correct 239 ms 33612 KB Output is correct
72 Correct 231 ms 33868 KB Output is correct
73 Correct 277 ms 33140 KB Output is correct
74 Correct 226 ms 33952 KB Output is correct
75 Correct 252 ms 33740 KB Output is correct
76 Correct 306 ms 32216 KB Output is correct
77 Correct 263 ms 30600 KB Output is correct
78 Correct 530 ms 35836 KB Output is correct
79 Correct 492 ms 34096 KB Output is correct
80 Correct 502 ms 40932 KB Output is correct
81 Correct 512 ms 37512 KB Output is correct
82 Correct 526 ms 37564 KB Output is correct
83 Correct 522 ms 34288 KB Output is correct
84 Correct 516 ms 40016 KB Output is correct
85 Correct 500 ms 38648 KB Output is correct
86 Correct 439 ms 33880 KB Output is correct
87 Correct 609 ms 35724 KB Output is correct
88 Correct 576 ms 36560 KB Output is correct
89 Correct 513 ms 32340 KB Output is correct
90 Correct 534 ms 32216 KB Output is correct
91 Correct 572 ms 34864 KB Output is correct
92 Correct 548 ms 33316 KB Output is correct
93 Correct 473 ms 33080 KB Output is correct
94 Correct 562 ms 32476 KB Output is correct
95 Correct 502 ms 33876 KB Output is correct
96 Correct 605 ms 32616 KB Output is correct
97 Correct 533 ms 34664 KB Output is correct