Submission #417428

#TimeUsernameProblemLanguageResultExecution timeMemory
417428shivensinha4Mergers (JOI19_mergers)C++17
0 / 100
76 ms17760 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); vector<bool> lone(n); int ans = 0; function<void(int,int)> dfs = [&](int p, int parent) { int badchild = 0; if (ct[st[p]] != 1) mp[p][st[p]] = 1; for (auto i: adj[p]) if (i != parent) { dfs(i, p); lone[p] = lone[p] or lone[i]; if (mp[i].size() == 0 and !lone[i]) badchild++; 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); } // cout << p << ": " << mp[p] << endl; ans += (badchild+1)/2; lone[p] = lone[p] or (badchild%2); }; dfs(0, 0); cout << ans << 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...