#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 2e5 + 5, nbit = 18;
int n, k;
vector<int> g[nmax];
#define lsb(x) (x & -x)
namespace AIB {
int tree[nmax], len;
void init(int nlen) {
len = nlen;
}
void upd(int x, int val) {
while(x > 0) tree[x] += val, x -= lsb(x);
}
int query(int x) {
int sum = 0;
while(x <= n) sum += tree[x], x += lsb(x);
return sum;
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
}
int pin[nmax], pout[nmax], inp = 1;
namespace LCA {
int p[nmax][nbit];
void init(int node, int f) {
pin[node] = inp++;
p[node][0] = f;
for(int i = 1; i < nbit; i++)
p[node][i] = p[p[node][i - 1]][i - 1];
for(auto x : g[node])
if(x == f) continue;
else init(x, node);
pout[node] = inp++;
}
bool isanc(int x, int y) {return pin[x] <= pin[y] && pout[y] <= pout[x]; }
int lca(int x, int y) {
if(isanc(x, y)) return x;
if(isanc(y, x)) return y;
for(int i = nbit - 1; i >= 0; i--)
if(!isanc(p[x][i], y))
x = p[x][i];
return p[x][0];
}
};
vector<int> ofcol[nmax];
int lca[nmax];
int start[nmax], fin[nmax];
int cnt;
vector<int> cuts;
int identify(int node, int f) {
int sum = start[node] + fin[node];
for(auto x : g[node]) {
if(x == f) continue;
int tmp = identify(x, node);
if(tmp == 0)
cuts.push_back(x);
else
sum += tmp;
}
return sum;
}
signed main() {
cin >> n >> k;
for(int i = 1, x, y; i < n; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
LCA::init(1, 1);
AIB::init(inp);
for(int i = 1, x; i <= n; i++) {
cin >> x;
ofcol[x].push_back(i);
}
for(int i = 1; i <= k; i++) {
lca[i] = ofcol[i].back();
for(auto x : ofcol[i])
lca[i] = LCA::lca(x, lca[i]);
for(auto x : ofcol[i])
start[x]++,
fin[lca[i]]--;
}
//for(int i = 1; i <= n; i++)
//cerr << pin[i] << ' ' << pout[i] << '\n';
identify(1, 1);
sort(all(cuts), [&](int a, int b) {return pout[a] < pout[b]; });
int total = cuts.size();
//for(auto x : cuts) {
//if(AIB::query(pin[x], pout[x]) == 0)
//AIB::upd(pin[x], 1), total++;
//}
cout << (total + 1) / 2 << '\n';
}
/**
De-atâtea nopți aud plouând,
Aud materia plângând..
Sînt singur, și mă duce un gând
Spre locuințele lacustre.
Și parcă dorm pe scânduri ude,
În spate mă izbește-un val --
Tresar prin somn și mi se pare
Că n-am tras podul de la mal.
Un gol istoric se întinde,
Pe-același vremuri mă găsesc..
Și simt cum de atâta ploaie
Pilonii grei se prăbușesc.
De-atâtea nopți aud plouând,
Tot tresărind, tot așteptând..
Sînt singur, și mă duce-un gând
Spre locuințele lacustre.
-- George Bacovia, Lacustră
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9724 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9700 KB |
Output is correct |
9 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9724 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9700 KB |
Output is correct |
9 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9724 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9700 KB |
Output is correct |
9 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
21920 KB |
Output is correct |
2 |
Correct |
138 ms |
26048 KB |
Output is correct |
3 |
Incorrect |
11 ms |
10200 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9724 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9700 KB |
Output is correct |
9 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |