# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
837649 | Desh03 | Split the Attractions (IOI19_split) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "split.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> f;
DSU(int n) {
f.resize(n, -1);
}
int find(int u) {
return f[u] < 0 ? u : f[u] = find(f[u]);
}
bool unite(int u, int v) {
u = find(u), v = find(v);
if (u == v) return 0;
if (f[u] > f[v]) swap(u, v);
f[u] += f[v];
f[v] = u;
return 1;
}
};
vector<vector<int>> g;
vector<int> sz, par, col;
vector<pair<int, int>> f(3);
int x, w, cnt, cnt2;
void dfs(int u) {
sz[u] = 1;
for (int v : g[u]) {
if (v != par[u]) {
par[v] = u;
dfs(v);
sz[u] += sz[v];
}
}
if (sz[u] >= f[0].first && sz[u] < x) {
x = sz[u];
w = u;
}
}
void dfs2(int u) {
if (cnt < f[0].first) {
cnt++;
col[u] = f[0].second;
} else {
col[u] = f[2].second;
}
for (int v : g[u]) {
if (v != par[u]) {
dfs2(v);
}
}
}
void dfs3(int u) {
if (cnt < f[1].first) {
cnt++;
col[u] = f[1].second;
} else {
col[u] = f[2].second;
}
for (int v : g[u]) {
if (v != w && v != par[u]) {
dfs3(v);
}
}
}
vector<int> find_split(int root) {
w = root, cnt = 0, x = g.size() + 1;
par[root] = root;
dfs(root);
if (w == root) return col;
dfs2(w);
cnt = 0;
dfs3(root);
if (cnt != f[1].second) return vector<int> (n);
return col;
}
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
f[0] = {a, 1}, f[1] = {b, 2}, f[2] = {c, 3}, x = n + 1, cnt = cnt2 = w = 0;
sort(f.begin(), f.end());
g = vector<vector<int>> (n), sz = par = col = vector<int> (n);
DSU ds(n);
for (int i = 0; i < p.size(); i++) {
if (ds.unite(p[i], q[i])) {
g[p[i]].push_back(q[i]);
g[q[i]].push_back(p[i]);
}
}
if (find_split(0) != vector<int> (n)) return col;
return find_split(n - 1);
}