# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
538566 |
2022-03-17T06:39:27 Z |
Soumya1 |
Mergers (JOI19_mergers) |
C++17 |
|
96 ms |
32120 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 (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);
if (comps == 1) leafs--;
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 |
7 ms |
12068 KB |
Output is correct |
2 |
Correct |
9 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12116 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
10 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
8 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12080 KB |
Output is correct |
10 |
Correct |
8 ms |
12076 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
8 ms |
12008 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
12116 KB |
Output is correct |
15 |
Correct |
9 ms |
12116 KB |
Output is correct |
16 |
Incorrect |
7 ms |
12072 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12068 KB |
Output is correct |
2 |
Correct |
9 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12116 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
10 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
8 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12080 KB |
Output is correct |
10 |
Correct |
8 ms |
12076 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
8 ms |
12008 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
12116 KB |
Output is correct |
15 |
Correct |
9 ms |
12116 KB |
Output is correct |
16 |
Incorrect |
7 ms |
12072 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12068 KB |
Output is correct |
2 |
Correct |
9 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12116 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
10 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
8 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12080 KB |
Output is correct |
10 |
Correct |
8 ms |
12076 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
8 ms |
12008 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
12116 KB |
Output is correct |
15 |
Correct |
9 ms |
12116 KB |
Output is correct |
16 |
Incorrect |
7 ms |
12072 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
26564 KB |
Output is correct |
2 |
Incorrect |
96 ms |
32120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12068 KB |
Output is correct |
2 |
Correct |
9 ms |
12116 KB |
Output is correct |
3 |
Correct |
7 ms |
12116 KB |
Output is correct |
4 |
Correct |
8 ms |
11988 KB |
Output is correct |
5 |
Correct |
10 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
7 ms |
11988 KB |
Output is correct |
8 |
Correct |
8 ms |
12116 KB |
Output is correct |
9 |
Correct |
7 ms |
12080 KB |
Output is correct |
10 |
Correct |
8 ms |
12076 KB |
Output is correct |
11 |
Correct |
7 ms |
12116 KB |
Output is correct |
12 |
Correct |
8 ms |
12008 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
12116 KB |
Output is correct |
15 |
Correct |
9 ms |
12116 KB |
Output is correct |
16 |
Incorrect |
7 ms |
12072 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |