#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii ;
typedef pair<ll, ll> pll ;
typedef vector<pii> vii ;
typedef vector<int> veci ;
typedef vector<pll> vll ;
typedef vector<ll> vecll;
// find_by_order order_of_key
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define F first
#define S second
#define pb push_back
#define endl '\n'
#define Mp make_pair
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r" , stdin) ; freopen("out.txt" , "w" , stdout);
#define lb lower_bound
#define ub upper_bound
#define for1(n) for(int i=1;i<=n;i++)
#define for0(n) for(int i=0;i<n;i++)
#define forn(n) for(int i=n;i>0;i--)
#define pq priority_queue <pii, vector<pii>, greater<pii>>
const int N=5e5+3;
int A[N],n,m,k,q,sz[N],is[N],cnt[N],e;
vector<int>g[N];
map<int,int>B[N];
void dfs(int v,int p=0){
for(int u:g[v])if(u!=p){
dfs(u,v);
sz[v]+=sz[u];
if(B[u].size()>B[v].size())
swap(B[u],B[v]);
for(pii p:B[u]){
B[v][p.F]+=p.S;
if(B[v][p.F]==cnt[p.F])
B[v].erase(p.F);
}
}
B[v][A[v]]++;
if(B[v][A[v]]==cnt[A[v]])B[v].erase(A[v]);
if(v!=1)
is[v]=(B[v].size()==0);
sz[v]+=is[v];
}
int main(){
fast_io
cin>>n>>k;
for1(n-1){
int u,v;cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
for1(n){
cin>>A[i];
cnt[A[i]]++;
}
dfs(1);
for1(n)if(is[i]){
if(sz[i]==1 || sz[i]==sz[1])
e++;
}
cout<<(e+1)/2<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |