#include <bits/stdc++.h>
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 5e5 + 5;
int n, k, x, y, tin[N], tout[N], timer, up[N][21], pref[N], par[N], ans, fl[N], m[N], sz[N], deg[N];
vector < vector <int> > g, vec;
void dfs (int v, int p = 0)
{
par[v] = p;
tin[v] = ++timer;
up[v][0] = p;
for (int i = 1; i < 20; i++)
up[v][i] = up[ up[v][i - 1] ][i - 1];
for (auto to : g[v]){
if (to == p) continue;
dfs(to, v);
}
tout[v] = ++timer;
}
bool upper (int a, int b)
{
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int lca (int a, int b)
{
if (upper(a, b) ) return a;
if (upper(b, a) ) return b;
for (int i = 19; i >= 0; i--){
if (up[a][i] && !upper(up[a][i], b) )
a = up[a][i];
}
return up[a][0];
}
int get (int v){
return v == m[v] ? v : m[v] = get(m[v]);
}
void unite (int a, int b)
{
a = get(a);
b = get(b);
if (a != b){
if (sz[a] > sz[b]) swap(a, b);
sz[b] += sz[a];
m[a] = b;
}
}
void Dfs (int v, int p = 0)
{
for (auto to : g[v]){
if (to == p) continue;
Dfs(to, v);
pref[v] += pref[to];
}
if (pref[v] > 0){
fl[v] = 1;
for (auto to : g[v]){
if (to == p) continue;
}
}
}
main(){
cin >> n >> k;
g.resize(n + 1);
vec.resize(k + 1);
for (int i = 1; i < n; i++){
scanf("%d%d", &x, &y);
g[x].pb(y);
g[y].pb(x);
}
dfs(1);
for (int i = 1; i <= n; i++){
scanf("%d", &x);
vec[x].pb(i);
m[i] = i;
sz[i] = 1;
}
//cout << "a vot i output\n";
for (int i = 1; i <= k; i++){
if (vec[i].empty() ) continue;
int LCA = vec[i][0];
for (int j = 1; j < (int)vec[i].size(); j++){
LCA = lca(LCA, vec[i][j]);
}
for (int j = 0; j < (int)vec[i].size(); j++){
pref[ vec[i][j] ]++;
pref[ LCA ]--;
}
}
Dfs(1);
for (int i = 1; i <= n; i++){
for (auto to : g[i]){
if (get(i) != get(to) )
deg[get(i)]++;
}
}
for (int i = 1; i <= n; i++){
if (get(1) == get(i)) continue;
if (m[i] == i && deg[i] == 1)
ans++;
}
cout << (ans + 1) / 2 << endl;
}
/**
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4
**/
Compilation message
mergers.cpp:79:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
mergers.cpp: In function 'int main()':
mergers.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:94:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
143 ms |
19636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |