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>
#define pii pair<int, int>
using namespace std;
vector<int>adj[100005];
int par[100005], sum[100005], ans[100005];
pii A, B;
bool done=false;
int cnter, bad, n, head=-1;
void go(int x, int p, int v){
if(ans[x]) return;
if(cnter){
cnter--;
ans[x]=v;
}
for(int y:adj[x]){
if(y==p) continue;
go(y, x, v);
}
}
void dfs(int x, int p){
par[x]=p;
sum[x]=1;
vector<pair<int, int>>cur;
for(int y:adj[x]){
if(y==p) continue;
dfs(y, x);
sum[x]+=sum[y];
cur.push_back({sum[y], y});
}
cur.push_back({n-sum[x], p});
if(done) return;
for(auto [x, y]: cur){
if(x>=A.first && n-x>=B.first){
head=y;
break;
}
if(x>=B.first && n-x>=A.first){
head=y;
swap(A, B);
break;
}
}
//A is baby
if(head==-1) return;
cnter=A.first;
go(head, x, A.second);
cnter=B.first;
go(x, -1, B.second);
for(int i=0;i<n;i++){
if(!ans[i]) ans[i]=bad;
}
done=true;
}
vector<int> find_split(int _n, int a, int b, int c, vector<int> p, vector<int> q) {
n=_n;
assert((int) p.size()==n-1);
for(int i=0;i<p.size();i++){
int x=p[i], y=q[i];
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<pii>v={{a, 1}, {b, 2}, {c, 3}};
sort(v.begin(), v.end());
A=v[0], B=v[1], bad=v[2].second;
dfs(0, -1);
vector<int>fans;
for(int i=0;i<n;i++){
fans.push_back(ans[i]);
}
return fans;
}
Compilation message (stderr)
split.cpp: In function 'void dfs(int, int)':
split.cpp:33:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for(auto [x, y]: cur){
| ^
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:58:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=0;i<p.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... |