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;
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
int m = p.size();
vector<vector<int>> adj(n);
for (int i = 0; i < m; i++) {
adj[p[i]].push_back(q[i]);
adj[q[i]].push_back(p[i]);
}
vector<int> ans(n);
auto F = [&](int s, int t, int p, int c, int b) {
queue<int> q;
q.push(s);
ans[s] = c;
int cnt = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int y : adj[x]) {
if (y != b && ans[y] == p && cnt < t) {
cnt++;
ans[y] = c;
q.push(y);
}
}
}
};
if (a == 1) {
ans = vector<int>(n, 3);
F(0, b, 3, 2, -1);
for (int i = 0; i < n; i++) {
if (ans[i] == 3) {
ans[i] = 1;
break;
}
}
return ans;
}
if (m == n - 1) {
auto dfs = [&](auto dfs, int x, int p) -> int {
int sz = 1;
for (int y : adj[x]) {
if (y == p) {
continue;
}
int t = dfs(dfs, y, x);
if (t == -1) {
return -1;
}
sz += t;
}
bool u = sz >= a && n - sz >= min(b, c);
bool v = sz >= b && n - sz >= min(a, c);
bool w = sz >= c && n - sz >= min(a, b);
int T[3], S[4] = {0, a, b, c};
if (u || v || w) {
if (u) {
T[0] = 1;
if (n - sz >= b) {
T[1] = 2;
T[2] = 3;
} else {
T[1] = 3;
T[2] = 2;
}
} else if (v) {
T[0] = 2;
if (n - sz >= a) {
T[1] = 1;
T[2] = 3;
} else {
T[1] = 3;
T[2] = 1;
}
} else {
T[0] = 3;
if (n - sz >= a) {
T[1] = 1;
T[2] = 2;
} else {
T[1] = 2;
T[2] = 1;
}
}
ans = vector<int>(n, T[2]);
F(x, S[T[0]], T[2], T[0], p);
F(p, S[T[1]], T[2], T[1], x);
return -1;
}
return sz;
};
if (dfs(dfs, 0, -1) == -1) {
return ans;
} else {
return vector<int>(n);
}
}
ans = vector<int>(n, 3);
int s = 0;
for (int i = 0; i < n; i++) {
if (adj[i].size() == 1) {
s = i;
break;
}
}
F(s, a, 3, 1, -1);
for (int i = 0; i < n; i++) {
if (ans[i] == 3) {
F(i, b, 3, 2, -1);
return ans;
}
}
return vector<int>(n);
}
# | 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... |