#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+5;
int n, k;
vector<int> g[N], st[N];
int par[N][20], dep[N];
void init_lca(int u, int p) {
par[u][0] = p, dep[u] = dep[p] + 1;
for(int i = 1; i < 20; i++) par[u][i] = par[par[u][i-1]][i-1];
for(int v : g[u]) if(v != p) init_lca(v, u);
}
int lca(int a, int b) {
if(dep[a] < dep[b]) swap(a, b);
for(int i = 19; ~i; i--) if(dep[par[a][i]] >= dep[b]) a = par[a][i];
if(a == b) return a;
for(int i = 19; ~i; i--) if(par[a][i] != par[b][i]) a = par[a][i], b = par[b][i];
return par[a][0];
}
int qs[N], col[N], ans;
void solve_qs(int u, int p) {
for(int v : g[u]) if(v != p) {
solve_qs(v, u);
qs[u] += qs[v];
}
}
void flood(int u, int c) {
col[u] = c;
for(int v : g[u]) {
if((!qs[v] && par[v][0] == u) || (par[u][0] == v && !qs[u])) continue;
if(!col[v]) flood(v, c);
}
}
int deg[N];
vector<pair<int, int> > E;
int main() {
scanf("%d %d", &n, &k);
for(int i = 1, a, b; i < n; i++) {
scanf("%d %d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
E.emplace_back(a, b);
}
init_lca(1, 0);
for(int i = 1, a; i <= n; i++) {
scanf("%d", &a);
st[a].emplace_back(i);
}
for(int i = 1; i <= k; i++) for(int j = 1; j < st[i].size(); j++) {
int l = lca(st[i][j-1], st[i][j]);
++qs[st[i][j-1]];
++qs[st[i][j]];
--qs[l];
}
solve_qs(1, 0);
for(int i = 1, ptr = 0; i <= n; i++) if(!col[i]) flood(i, ++ptr);
for(pair<int, int> p : E) {
int a, b; tie(a, b) = p;
if(col[a] != col[b]) ++deg[col[a]], ++deg[col[b]];
}
for(int i = 1; i <= n; i++) if(deg[i] == 1) ++ans;
printf("%d\n", (ans + 1) / 2);
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:59:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i <= k; i++) for(int j = 1; j < st[i].size(); j++) {
~~^~~~~~~~~~~~~~
mergers.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
37768 KB |
Output is correct |
2 |
Correct |
130 ms |
40264 KB |
Output is correct |
3 |
Incorrect |
26 ms |
24312 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |