# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
422683 | oleh1421 | Split the Attractions (IOI19_split) | C++17 | 0 ms | 0 KiB |
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 "grader.cpp"
#include "split.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=200010;
vector<int>g[N];
pair<int,int>gr[3];
int sz[N];
void dfs(int v,int pr){
sz[v]=1;
for (int to:g[v]){
if (to!=pr){
dfs(to,v);
sz[v]+=sz[to];
}
}
}
int ans[N];
void Mark(int v,int ind){
if (!gr[ind].first) return;
gr[ind].first--;
ans[v]=gr[ind].second;
// if (!ind) cout<<"OK "<<v<<" "<<gr[ind].first<<" "<<gr[ind].second<<endl;
for (int to:g[v]){
if (ans[to]) continue;
Mark(to,ind);
}
}
bool DONE=false;
void dfs1(int v,int pr){
if (DONE) return;
vector<pair<int,int> >go;
for (int to:g[v]){
if (to!=pr){
go.push_back({sz[to],to});
}
}
sort(go.begin(),go.end());
reverse(go.begin(),go.end());
if (go[0].first>=gr[0].first && go[0].first<=gr[1].first+gr[2].first){
if (go[0].first<=gr[0].first+gr[2].first){
ans[go[0].second]=gr[0].second;
ans[v]=gr[1].second;
Mark(go[0].second,0);
Mark(v,1);
// for (int i=0;i<7;i++) cout<<ans[i]<<" ";
// cout<<endl;
// cout<<"OK "<<v<<" "<<go[0].second<<endl;
} else {
ans[go[0].second]=gr[1].second;
ans[v]=gr[0].second;
Mark(go[0].second,1);
Mark(v,0);
}
DONE=true;
return;
}
if (go[0].first<gr[0].first) return;
dfs1(go[0].first,v);
}
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
gr[0]={a,1};
gr[1]={b,2};
gr[2]={c,3};
sort(gr,gr+3);
for (int i=0;i<p.size();i++){
g[p[i]].push_back(q[i]);
g[q[i]].push_back(p[i]);
}
if (p.size()==n-1){
dfs(0,-1);
dfs1(0,-1);
if (DONE){
for (int i=0;i<n;i++){
if (!ans[i]) ans[i]=gr[2].second;
}
}
}
vector<int>res;
for (int i=0;i<n;i++) res.push_back(ans[i]);
return res;
}
/**
8 7 2 3 3
0 1
0 2
2 3
2 4
2 5
2 6
2 8
1 2 2 3 3 3 3 3
*/