제출 #154397

#제출 시각아이디문제언어결과실행 시간메모리
154397rama_pangSplit the Attractions (IOI19_split)C++14
100 / 100
198 ms20720 KiB
#include "split.h" #include <bits/stdc++.h> using namespace std; struct disj { //disjoint set union find data structure vector<int> p, sz; disj(int n) { p.resize(n); iota(p.begin(), p.end(), 0); sz.assign(n, 1); } int par(int n) { return (p[n] == n)? n : p[n] = par(p[n]); } }; void centroid(int n, int p, vector<vector<int>> &G, int &cent, vector<int> &sz_dfs) { //find centroid of tree bool is_centroid = true; for (auto i : G[n]) { if (i == p) continue; centroid(i, n, G, cent, sz_dfs); if (sz_dfs[i] > G.size() / 2) is_centroid = false; sz_dfs[n] += sz_dfs[i]; } if (G.size() - sz_dfs[n] > G.size() / 2) is_centroid = false; if (is_centroid) cent = n; } int find_size(int n, int p, int id, vector<vector<int>> &G, disj &a, vector<int> &sz_dfs) { //find size of subtree a.p[n] = id; int res = 1; for (auto i : G[n]) { if (i == p) continue; sz_dfs[n] += find_size(i, n, id, G, a, sz_dfs); } return sz_dfs[n]; } void dfs_color(int n, int p, int &cnt, int color, vector<vector<int>> &G, vector<int> &res, int except) { //coloring by dfs if (n == except) return; if (cnt > 0) { cnt--; res[n] = color; } for (auto i : G[n]) { if (i == p) continue; if (res[i] != 0) continue; if (cnt <= 0) break; dfs_color(i, n, cnt, color, G, res, except); } } vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) { vector<int> res(n, 0); vector<pair<int, int>> tmp = {{a, 1}, {b, 2}, {c, 3}}; sort(tmp.begin(), tmp.end()); //assume a <= b <= c disj ds(n); vector<pair<int, int>> edge; vector<vector<int>> G(n); vector<int> sz_dfs(n, 1); for (int i = 0; i < p.size(); i++) edge.push_back({p[i], q[i]}); for (auto i : edge) { //create (random) spanning tree int p1 = ds.par(i.first), p2 = ds.par(i.second); if (p1 != p2) { ds.p[p1] = p2; G[i.first].push_back(i.second); G[i.second].push_back(i.first); } } int center; centroid(0, 0, G, center, sz_dfs); //find centroid of spanning tree int max_subtree = 0; vector<int> subtrees; disj anc(n); sz_dfs.assign(n, 1); bool done = false; //check if a is already found int lim = tmp[0].first; //finding and coloring for (auto i : G[center]) { if (done) break; max_subtree = max(max_subtree, sz_dfs[i] = find_size(i, center, i, G, anc, sz_dfs)); anc.sz[i] = sz_dfs[i]; if (max_subtree >= tmp[0].first) { //pick if subtree of centroid has size >= a; b is guaranteed (centroid to all nodes in other subtrees), c is leftover dfs_color(i, center, tmp[0].first, tmp[0].second, G, res, -1); done = true; } } for (auto i : edge) if (!done) { //a <= n / 3 if (i.first == center || i.second == center) continue; int p1 = anc.par(i.first), p2 = anc.par(i.second); //worst case coloring scenario: dfs coloring of a through all child of centroid -> centroid trapped if (p1 == p2) continue; //for worst case scenario, all subtrees < a (since a cannot be chosen from a single subtree) G[i.first].push_back(i.second); G[i.second].push_back(i.first); //a+b+c -(2a-2) - 1(centroid) = b + c - a + 1 = sizeof connected subtrees left from centroid max_subtree = max(max_subtree, anc.sz[p1] + anc.sz[p2]); //assume b (needed size of subtree) > b + c - a + 2 (centroid-connected subtree left) anc.sz[p1] += anc.sz[p2]; anc.sz[p2] = 0; anc.p[p2] = p1; //then a - 2 > c, however a <= c. Contradiction. Thus coloring always works if (max_subtree >= tmp[0].first) { //connect subtrees of spanning tree -> if component has size >= a, then found a dfs_color(p1, center, tmp[0].first, tmp[0].second, G, res, center); done = true; } } dfs_color(center, -1, tmp[1].first, tmp[1].second, G, res, -1); //since centroid isn't used and it is connected to all nodes, it can be used to get b for (int i = 0; i < n; i++) res[i] = (res[i] == 0)? tmp[2].second : res[i]; //leftover for c if (max_subtree < lim) res.assign(n, 0); //if max connected subtree has size < a, then there is no solution return res; }

컴파일 시 표준 에러 (stderr) 메시지

split.cpp: In function 'void centroid(int, int, std::vector<std::vector<int> >&, int&, std::vector<int>&)':
split.cpp:16:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (sz_dfs[i] > G.size() / 2) is_centroid = false;
split.cpp: In function 'int find_size(int, int, int, std::vector<std::vector<int> >&, disj&, std::vector<int>&)':
split.cpp:25:9: warning: unused variable 'res' [-Wunused-variable]
     int res = 1;
         ^~~
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:55:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < p.size(); i++) edge.push_back({p[i], q[i]});
                     ~~^~~~~~~~~~
#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...