Submission #602040

# Submission time Handle Problem Language Result Execution time Memory
602040 2022-07-22T14:03:32 Z Soumya1 Capital City (JOI20_capital_city) C++17
100 / 100
822 ms 43004 KB
#include <bits/stdc++.h>
#ifdef __LOCAL__
  #include <debug_local.h>
#endif
using namespace std;
const int mxN = 2e5 + 5;
vector<int> ad[mxN], l[mxN];
int city[mxN];
int ans = mxN;
int sz[mxN], par[mxN];
bool deleted[mxN], visc[mxN];
vector<int> vec;
void get_size(int u, int p = -1) {
  vec.push_back(u);
  visc[city[u]] = false;
  sz[u] = 1;
  for (int v : ad[u]) {
    if (v == p || deleted[v]) continue;
    get_size(v, u);
    par[v] = u;
    sz[u] += sz[v];
  }
}
int get_centroid(int u, int p, int n) {
  for (int v : ad[u]) {
    if (v == p || deleted[v]) continue;
    if (sz[v] > n / 2) return get_centroid(v, u, n); 
  }
  return u;
}
void solve(int s) {
  get_size(s);
  int c = get_centroid(s, -1, sz[s]);
  vec.clear();
  get_size(c);
  sort(vec.begin(), vec.end());
  int cur = 0;
  queue<int> q;
  visc[city[c]] = true;
  q.push(city[c]);
  while (!q.empty()) {
    cur++;
    int u = q.front();
    q.pop();
    for (int v : l[u]) {
      if (!binary_search(vec.begin(), vec.end(), v)) {
        cur = mxN;
        break;
      }
      else {
        if (v != c && !visc[city[par[v]]]) {
          q.push(city[par[v]]);
          visc[city[par[v]]] = true;
        }
      }
    }
  }
  ans = min(ans, cur);
  deleted[c] = true;
  for (int v : ad[c]) {
    if (!deleted[v]) solve(v);
  }
}
void testCase() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    ad[u].push_back(v);
    ad[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    cin >> city[i];
    l[city[i]].push_back(i);
  }
  solve(1);
  cout << ans - 1 << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 8 ms 9864 KB Output is correct
12 Correct 8 ms 9864 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 7 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 8 ms 9896 KB Output is correct
17 Correct 7 ms 9940 KB Output is correct
18 Correct 6 ms 9940 KB Output is correct
19 Correct 7 ms 9948 KB Output is correct
20 Correct 7 ms 9940 KB Output is correct
21 Correct 6 ms 9876 KB Output is correct
22 Correct 7 ms 9940 KB Output is correct
23 Correct 6 ms 9940 KB Output is correct
24 Correct 7 ms 9904 KB Output is correct
25 Correct 8 ms 9916 KB Output is correct
26 Correct 7 ms 10016 KB Output is correct
27 Correct 7 ms 9940 KB Output is correct
28 Correct 7 ms 9940 KB Output is correct
29 Correct 7 ms 9852 KB Output is correct
30 Correct 8 ms 9916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 610 ms 38844 KB Output is correct
2 Correct 290 ms 39308 KB Output is correct
3 Correct 658 ms 38704 KB Output is correct
4 Correct 316 ms 39320 KB Output is correct
5 Correct 720 ms 35588 KB Output is correct
6 Correct 307 ms 39296 KB Output is correct
7 Correct 648 ms 35904 KB Output is correct
8 Correct 307 ms 42684 KB Output is correct
9 Correct 822 ms 37744 KB Output is correct
10 Correct 794 ms 35208 KB Output is correct
11 Correct 809 ms 38216 KB Output is correct
12 Correct 750 ms 40704 KB Output is correct
13 Correct 770 ms 34852 KB Output is correct
14 Correct 744 ms 41088 KB Output is correct
15 Correct 736 ms 40812 KB Output is correct
16 Correct 739 ms 35856 KB Output is correct
17 Correct 733 ms 36368 KB Output is correct
18 Correct 773 ms 32956 KB Output is correct
19 Correct 783 ms 36008 KB Output is correct
20 Correct 724 ms 38208 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 8 ms 9864 KB Output is correct
12 Correct 8 ms 9864 KB Output is correct
13 Correct 7 ms 9812 KB Output is correct
14 Correct 7 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 8 ms 9896 KB Output is correct
17 Correct 7 ms 9940 KB Output is correct
18 Correct 6 ms 9940 KB Output is correct
19 Correct 7 ms 9948 KB Output is correct
20 Correct 7 ms 9940 KB Output is correct
21 Correct 6 ms 9876 KB Output is correct
22 Correct 7 ms 9940 KB Output is correct
23 Correct 6 ms 9940 KB Output is correct
24 Correct 7 ms 9904 KB Output is correct
25 Correct 8 ms 9916 KB Output is correct
26 Correct 7 ms 10016 KB Output is correct
27 Correct 7 ms 9940 KB Output is correct
28 Correct 7 ms 9940 KB Output is correct
29 Correct 7 ms 9852 KB Output is correct
30 Correct 8 ms 9916 KB Output is correct
31 Correct 610 ms 38844 KB Output is correct
32 Correct 290 ms 39308 KB Output is correct
33 Correct 658 ms 38704 KB Output is correct
34 Correct 316 ms 39320 KB Output is correct
35 Correct 720 ms 35588 KB Output is correct
36 Correct 307 ms 39296 KB Output is correct
37 Correct 648 ms 35904 KB Output is correct
38 Correct 307 ms 42684 KB Output is correct
39 Correct 822 ms 37744 KB Output is correct
40 Correct 794 ms 35208 KB Output is correct
41 Correct 809 ms 38216 KB Output is correct
42 Correct 750 ms 40704 KB Output is correct
43 Correct 770 ms 34852 KB Output is correct
44 Correct 744 ms 41088 KB Output is correct
45 Correct 736 ms 40812 KB Output is correct
46 Correct 739 ms 35856 KB Output is correct
47 Correct 733 ms 36368 KB Output is correct
48 Correct 773 ms 32956 KB Output is correct
49 Correct 783 ms 36008 KB Output is correct
50 Correct 724 ms 38208 KB Output is correct
51 Correct 5 ms 9684 KB Output is correct
52 Correct 472 ms 21200 KB Output is correct
53 Correct 531 ms 21220 KB Output is correct
54 Correct 500 ms 22304 KB Output is correct
55 Correct 632 ms 21268 KB Output is correct
56 Correct 544 ms 21200 KB Output is correct
57 Correct 497 ms 21240 KB Output is correct
58 Correct 586 ms 25872 KB Output is correct
59 Correct 574 ms 29764 KB Output is correct
60 Correct 580 ms 29064 KB Output is correct
61 Correct 630 ms 28792 KB Output is correct
62 Correct 298 ms 42996 KB Output is correct
63 Correct 289 ms 43004 KB Output is correct
64 Correct 294 ms 42656 KB Output is correct
65 Correct 296 ms 42996 KB Output is correct
66 Correct 359 ms 34556 KB Output is correct
67 Correct 427 ms 34344 KB Output is correct
68 Correct 371 ms 34480 KB Output is correct
69 Correct 380 ms 34472 KB Output is correct
70 Correct 399 ms 34388 KB Output is correct
71 Correct 367 ms 34432 KB Output is correct
72 Correct 369 ms 34432 KB Output is correct
73 Correct 392 ms 33584 KB Output is correct
74 Correct 363 ms 34460 KB Output is correct
75 Correct 376 ms 34472 KB Output is correct
76 Correct 664 ms 32980 KB Output is correct
77 Correct 641 ms 30276 KB Output is correct
78 Correct 679 ms 36332 KB Output is correct
79 Correct 679 ms 34304 KB Output is correct
80 Correct 674 ms 41380 KB Output is correct
81 Correct 686 ms 37904 KB Output is correct
82 Correct 676 ms 38076 KB Output is correct
83 Correct 685 ms 34588 KB Output is correct
84 Correct 694 ms 40640 KB Output is correct
85 Correct 682 ms 38972 KB Output is correct
86 Correct 692 ms 34172 KB Output is correct
87 Correct 692 ms 36040 KB Output is correct
88 Correct 614 ms 37192 KB Output is correct
89 Correct 686 ms 33184 KB Output is correct
90 Correct 609 ms 32968 KB Output is correct
91 Correct 629 ms 35404 KB Output is correct
92 Correct 602 ms 34168 KB Output is correct
93 Correct 603 ms 33856 KB Output is correct
94 Correct 608 ms 33084 KB Output is correct
95 Correct 626 ms 34548 KB Output is correct
96 Correct 617 ms 33380 KB Output is correct
97 Correct 664 ms 35300 KB Output is correct