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;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 1e9+7;
struct DSU {
vector<int> par, sz;
DSU() {}
DSU(int n): par(n) {
iota(par.begin(), par.end(), 0);
sz.assign(n, 1);
}
int find_set(int v) {
return v == par[v] ? v : par[v] = find_set(par[v]);
}
bool union_sets(int a, int b) {
if ((a = find_set(a)) == (b = find_set(b))) return false;
if (sz[a] < sz[b]) swap(a, b);
par[b] = a, sz[a] += sz[b], sz[b] = 0;
return true;
}
} d;
int n, m, sub[N], par[N];
vector<int> adj[N];
int dfs_sub(int c, int p) {
sub[c] = 1, par[c] = p;
for (int nxt : adj[c]) if (nxt != p) {
sub[c] += dfs_sub(nxt, c);
}
return sub[c];
}
bool vis[N];
vector<int> gather(int root, int p, int cnt) {
assert(dfs_sub(root, p) >= cnt);
memset(vis, 0, sizeof(vis));
vector<int> q{root}; vis[root] = vis[p] = 1;
for (int rep = 0; rep < sz(q); rep++) {
int c = q[rep];
for (int nxt : adj[c]) if (!vis[nxt]) {
q.push_back(nxt);
vis[nxt] = 1;
}
}
// assert(sz(q) >= cnt);
q.resize(cnt);
// assert(sz(q) == cnt);
return q;
}
vector<int> find_split(int _n, int p1, int p2, int p3, vector<int> p, vector<int> q) {
n = _n, m = sz(p);
assert(m == n-1);
for (int i = 0; i < m; i++) {
int x = p[i], y = q[i];
adj[x].push_back(y), adj[y].push_back(x);
}
vector<pair<int, int>> cols(3);
cols[0] = {p1, 1}, cols[1] = {p2, 2}, cols[2] = {p3, 3};
sort(cols.begin(), cols.end());
dfs_sub(0, -1);
pair<int, int> cut{-1, -1};
for (int c = 1; c < n; c++) {
if (sub[c] >= cols[0].first && n - sub[c] >= cols[1].second) {
cut = {c, par[c]};
}
if (n - sub[c] >= cols[0].first && sub[c] >= cols[1].second) {
cut = {par[c], c};
}
}
if (cut == make_pair(-1, -1)) return vector<int>(n, 0);
vector<int> one = gather(cut.first, cut.second, cols[0].first);
vector<int> two = gather(cut.second, cut.first, cols[1].first);
// cout << sz(one) << ' ' << sz(two) << endl;
vector<int> ans(n, cols[2].second);
for (int x : one) ans[x] = cols[0].second;
for (int x : two) ans[x] = cols[1].second;
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |