#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
const int N = 500002 , l = 25;
vector<int> adj[N];
int timer;
int tin[N], tout[N];
int up[N][l + 5];
int cnt[N];
void dfs(int v, int p){
tin[v] = ++timer;
up[v][0] = p;
for (int i = 1; i <= l; ++i)
up[v][i] = up[up[v][i-1]][i-1];
for (int u : adj[v]) {
if (u != p)
dfs(u, v);
}
tout[v] = ++timer;
}
bool is_ancestor(int u, int v){
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v){
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int i = l; i >= 0; --i) {
if (!is_ancestor(up[u][i], v))
u = up[u][i];
}
return up[u][0];
}
struct UFDS {
int p[N],siz[N];
void init(int n){
for(int i = 0; i <= n; i++){
p[i] = i;
siz[i] = 1;
}
}
int get(int x){
return p[x]=(x==p[x] ? x : get(p[x]));
}
void unio(int u , int v){
int a = get(u);
int b=get(v);
if(a==b)return;
if(siz[a]>siz[b])swap(a,b);
p[b]=a;
siz[a]+=siz[b];
}
};
UFDS d;
void dfs1(int node , int par = -1){
for(int i : adj[node]){
if(i!=par){
dfs1(i,node);
if(cnt[i] != 0)d.unio(i , node);
cnt[node] += cnt[i];
}
}
}
bool cmp(int x , int y){
return tin[x] < tin[y];
}
void solve(){
int n;
cin >> n;
d.init(n + 2);
int k;
cin >> k;
for(int i = 0; i < n-1; i++){
int a , b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
vector<int> t[k + 1];
for(int i = 0; i < n; i++){
int x;
cin >> x;
t[x].pb(i + 1);
}
memset(cnt,0,sizeof cnt);
dfs(1,1);
for(int i = 1; i <= k; i++){
sort(all(t[i]),cmp);
for(int j = 0; j < (int)t[k].size(); j++){
int lc = lca(t[i][j] , t[i][(j+1)%t[i].size()]);
cnt[lc]-=2;
cnt[t[i][j]]++;
cnt[t[i][(j+1)%t[i].size()]]++;
}
}
dfs1(1,-1);
vector<int> leaf(n+1,0);
for(int i = 1; i <= n; i++){
for(int j : adj[i]){
if(d.get(i) != d.get(j))leaf[i]++,leaf[j]++;
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
if(d.get(i) == i && leaf[i] == 2)ans++;
}
cout << (ans + 1)/2 << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14028 KB |
Output is correct |
2 |
Correct |
6 ms |
13984 KB |
Output is correct |
3 |
Incorrect |
7 ms |
14028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14028 KB |
Output is correct |
2 |
Correct |
6 ms |
13984 KB |
Output is correct |
3 |
Incorrect |
7 ms |
14028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14028 KB |
Output is correct |
2 |
Correct |
6 ms |
13984 KB |
Output is correct |
3 |
Incorrect |
7 ms |
14028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
89 ms |
64996 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14028 KB |
Output is correct |
2 |
Correct |
6 ms |
13984 KB |
Output is correct |
3 |
Incorrect |
7 ms |
14028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |