This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long int
using namespace std;
const int maxn = 1e5 + 100;
int n ,k, a[maxn], num[maxn], sz[maxn], cnt[maxn] ,ans;
bool mark[maxn];
vector<int> adj[maxn];
vector<int> states[maxn];
void dfs1(int v, int mpar = 0){
sz[v] = 1;
states[v][a[v]] = true;
for(auto u : adj[v]){
if(u != mpar){
dfs1(u, v);
sz[v] += sz[u];
cnt[v] += cnt[u];
for(int i = 1; i <= k; i++){
states[v][i] |= states[u][i];
}
}
}
int tedad = 0;
for(int i = 1; i <= k ;i ++){
if(states[v][i]){
tedad += num[i];
}
}
if((v != 1) and (tedad == sz[v])){
mark[v] = true;
cnt[v]++;
}
}
void dfs2(int v ,int mpar = 0){
if(mark[v]){
if(cnt[v] == 1 or cnt[v] == cnt[1]){
ans++;
}
}
for(auto u : adj[v]){
if(u != mpar){
dfs2(u ,v);
}
}
}
int32_t main(){
ios::sync_with_stdio(0) ,cout.tie(0) ,cin.tie(0);
cin >> n >> k;
for(int i = 0 ;i < n - 1 ;i++){
int x ,y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 1; i <= n; i++){
cin >> a[i];
num[a[i]]++;
}
for(int i = 1 ;i <= n; i++){
states[i] = vector<int> (k + 100, 0);
}
dfs1(1), dfs2(1);
cout << (ans + 1) / 2 << endl;
system("pause");
return 0;
}
Compilation message (stderr)
mergers.cpp: In function 'int32_t main()':
mergers.cpp:78:11: warning: ignoring return value of 'int system(const char*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | system("pause");
| ~~~~~~^~~~~~~~~
# | 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... |