#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<>,
rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
#define ll long long
#define ii pair<ll,ll>
#define iii tuple<ll,ll,ll>
#ifndef DEBUG
#define cerr if (0) cerr
#define endl '\n'
#endif
const ll inf=1e15;
const ll maxn=5e5+5;
const ll mod=1e9+7;
mt19937 rng(69);
vector<ll> adj[maxn];
vector<ll> type[maxn];
ll h[maxn];
ll dp[maxn];
ll pre[maxn],post[maxn],pref[maxn];
ll TIME,cnt;
vector<ll> check;
void dfs(ll x, ll p=0){
pre[x]=++TIME;
dp[x]=h[x];
for (auto u:adj[x]){
if (u==p) continue;
dfs(u,x);
dp[x]^=dp[u];
}
post[x]=TIME;
cerr<<dp[x]<<' '<<x<<endl;
if (x!=1 && dp[x]==0){
pref[pre[x]]++;
pref[post[x]+1]--;
check.emplace_back(x);
cnt++;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,k,a,b;
cin>>n>>k;
for (int i=1;i<n;i++){
cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
for (int i=1;i<=n;i++){
cin>>a;
type[a].emplace_back(i);
}
for (int i=1;i<=k;i++){
ll tot=0;
for (int j=0;j<type[i].size()-1;j++){
h[type[i][j]]=rng();
tot^=h[type[i][j]];
}
h[type[i].back()]=tot;
}
dfs(1);
for (int i=1;i<maxn;i++) pref[i]+=pref[i-1];
ll ans=0;
for (auto u:check){
ll res=pref[post[u]]-pref[pre[u]-1]; //number of bad edges in subtree
if (res==1 || res==cnt) ans++;
}
cout<<(ans+1)/2;
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:66:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int j=0;j<type[i].size()-1;j++){
| ~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
35164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
35164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
35164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
46296 KB |
Output is correct |
2 |
Incorrect |
52 ms |
49860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
35164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |