제출 #610582

#제출 시각아이디문제언어결과실행 시간메모리
610582lorenzoferrariSplit the Attractions (IOI19_split)C++17
40 / 100
111 ms17780 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) { assert(sc[0] <= sc[1] && sc[1] <= sc[2]); 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); assert(sz[0] == n); vector<int> ans(n, 0); array<int, 3> cnt{}; auto colour = [&](auto&& self, int v, int c, bool risali) -> void { if (ans[v] != 0 || 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) { if (sc[c] <= sz[i] && sz[i] <= sc[c] + sc[2]) { colour(colour, i, c, false); colour(colour, p[i], c^1, true); for (int& x : ans) if (!x) x = lb[2]; assert(cnt[0] == sc[0] && cnt[1] == sc[1]); 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...