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);
}
}
}
vector<int> sub;
vector<int> par;
void color (int u, int c) {
if (k[c]) {
--k[c];
ans[u] = c + 1;
}
for (int v : adj[u]) {
if (par[v] == u) {
color(v, c);
}
}
}
void dfs2 (int u, int p = -1) {
sub[u] = 1;
vis[u] = 1;
par[u] = p;
for (int v : adj[u]) {
if (!vis[v]) {
dfs2(v, u);
sub[u] += sub[v];
}
}
for (int i = 0; i < 3; ++i) {
if (sub[u] >= k[i]) {
color(u, i);
sub[u] = 0;
break;
}
}
}
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) {
sub.resize(n);
par.resize(n);
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... |