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;
const int nax = 1e5 + 3;
//subtask 1&3
vector<int>g[nax];
int m, n;
int sub[nax];
bool taken[nax];
int dfs(int v, int p) {
sub[v] = 1;
for(int u : g[v]) if(u!=p) {
sub[v] += dfs(u, v);
}
return sub[v];
}
int get_centroid(int v, int p) {
for(int u : g[v]) if(u!=p) {
if(sub[u]*2>n) {
return get_centroid(u, v);
}
}
return v;
}
vector<int> find_split(int N, int A, int B, int C,
vector<int> p, vector<int> q) {
n = N;
vector<int>sz = {A, B, C}, ord = {1, 2, 3};
sort(ord.begin(), ord.end(), [&] (int i, int j) {
return sz[i] < sz[j];
});
m = p.size();
for(int i=0; i<m; ++i) {
g[p[i]].push_back(q[i]);
g[q[i]].push_back(p[i]);
}
dfs(0, 0);
int ct = get_centroid(0, 0);
dfs(ct, ct);
int bc = -1;
for(int u : g[ct]) {
if(sub[u]>=sz[ord[0]]) {
bc = u;
break;
}
}
if(bc==-1) {
return vector<int>(n, 0);
}
taken[ct] = 1;
vector<int>g1;
queue<int>qu;
qu.push(bc);
while(g1.size()<sz[ord[0]]) {
int v = qu.front();
qu.pop();
if(taken[v]) continue;
taken[v] = 1;
g1.push_back(v);
for(int u : g[v]) if(!taken[u]) {
qu.push(u);
}
}
taken[ct] = 0;
while(!qu.empty()) qu.pop();
qu.push(ct);
vector<int>g2;
while(g2.size()<sz[ord[1]]) {
int v = qu.front();
qu.pop();
if(taken[v]) continue;
taken[v] = 1;
g2.push_back(v);
for(int u : g[v]) if(!taken[u]) {
qu.push(u);
}
}
vector<int>res(n, 0);
for(int x : g1) {
res[x] = ord[0];
}
for(int x : g2) {
res[x] = ord[1];
}
for(int i=0; i<n; ++i) if(res[i]==0) {
res[i] = ord[2];
}
return res;
}
Compilation message (stderr)
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:68:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
68 | while(g1.size()<sz[ord[0]]) {
split.cpp:85:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
85 | while(g2.size()<sz[ord[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... |