#include <bits/stdc++.h>
#ifdef mlocal
#include "./Competitive-Code-Library/snippets/prettyprint.h"
#endif
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k; cin >> n >> k;
vector<vi> adj(n);
for_(i, 0, n-1) {
int a, b; cin >> a >> b;
a -= 1; b -= 1;
adj[a].push_back(b); adj[b].push_back(a);
}
vi st(n), ct(n);
for_(i, 0, n) {
cin >> st[i];
st[i] -= 1;
ct[st[i]]++;
}
vector<map<int, int>> mp(n);
vi deg(n);
vector<bool> rem(n);
int ans = 0, oc = 0;
function<void(int,int)> dfs = [&](int p, int parent) {
if (ct[st[p]] != 1) mp[p][st[p]] = 1;
for (auto i: adj[p]) if (i != parent) {
dfs(i, p);
if (!mp[i].size()) {
deg[p]++;
deg[i]++;
}
else {
deg[p] += deg[i];
rem[i] = true;
}
if (mp[i].size() > mp[p].size()) swap(mp[p], mp[i]);
vi rem;
for (auto &v: mp[i]) {
mp[p][v.first] += v.second;
if (mp[p][v.first] == ct[v.first]) rem.push_back(v.first);
}
for (auto v: rem) mp[p].erase(v);
}
};
dfs(0, 0);
int odd = 0;
for_(i, 0, n) if (!rem[i]) odd++;
cout << (odd+1)/2 << endl;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:39:6: warning: unused variable 'ans' [-Wunused-variable]
39 | int ans = 0, oc = 0;
| ^~~
mergers.cpp:39:15: warning: unused variable 'oc' [-Wunused-variable]
39 | int ans = 0, oc = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
73 ms |
16676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |