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 <bits/stdc++.h>
#define vi vector < int >
#include "split.h"
using namespace std;
const int N = 2e5 + 10;
int in[N], out[N], sub[N], P[N];
int countA, countB, countC, aa = 1, bb = 2, cc = 3;
bool vis[N];
int timer = 0;
vi edges[N], redges[N], ans;
void dfs(int x, int p) {
if(vis[x]) return;
vis[x] = sub[x] = 1;
for(int to : edges[x]) {
if(to != p) {
if(!vis[to]) {
P[to] = x;
dfs(to, x);
sub[x] += sub[to];
redges[to].push_back(x);
redges[x].push_back(to);
}
}
}
}
void reroot(int x, int p) {
in[x] = ++timer;
sub[x] = 1;
for(int to : redges[x]) {
if(to != p) {
reroot(to, x);
sub[x] += sub[to];
}
}
out[x] = ++timer;
}
void find_answer(int x, int p, int v) {
if(in[v] <= in[x] && out[v] >= out[x]) {
if(countA > 0) {
ans[x] = aa;
--countA;
}
} else {
if(countB > 0) {
ans[x] = bb;
--countB;
}
}
for(int to : redges[x]) {
if(to != p) {
find_answer(to, x, v);
}
}
}
int find_centroid(int x, int p) {
for(int to : redges[x]) {
if(to != p && sub[to] * 2 > sub[0]) {
return find_centroid(to, x);
}
}
return x;
}
vi find_split(int n, int a, int b, int c, vi p, vi q) {
ans.resize(n);
if(a > b) swap(a, b), swap(aa, bb);
if(b > c) swap(b, c), swap(bb, cc);
if(a > b) swap(a, b), swap(aa, bb);
countA = a, countB = b, countC = c;
for(int i = 0; i < q.size(); ++i) {
edges[q[i]].push_back(p[i]);
edges[p[i]].push_back(q[i]);
}
dfs(0, -1);
int root = find_centroid(0, -1), from = -1;
reroot(root, -1);
for(int i = 0; i < n; ++i) {
if(sub[i] >= a && sub[i] <= c + a) {
from = i;
}
}
if(from == -1) return ans;
find_answer(root, -1, from);
for(int i = 0; i < n; ++i) if(ans[i] == 0) ans[i] = cc;
return ans;
}
Compilation message (stderr)
split.cpp: In function 'void dfs(int, int)':
split.cpp:13:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
13 | vis[x] = sub[x] = 1;
| ~~~~~~~^~~
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:69:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i = 0; i < q.size(); ++i) {
| ~~^~~~~~~~~~
# | 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... |