# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
569969 |
2022-05-28T11:35:14 Z |
Doxeno |
Mergers (JOI19_mergers) |
C++17 |
|
0 ms |
0 KB |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500010;
vector<int> adj[MAXN];
vector<int> col[MAXN];
int c[MAXN];
int d[MAXN];
int st[MAXN][20];
bool vis[MAXN];
int mc[MAXN];
int og[MAXN];
int ans;
void dd(int n){
vis[n] = 1;
for(auto x: adj[n]){
if(vis[x])continue;
st[x][0] = n;
d[x] = d[n]+1;
dd(x);
}
for(int i = 1; i < 20; i++){
st[n][i] = st[st[n][i-1]][i-1];
}
}
int lca(int a, int b){
if(d[a] < d[b])swap(a,b);
for(int i = 0; i < 20; i++){
if((d[a]-d[b])&(1<<i))a = st[a][i];
}
for(int i = 19; i >= 0; i--){
if(st[a][i] != st[b][i]){
a = st[a][i];
b = st[b][i];
}
}
return st[a][0];
}
int dfs(int n){
vis[n] = 1;
int k = 0;
og[n] = mc[c[n]];
for(auto x: adj[n]){
if(vis[x])continue;
if(dfs(x))k++;
og[n] = min(og[n],og[x]);
}
ans+=k/2;
return(((n!=0) && (og[n] == d[n]) || (k&1));
}
int main(){
int N,K,a,b;
cin >> N >> K;
for(int i = 0; i < N-1; i++){
cin >> a >> b;
adj[--a].push_back(--b);
adj[b].push_back(a);
}
for(int i = 0; i < N; i++){
cin >> c[i]; c[i]--;
col[c[i]].push_back(i);
}
dd(0);
for(int i = 0; i < N; i++)vis[i] = 0;
for(int i = 0; i < K; i++){
if(col[i].empty())continue;
mc[i] = col[i][0];
for(int j = 1; j < col[i].size(); j++){
mc[i] = lca(mc[i],col[i][j]);
}
mc[i] = d[mc[i]];
}
cout << ans+dfs(0) << endl;
}
Compilation message
mergers.cpp: In function 'int dfs(int)':
mergers.cpp:50:20: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
50 | return(((n!=0) && (og[n] == d[n]) || (k&1));
| ~~~~~~~^~~~~~~~~~~~~~~~~~
mergers.cpp:50:48: error: expected ')' before ';' token
50 | return(((n!=0) && (og[n] == d[n]) || (k&1));
| ~ ^
| )
mergers.cpp: In function 'int main()':
mergers.cpp:70:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int j = 1; j < col[i].size(); j++){
| ~~^~~~~~~~~~~~~~~