제출 #610536

#제출 시각아이디문제언어결과실행 시간메모리
610536lorenzoferrariSplit the Attractions (IOI19_split)C++17
18 / 100
87 ms17736 KiB
#include "split.h" #include <bits/stdc++.h> using namespace std; vector<int> solve_tree(int n, array<int, 3> sc, array<int, 3> lb, vector<vector<int>> adj) { vector<int> p(n, -1); vector<int> sz(n, 0); auto dfs = [&](auto&& self, int v) -> int { sz[v] = 1; for (int u : adj[v]) { if (u == p[v]) continue; p[u] = v; sz[v] += self(self, u); } return sz[v]; }; dfs(dfs, 0); vector<int> ans(n, 0); array<int, 3> cnt{}; auto colour = [&](auto&& self, int v, int c, bool risali) -> void { if (ans[v] || cnt[c] == sc[c]) return; ans[v] = lb[c]; ++cnt[c]; for (int u : adj[v]) { if (u == p[v] && !risali) continue; self(self, u, c, risali); } }; for (int i = 0; i < n; ++i) { for (int c = 0; c < 2; ++c) for (int d = 0; d < 2; ++d) { if (c == d) continue; if (sc[c] <= sz[i] && sz[i] <= sc[c] + sc[d]) { colour(colour, i, c, false); colour(colour, p[i], 3-c-d, true); for (int& x : ans) if (!x) x = lb[d]; return ans; } } } return ans; } vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) { array<array<int, 2>, 3> v; v[0] = {a,1}, v[1] = {b,2}, v[2] = {c, 3}; sort(v.begin(), v.end()); array<int, 3> sc{v[0][0], v[1][0], v[2][0]}; array<int, 3> lb{v[0][1], v[1][1], v[2][1]}; vector<vector<int>> adj(n); for (int i = 0; i < (int)p.size(); ++i) { adj[p[i]].push_back(q[i]); adj[q[i]].push_back(p[i]); } if ((int)p.size() == n - 1) { return solve_tree(n, sc, lb, adj); } int min_deg = 0; for (int i = 0; i < n; ++i) { if (adj[i].size() < adj[min_deg].size()) { min_deg = i; } } array<int, 3> cnt{}; vector<int> ans(n, 0); auto dfs = [&](auto&& self, int v) -> void { if (ans[v] != 0) return; for (int i = 0; i < 3; ++i) { if (cnt[i] < sc[i]) { ++cnt[i]; ans[v] = lb[i]; break; } } for (int u : adj[v]) { self(self, u); } }; dfs(dfs, min_deg); return ans; }
#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...