Submission #538568

# Submission time Handle Problem Language Result Execution time Memory
538568 2022-03-17T06:45:09 Z Soumya1 Mergers (JOI19_mergers) C++17
100 / 100
1137 ms 123972 KB
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 5e5 + 5;
const int lg = 20;
vector<int> ad[mxN];
int c[mxN];
int p[mxN][lg];
int d[mxN];
int mn[mxN];
int f[mxN];
int leafs = 0;
int comps = 0;
void dfs(int u) {
  for (int i = 1; i < lg; i++) {
    p[u][i] = p[p[u][i - 1]][i - 1];
  }
  for (int v : ad[u]) {
    if (v == p[u][0]) continue;
    p[v][0] = u;
    d[v] = d[u] + 1;
    dfs(v);
  }
}
int lca(int u, int v) {
  if (d[u] < d[v]) swap(u, v);
  for (int j = 0; j < lg; j++) {
    if ((d[u] - d[v]) & (1 << j)) {
      u = p[u][j];
    }
  }
  if (u == v) return u;
  for (int i = lg - 1; i >= 0; i--) {
    if (p[u][i] != p[v][i]) {
      u = p[u][i];
      v = p[v][i];
    }
  }
  return p[u][0];
}
void decompose(int u) {
  for (int v : ad[u]) {
    if (v == p[u][0]) continue;
    decompose(v);
    mn[u] = min(mn[u], mn[v]);
    f[u] += f[v];
  }
  if (mn[u] == d[u]) {
    comps++;
    if (u == 1 && f[u] == 1) leafs++;
    if (u > 1 && f[u] == 0) leafs++;
    f[u] = 1;
  }
}
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 >> c[i];
  dfs(1);
  {
    vector<int> nodes[k + 1];
    for (int i = 1; i <= n; i++) {
      mn[i] = d[i];
      nodes[c[i]].push_back(i);
    }
    for (int i = 1; i <= k; i++) {
      int l = nodes[i][0];
      for (int j : nodes[i]) {
        l = lca(l, j);
      }
      for (int j : nodes[i]) {
        mn[j] = min(mn[j], d[l]);
      }
    }
  }
  decompose(1);
  cout << (leafs + 1) / 2 << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 7 ms 12116 KB Output is correct
3 Correct 7 ms 12116 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 12116 KB Output is correct
7 Correct 7 ms 12100 KB Output is correct
8 Correct 6 ms 12044 KB Output is correct
9 Correct 7 ms 12100 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 6 ms 12116 KB Output is correct
12 Correct 7 ms 12116 KB Output is correct
13 Correct 7 ms 11988 KB Output is correct
14 Correct 9 ms 12116 KB Output is correct
15 Correct 10 ms 12116 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 12068 KB Output is correct
18 Correct 8 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12068 KB Output is correct
22 Correct 6 ms 12076 KB Output is correct
23 Correct 6 ms 12076 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 7 ms 12116 KB Output is correct
3 Correct 7 ms 12116 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 12116 KB Output is correct
7 Correct 7 ms 12100 KB Output is correct
8 Correct 6 ms 12044 KB Output is correct
9 Correct 7 ms 12100 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 6 ms 12116 KB Output is correct
12 Correct 7 ms 12116 KB Output is correct
13 Correct 7 ms 11988 KB Output is correct
14 Correct 9 ms 12116 KB Output is correct
15 Correct 10 ms 12116 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 12068 KB Output is correct
18 Correct 8 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12068 KB Output is correct
22 Correct 6 ms 12076 KB Output is correct
23 Correct 6 ms 12076 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
25 Correct 7 ms 12076 KB Output is correct
26 Correct 9 ms 12600 KB Output is correct
27 Correct 10 ms 12500 KB Output is correct
28 Correct 9 ms 12628 KB Output is correct
29 Correct 9 ms 12596 KB Output is correct
30 Correct 8 ms 12500 KB Output is correct
31 Correct 7 ms 12072 KB Output is correct
32 Correct 8 ms 12724 KB Output is correct
33 Correct 7 ms 12068 KB Output is correct
34 Correct 9 ms 12464 KB Output is correct
35 Correct 8 ms 12628 KB Output is correct
36 Correct 9 ms 12468 KB Output is correct
37 Correct 9 ms 12536 KB Output is correct
38 Correct 7 ms 12116 KB Output is correct
39 Correct 9 ms 12592 KB Output is correct
40 Correct 10 ms 12504 KB Output is correct
41 Correct 9 ms 12500 KB Output is correct
42 Correct 8 ms 12500 KB Output is correct
43 Correct 9 ms 12756 KB Output is correct
44 Correct 7 ms 12068 KB Output is correct
45 Correct 9 ms 12628 KB Output is correct
46 Correct 8 ms 12500 KB Output is correct
47 Correct 6 ms 12116 KB Output is correct
48 Correct 9 ms 12452 KB Output is correct
49 Correct 9 ms 12652 KB Output is correct
50 Correct 10 ms 12628 KB Output is correct
51 Correct 10 ms 12468 KB Output is correct
52 Correct 9 ms 12628 KB Output is correct
53 Correct 9 ms 12596 KB Output is correct
54 Correct 8 ms 12500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 7 ms 12116 KB Output is correct
3 Correct 7 ms 12116 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 12116 KB Output is correct
7 Correct 7 ms 12100 KB Output is correct
8 Correct 6 ms 12044 KB Output is correct
9 Correct 7 ms 12100 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 6 ms 12116 KB Output is correct
12 Correct 7 ms 12116 KB Output is correct
13 Correct 7 ms 11988 KB Output is correct
14 Correct 9 ms 12116 KB Output is correct
15 Correct 10 ms 12116 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 12068 KB Output is correct
18 Correct 8 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12068 KB Output is correct
22 Correct 6 ms 12076 KB Output is correct
23 Correct 6 ms 12076 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
25 Correct 7 ms 12116 KB Output is correct
26 Correct 74 ms 26564 KB Output is correct
27 Correct 114 ms 26336 KB Output is correct
28 Correct 9 ms 12500 KB Output is correct
29 Correct 7 ms 12116 KB Output is correct
30 Correct 7 ms 12116 KB Output is correct
31 Correct 112 ms 26328 KB Output is correct
32 Correct 8 ms 12500 KB Output is correct
33 Correct 148 ms 33488 KB Output is correct
34 Correct 99 ms 26288 KB Output is correct
35 Correct 9 ms 12500 KB Output is correct
36 Correct 154 ms 27032 KB Output is correct
37 Correct 10 ms 12500 KB Output is correct
38 Correct 8 ms 12500 KB Output is correct
39 Correct 71 ms 26532 KB Output is correct
40 Correct 9 ms 12756 KB Output is correct
41 Correct 90 ms 26148 KB Output is correct
42 Correct 152 ms 28624 KB Output is correct
43 Correct 7 ms 12116 KB Output is correct
44 Correct 179 ms 33676 KB Output is correct
45 Correct 137 ms 30032 KB Output is correct
46 Correct 8 ms 12468 KB Output is correct
47 Correct 8 ms 12500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 25112 KB Output is correct
2 Correct 100 ms 30480 KB Output is correct
3 Correct 10 ms 12628 KB Output is correct
4 Correct 9 ms 12500 KB Output is correct
5 Correct 7 ms 12068 KB Output is correct
6 Correct 6 ms 12072 KB Output is correct
7 Correct 8 ms 12500 KB Output is correct
8 Correct 121 ms 29096 KB Output is correct
9 Correct 9 ms 12468 KB Output is correct
10 Correct 131 ms 26864 KB Output is correct
11 Correct 9 ms 12116 KB Output is correct
12 Correct 118 ms 26700 KB Output is correct
13 Correct 116 ms 29116 KB Output is correct
14 Correct 114 ms 31924 KB Output is correct
15 Correct 85 ms 26516 KB Output is correct
16 Correct 9 ms 12600 KB Output is correct
17 Correct 7 ms 12116 KB Output is correct
18 Correct 90 ms 31600 KB Output is correct
19 Correct 142 ms 33464 KB Output is correct
20 Correct 9 ms 12484 KB Output is correct
21 Correct 8 ms 12072 KB Output is correct
22 Correct 88 ms 29316 KB Output is correct
23 Correct 9 ms 12500 KB Output is correct
24 Correct 141 ms 27296 KB Output is correct
25 Correct 136 ms 32088 KB Output is correct
26 Correct 8 ms 12616 KB Output is correct
27 Correct 10 ms 12676 KB Output is correct
28 Correct 9 ms 12464 KB Output is correct
29 Correct 8 ms 12492 KB Output is correct
30 Correct 8 ms 12500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 7 ms 12116 KB Output is correct
3 Correct 7 ms 12116 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 7 ms 12116 KB Output is correct
7 Correct 7 ms 12100 KB Output is correct
8 Correct 6 ms 12044 KB Output is correct
9 Correct 7 ms 12100 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 6 ms 12116 KB Output is correct
12 Correct 7 ms 12116 KB Output is correct
13 Correct 7 ms 11988 KB Output is correct
14 Correct 9 ms 12116 KB Output is correct
15 Correct 10 ms 12116 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 12068 KB Output is correct
18 Correct 8 ms 12076 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 6 ms 12068 KB Output is correct
22 Correct 6 ms 12076 KB Output is correct
23 Correct 6 ms 12076 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
25 Correct 7 ms 12076 KB Output is correct
26 Correct 9 ms 12600 KB Output is correct
27 Correct 10 ms 12500 KB Output is correct
28 Correct 9 ms 12628 KB Output is correct
29 Correct 9 ms 12596 KB Output is correct
30 Correct 8 ms 12500 KB Output is correct
31 Correct 7 ms 12072 KB Output is correct
32 Correct 8 ms 12724 KB Output is correct
33 Correct 7 ms 12068 KB Output is correct
34 Correct 9 ms 12464 KB Output is correct
35 Correct 8 ms 12628 KB Output is correct
36 Correct 9 ms 12468 KB Output is correct
37 Correct 9 ms 12536 KB Output is correct
38 Correct 7 ms 12116 KB Output is correct
39 Correct 9 ms 12592 KB Output is correct
40 Correct 10 ms 12504 KB Output is correct
41 Correct 9 ms 12500 KB Output is correct
42 Correct 8 ms 12500 KB Output is correct
43 Correct 9 ms 12756 KB Output is correct
44 Correct 7 ms 12068 KB Output is correct
45 Correct 9 ms 12628 KB Output is correct
46 Correct 8 ms 12500 KB Output is correct
47 Correct 6 ms 12116 KB Output is correct
48 Correct 9 ms 12452 KB Output is correct
49 Correct 9 ms 12652 KB Output is correct
50 Correct 10 ms 12628 KB Output is correct
51 Correct 10 ms 12468 KB Output is correct
52 Correct 9 ms 12628 KB Output is correct
53 Correct 9 ms 12596 KB Output is correct
54 Correct 8 ms 12500 KB Output is correct
55 Correct 7 ms 12116 KB Output is correct
56 Correct 74 ms 26564 KB Output is correct
57 Correct 114 ms 26336 KB Output is correct
58 Correct 9 ms 12500 KB Output is correct
59 Correct 7 ms 12116 KB Output is correct
60 Correct 7 ms 12116 KB Output is correct
61 Correct 112 ms 26328 KB Output is correct
62 Correct 8 ms 12500 KB Output is correct
63 Correct 148 ms 33488 KB Output is correct
64 Correct 99 ms 26288 KB Output is correct
65 Correct 9 ms 12500 KB Output is correct
66 Correct 154 ms 27032 KB Output is correct
67 Correct 10 ms 12500 KB Output is correct
68 Correct 8 ms 12500 KB Output is correct
69 Correct 71 ms 26532 KB Output is correct
70 Correct 9 ms 12756 KB Output is correct
71 Correct 90 ms 26148 KB Output is correct
72 Correct 152 ms 28624 KB Output is correct
73 Correct 7 ms 12116 KB Output is correct
74 Correct 179 ms 33676 KB Output is correct
75 Correct 137 ms 30032 KB Output is correct
76 Correct 8 ms 12468 KB Output is correct
77 Correct 8 ms 12500 KB Output is correct
78 Correct 85 ms 25112 KB Output is correct
79 Correct 100 ms 30480 KB Output is correct
80 Correct 10 ms 12628 KB Output is correct
81 Correct 9 ms 12500 KB Output is correct
82 Correct 7 ms 12068 KB Output is correct
83 Correct 6 ms 12072 KB Output is correct
84 Correct 8 ms 12500 KB Output is correct
85 Correct 121 ms 29096 KB Output is correct
86 Correct 9 ms 12468 KB Output is correct
87 Correct 131 ms 26864 KB Output is correct
88 Correct 9 ms 12116 KB Output is correct
89 Correct 118 ms 26700 KB Output is correct
90 Correct 116 ms 29116 KB Output is correct
91 Correct 114 ms 31924 KB Output is correct
92 Correct 85 ms 26516 KB Output is correct
93 Correct 9 ms 12600 KB Output is correct
94 Correct 7 ms 12116 KB Output is correct
95 Correct 90 ms 31600 KB Output is correct
96 Correct 142 ms 33464 KB Output is correct
97 Correct 9 ms 12484 KB Output is correct
98 Correct 8 ms 12072 KB Output is correct
99 Correct 88 ms 29316 KB Output is correct
100 Correct 9 ms 12500 KB Output is correct
101 Correct 141 ms 27296 KB Output is correct
102 Correct 136 ms 32088 KB Output is correct
103 Correct 8 ms 12616 KB Output is correct
104 Correct 10 ms 12676 KB Output is correct
105 Correct 9 ms 12464 KB Output is correct
106 Correct 8 ms 12492 KB Output is correct
107 Correct 8 ms 12500 KB Output is correct
108 Correct 611 ms 97620 KB Output is correct
109 Correct 1092 ms 101052 KB Output is correct
110 Correct 1137 ms 112224 KB Output is correct
111 Correct 1038 ms 123972 KB Output is correct
112 Correct 995 ms 114096 KB Output is correct
113 Correct 640 ms 111392 KB Output is correct
114 Correct 706 ms 82972 KB Output is correct
115 Correct 709 ms 82980 KB Output is correct
116 Correct 851 ms 92140 KB Output is correct
117 Correct 865 ms 112532 KB Output is correct
118 Correct 702 ms 83540 KB Output is correct
119 Correct 864 ms 112540 KB Output is correct
120 Correct 1044 ms 114780 KB Output is correct
121 Correct 879 ms 112556 KB Output is correct
122 Correct 932 ms 101556 KB Output is correct
123 Correct 631 ms 114072 KB Output is correct
124 Correct 976 ms 109980 KB Output is correct