#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 5e5 + 5;
int n, k, a[N], dep[N], par[N][20], alllca[N];
vector<int> g[N];
void dfs(int u, int p) {
par[u][0] = p;
fa (v, g[u]) if (v != p) {
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
void sparse() {
fw (j, 1, 20) fw (i, 0, n) if (par[i][j - 1] != -1) {
par[i][j] = par[par[i][j - 1]][j - 1];
}
}
int getLCA(int u, int v) {
if (u == -1) return v;
if (v == -1) return u;
if (dep[u] < dep[v]) swap(u, v);
int diff = dep[u] - dep[v];
fw (i, 0, 20) if (diff & (1 << i)) u = par[u][i];
if (u == v) return u;
bw (i, 20, 0) if (par[u][i] != par[v][i]) {
u = par[u][i], v = par[v][i];
}
return par[u][0];
}
bool col[N];
int mnDep[N];
void dfs2(int u, int p) {
mnDep[u] = dep[alllca[a[u]]];
fa (v, g[u]) if (v != p) {
dfs2(v, u);
mnDep[u] = min(mnDep[u], mnDep[v]);
}
if (mnDep[u] == dep[u]) col[u] = 0;
else col[u] = 1;
}
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> k;
fw (i, 1, n) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].pb(v), g[v].pb(u);
}
fw (i, 0, n) cin >> a[i], a[i]--;
//Find all subtrees that can be splitted. Color them white, everything else black.
memset(par, -1, sizeof par);
dep[0] = 0;
dfs(0, -1);
sparse();
fw (i, 0, k) alllca[i] = -1;
fw (i, 0, n) alllca[a[i]] = getLCA(alllca[a[i]], i);
dfs2(0, -1);
int cnt = 0;
fw (i, 0, n) if (col[i] == 0) cnt++;
cout << cnt / 2;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
51192 KB |
Output is correct |
2 |
Correct |
32 ms |
51192 KB |
Output is correct |
3 |
Correct |
32 ms |
51192 KB |
Output is correct |
4 |
Correct |
33 ms |
51192 KB |
Output is correct |
5 |
Correct |
32 ms |
51320 KB |
Output is correct |
6 |
Correct |
31 ms |
51192 KB |
Output is correct |
7 |
Correct |
37 ms |
51180 KB |
Output is correct |
8 |
Correct |
31 ms |
51192 KB |
Output is correct |
9 |
Incorrect |
32 ms |
51192 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
51192 KB |
Output is correct |
2 |
Correct |
32 ms |
51192 KB |
Output is correct |
3 |
Correct |
32 ms |
51192 KB |
Output is correct |
4 |
Correct |
33 ms |
51192 KB |
Output is correct |
5 |
Correct |
32 ms |
51320 KB |
Output is correct |
6 |
Correct |
31 ms |
51192 KB |
Output is correct |
7 |
Correct |
37 ms |
51180 KB |
Output is correct |
8 |
Correct |
31 ms |
51192 KB |
Output is correct |
9 |
Incorrect |
32 ms |
51192 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
51192 KB |
Output is correct |
2 |
Correct |
32 ms |
51192 KB |
Output is correct |
3 |
Correct |
32 ms |
51192 KB |
Output is correct |
4 |
Correct |
33 ms |
51192 KB |
Output is correct |
5 |
Correct |
32 ms |
51320 KB |
Output is correct |
6 |
Correct |
31 ms |
51192 KB |
Output is correct |
7 |
Correct |
37 ms |
51180 KB |
Output is correct |
8 |
Correct |
31 ms |
51192 KB |
Output is correct |
9 |
Incorrect |
32 ms |
51192 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
56048 KB |
Output is correct |
2 |
Correct |
91 ms |
56432 KB |
Output is correct |
3 |
Incorrect |
32 ms |
51320 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
51192 KB |
Output is correct |
2 |
Correct |
32 ms |
51192 KB |
Output is correct |
3 |
Correct |
32 ms |
51192 KB |
Output is correct |
4 |
Correct |
33 ms |
51192 KB |
Output is correct |
5 |
Correct |
32 ms |
51320 KB |
Output is correct |
6 |
Correct |
31 ms |
51192 KB |
Output is correct |
7 |
Correct |
37 ms |
51180 KB |
Output is correct |
8 |
Correct |
31 ms |
51192 KB |
Output is correct |
9 |
Incorrect |
32 ms |
51192 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |