# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
923918 | 2024-02-08T06:25:03 Z | boris_mihov | Mergers (JOI19_mergers) | C++17 | 32 ms | 24532 KB |
#include <algorithm> #include <iostream> #include <numeric> #include <cassert> #include <vector> #include <bitset> #include <random> #include <queue> #include <set> typedef long long llong; const int MAXN = 500000 + 10; const int INTINF = 2e9; const llong INF = 1e18; const int MAXLOG = 17; int n, k; int s[MAXN]; int sz[MAXN]; int cnt[MAXN]; int full[MAXN]; int szBad[MAXN]; bool isBad[MAXN]; std::vector <int> g[MAXN]; void buildDFS(int node, int par) { for (int &u : g[node]) { if (u == par) { std::swap(u, g[node].back()); g[node].pop_back(); break; } } sz[node] = 1; for (const int &u : g[node]) { if (u == par) { continue; } buildDFS(u, node); sz[node] += sz[u]; } for (int i = 1 ; i < g[node].size() ; ++i) { if (sz[g[node][i]] > sz[g[node][0]]) { std::swap(g[node][i], g[node][0]); } } } int cntUnfull; void addNode(int node) { cntUnfull -= (0 < cnt[s[node]] && cnt[s[node]] < full[s[node]]); cnt[s[node]]++; cntUnfull += (0 < cnt[s[node]] && cnt[s[node]] < full[s[node]]); } void remNode(int node) { cntUnfull -= (0 < cnt[s[node]] && cnt[s[node]] < full[s[node]]); cnt[s[node]]--; cntUnfull += (0 < cnt[s[node]] && cnt[s[node]] < full[s[node]]); } void addDFS(int node) { addNode(node); for (const int &u : g[node]) { addDFS(u); } } void remDFS(int node) { remNode(node); for (const int &u : g[node]) { addDFS(u); } } void solveDFS(int node) { for (int i = 1 ; i < g[node].size() ; ++i) { solveDFS(g[node][i]); remDFS(g[node][i]); } if (g[node].size()) { solveDFS(g[node][0]); } for (int i = 1 ; i < g[node].size() ; ++i) { addDFS(g[node][i]); } addNode(node); if (cntUnfull == 0) { isBad[node] = true; } } int ans; void calcDFS(int node) { szBad[node] = isBad[node]; for (const int &u : g[node]) { calcDFS(u); szBad[node] += szBad[u]; } } void ansDFS(int node) { for (const int &u : g[node]) { if (szBad[u] > szBad[1] / 2) { ans += isBad[node]; ansDFS(u); return; } } ans += szBad[node] / 2; ans += szBad[node] % 2; } void solve() { buildDFS(1, 0); solveDFS(1); isBad[1] = false; calcDFS(1); ansDFS(1); // for (int i = 2 ; i <= n ; ++i) // { // std::cout << isBad[i] << '\n'; // } std::cout << szBad[1] / 2 + (szBad[1] % 2) << '\n'; } void input() { std::cin >> n >> k; for (int i = 1 ; i < n ; ++i) { int u, v; std::cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1 ; i <= n ; ++i) { std::cin >> s[i]; full[s[i]]++; } } void fastIOI() { std::ios_base :: sync_with_stdio(0); std::cout.tie(nullptr); std::cin.tie(nullptr); } int main() { fastIOI(); input(); solve(); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 19032 KB | Output is correct |
2 | Incorrect | 3 ms | 19032 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 19032 KB | Output is correct |
2 | Incorrect | 3 ms | 19032 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 19032 KB | Output is correct |
2 | Incorrect | 3 ms | 19032 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 32 ms | 22484 KB | Output is correct |
2 | Correct | 32 ms | 24532 KB | Output is correct |
3 | Incorrect | 5 ms | 19036 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 19032 KB | Output is correct |
2 | Incorrect | 3 ms | 19032 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |