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 <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
bool used[N];
int subtree[N];
int par[N];
vector<vector<int>> G;
int sz;
vector<int> esim;
void dfs(int v) {
if (used[v]) return;
if ((int)esim.size() == sz) return;
esim.push_back(v);
used[v] = true;
for (auto it : G[v]) {
dfs(it);
}
}
void dfss(int v, int p) {
par[v] = p;
subtree[v] = 1;
for (auto it : G[v]) {
if (it == p) continue;
dfss(it, v);
subtree[v] += subtree[it];
}
}
vector<int> find_split(int n, int A, int B, int C, vector<int> p, vector<int> q) {
int m = (int)p.size();
bool sb1 = true, sb2 = true;
G.resize(n);
for (int i = 0; i < m; i++) {
G[p[i]].push_back(q[i]);
G[q[i]].push_back(p[i]);
}
for (int i = 0; i < n; i++) {
if ((int)G[i].size() > 2) sb1 = false;
}
if (sb1) {
int a = -1, b = -1;
for (int i = 0; i < n; i++) {
if ((int)G[i].size() == 1) {
if (a == -1) a = i;
else b = i;
}
}
if (a == -1) a = 0;
vector<int> v;
v.push_back(a);
v.push_back(G[a][0]);
while ((int)v.size() < n) {
int a = v[(int)v.size() - 1];
int b = v[(int)v.size() - 2];
if (G[a][0] == b) v.push_back(G[a][1]);
else v.push_back(G[a][0]);
}
vector<int> ans(n);
for (int i = 0; i < n; i++) {
if (i < A) ans[v[i]] = 1;
else if (i < A + B) ans[v[i]] = 2;
else ans[v[i]] = 3;
}
return ans;
}
sb2 = (A == 1);
if (sb2) {
sz = B;
dfs(0);
vector<int> ans(n);
bool al = true;
for (int i = 0; i < n; i++) {
if (used[i]) ans[i] = 2;
else {
if (al) {
al = false;
ans[i] = 1;
}
else ans[i] = 3;
}
}
return ans;
}
vector<pair<int, int>> v = { { A, 1 }, { B, 2 }, { C, 3 } };
sort(v.begin(), v.end());
dfss(0, 0);
bool ch = true;
if (ch) {
set<pair<int, int>> candidates;
for (auto it : G[0]) {
candidates.insert({ subtree[it], it });
}
vector<int> f = { 0 };
int cur_sz = 1;
while (cur_sz < v[0].first) {
auto it = candidates.begin();
int v = it->second;
f.push_back(v);
for (auto it : G[v]) {
if (it == par[v]) continue;
candidates.insert({ subtree[it], it });
}
cur_sz++;
}
auto it = candidates.end(); it--;
if (it->first >= v[1].first) {
vector<int> ans(n, 0);
esim.clear();
for (int i = 0; i < n; i++) {
used[i] = false;
}
int w = it->second;
sz = v[1].first;
used[par[w]] = true;
dfs(w);
for (auto& it : f) {
ans[it] = v[0].second;
}
for (auto& it : esim) {
ans[it] = v[1].second;
}
for (auto& it : ans) {
if (it == 0) it = v[2].second;
}
return ans;
}
}
swap(v[0], v[1]);
if (ch) {
set<pair<int, int>> candidates;
for (auto it : G[0]) {
candidates.insert({ subtree[it], it });
}
vector<int> f = { 0 };
int cur_sz = 1;
while (cur_sz < v[0].first) {
auto it = candidates.begin();
int v = it->second;
f.push_back(v);
for (auto it : G[v]) {
if (it == par[v]) continue;
candidates.insert({ subtree[it], it });
}
cur_sz++;
}
auto it = candidates.end(); it--;
if (it->first >= v[1].first) {
vector<int> ans(n, 0);
esim.clear();
for (int i = 0; i < n; i++) {
used[i] = false;
}
int w = it->second;
sz = v[1].first;
used[par[w]] = true;
dfs(w);
for (auto& it : f) {
ans[it] = v[0].second;
}
for (auto& it : esim) {
ans[it] = v[1].second;
}
for (auto& it : ans) {
if (it == 0) it = v[2].second;
}
return ans;
}
}
vector<int> ans(n, 0);
return ans;
}
/*
int main() {
vector<int> p, q;
int n, a, b, c;
cin >> n >> a >> b >> c;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
p.push_back(x);
q.push_back(y);
}
vector<int> qwe = find_split(n, a, b, c, p, q);
for (auto it : qwe) {
cout << it << " ";
}
}
*/
Compilation message (stderr)
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:51:21: warning: variable 'b' set but not used [-Wunused-but-set-variable]
51 | int a = -1, b = -1;
| ^
# | 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... |