#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 2e5 + 1;
int a[nmax][22];
int in[nmax], out[nmax];
int timer = 0;
bool is_ancestor(int x, int y){
return in[x] <= in[y] && out[y] <= out[x];
}
vector <vector <int> > g(nmax);
vector <int> sz(nmax);
void dfs(int v, int e = -1){
in[v] = timer++;
a[v][0] = e;
sz[v]++;
for(int to : g[v]){
if(to == e) continue;
dfs(to, v);
sz[v] += sz[to];
}
out[v] = timer++;
}
void prep(int n){
dfs(1);
for(int j = 1; j <= 20; j++)
for(int i = 1; i <= n; i++)
if(a[i][j - 1]) a[i][j] = a[a[i][j - 1]][j - 1];
}
int lca(int u, int v){
for(int j = 20; j >= 0; j--){
if(is_ancestor(a[v][j], u))
continue;
v = a[v][j];
}
return a[v][0];
}
int sum = 0, mx = 0;
vector <int> c(nmax);
vector <int> cnt(nmax);
void DFS(int v, int e = -1){
for(int to : g[v]){
if(to == e) continue;
DFS(to, v);
}
if(c[v] == c[1]){
for(int to : g[v]){
sum += cnt[to];
mx = max(mx, cnt[to]);
}
return;
}
if(!sz[v]){
cnt[v] = 1;
}
else{
for(int to : g[v])
cnt[v] += cnt[to];
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
int k; cin >> k;
vector <vector <int> > vec(k + 1);
for(int i = 1; i < n; ++i){
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1);
for(int i = 1; i <= n; i++){
cin >> c[i];
vec[c[i]].pb(i);
}
for(int i = 1; i <= k; i++){
int l = vec[i][0];
for(int j = 1; j < vec[i].size(); j++)
l = lca(l, vec[i][j]);
sz[l] -= vec[i].size();
}
DFS(1);
if(mx * 2 >= sum)
cout << mx << "\n";
else cout << (sum + 1) / 2 << "\n";
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:98:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int j = 1; j < vec[i].size(); j++)
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
22212 KB |
Output is correct |
2 |
Incorrect |
61 ms |
27468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
7400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |