Submission #417470

#TimeUsernameProblemLanguageResultExecution timeMemory
417470shivensinha4Mergers (JOI19_mergers)C++17
100 / 100
1295 ms152372 KiB
#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); 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]; deg[i] = 0; } if (mp[i].size() > mp[p].size()) mp[p].swap(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 (deg[i] == 1) odd++; cout << (odd+1)/2 << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...