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<vector<int>> adj;
vector<bool> vis;
int n, m;
vector<int> ord, k, ans;
void dfs (int u) {
vis[u] = 1;
for (int v : adj[u]) {
if (!vis[v]) {
dfs(v);
}
}
ord.push_back(u);
}
void go (int i) {
while (k[i]) {
--k[i];
ans[ord.back()] = i + 1;
ord.pop_back();
}
}
void split () {
if ((int)ord.size() == k[0] + k[1] + k[2]) {
go(0);
go(1);
go(2);
}
for (int i = 0; i < 3; ++i) {
for (int j = i + 1; j < 3; ++j) {
if (k[i] + k[j] == (int)ord.size()) {
go(i);
go(j);
}
}
}
for (int i = 0; i < 3; ++i) {
if ((int)ord.size() == k[i]) {
go(i);
}
}
}
void color (int u, int c, int p, int f = -1) {
ans[u] = c;
for (int v : adj[u]) {
if (v ^ p && v ^ f) {
color(v, c, u);
}
}
}
int dfs2 (int u, int p = -1) {
int s = 1;
vector<array<int, 2>> szz;
for (int v : adj[u]) {
if (v ^ p) {
szz.push_back({dfs2(v, u), v});
s += szz.back()[0];
}
}
auto i = find(k.begin(), k.end(), s);
if (i != k.end()) {
color(u, i - k.begin() + 1, p);
*i = 0;
if (~p) {
adj[p].erase(find(adj[p].begin(), adj[p].end(), u));
}
return 0;
}
for (auto &v : szz) {
i = find(k.begin(), k.end(), s - v[0]);
if (i != k.end()) {
color(u, i - k.begin() + 1, p, v[1]);
*i = 0;
if (~u) {
adj[p].erase(find(adj[p].begin(), adj[p].end(), u));
adj[p].push_back(v[1]);
}
return 0;
}
}
return s;
}
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
::n = n;
k = {a, b, c};
m = p.size();
adj.resize(n);
ans.resize(n);
for (int i = 0; i < m; ++i) {
adj[p[i]].push_back(q[i]);
adj[q[i]].push_back(p[i]);
}
vis.resize(n);
if (m == n - 1) {
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
dfs2(i);
}
if (!ans[i]) {
ord.push_back(i);
}
}
split();
if (ord.size()) {
return vector<int>(n, 0);
}
return ans;
}
vector<int> cmp[m + 1];
for (int i = 0; i < n; ++i) {
cmp[adj[i].size()].push_back(i);
}
for (int j = 1; j <= m; ++j) {
for (int i : cmp[j]) {
if (!vis[i]) {
dfs(i);
split();
if (ord.size()) {
return vector<int>(n, 0);
}
}
}
}
if (count(k.begin(), k.end(), 0) < 2) {
return vector<int>(n, 0);
}
ord = cmp[0];
split();
if (ord.size()) {
return vector<int>(n, 0);
}
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... |