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>
#include"split.h"
using namespace std;
vector<int>g[100005];
int ans[100005],b;
pair<int,int>p[3];
int sz[100005],n;
int fir(int v,int p){
for(int to:g[v])if(to!=p)return fir(to,v);
return v;
}
void dfs(int v){
if(!b)return;
b--;
ans[v]=2;
for(int to:g[v])if(!ans[to])dfs(to);
}
int calc(int v,int p){
sz[v]=1;
for(int to:g[v])if(to!=p)sz[v]+=calc(to,v);
return sz[v];
}
int dfs(int v,int p){
for(int to:g[v])if(to!=p)
if(sz[to]>(n>>1))return dfs(to,v);
return v;
}
bool cmp(int l,int r){
return sz[l]>sz[r];
}
vector<int>zero(int n){
vector<int>ans;
while(n--)ans.push_back(0);
return ans;
}
void go(int v,int par,int cnt,int val){
if(!p[cnt].first)return;
ans[v]=val;
p[cnt].first--;
for(int to:g[v])if(to!=par)
go(to,v,cnt,val);
}
vector<int>find_split(int N,int a,int B,int c,vector<int>p1,vector<int>p2){
b=B;n=N;
if(a==1){
for(int i=0;i<p1.size();i++){
g[p1[i]].push_back(p2[i]);
g[p2[i]].push_back(p1[i]);
}
dfs(0);
vector<int>res;
for(int i=0;i<n;i++){
if(!ans[i]){
if(a){
a--;
ans[i]=1;
}else ans[i]=3;
}
res.push_back(ans[i]);
}
return res;}else if((int)p1.size()==n-1){
p[0]={a,1};
p[1]={b,2};
p[2]={c,3};
sort(p,p+3);
for(int i=0;i<p1.size();i++){
g[p1[i]].push_back(p2[i]);
g[p2[i]].push_back(p1[i]);
}
calc(1,-1);
int root=dfs(1,-1);
calc(root,-1);
sort(g[root].begin(),g[root].end(),cmp);
if(sz[g[root][0]]<p[0].first)return zero(n);
go(g[root][0],root,0,p[0].second);
p[1].first--;
ans[root]=p[1].second;
for(int i=1;i<g[root].size();i++)go(g[root][i],root,1,p[1].second);
vector<int>vec;
for(int i=0;i<n;i++){
if(!ans[i])ans[i]=p[2].second;
vec.push_back(ans[i]);
}
return vec;
}else{
int root=0;
for(int i=0;i<p1.size();i++){
g[p1[i]].push_back(p2[i]);
g[p2[i]].push_back(p1[i]);
}
if(p1.size()!=n)root=fir(root,-1);
while(a--){
ans[root]=1;
for(int to:g[root])if(!ans[to])root=to;
}
while(b--){
ans[root]=2;
for(int to:g[root])if(!ans[to])root=to;
}
while(c--){
ans[root]=3;
for(int to:g[root])if(!ans[to])root=to;
}
vector<int>res;
for(int i=0;i<n;i++)res.push_back(ans[i]);
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:46:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i=0;i<p1.size();i++){
| ~^~~~~~~~~~
split.cpp:67:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i=0;i<p1.size();i++){
| ~^~~~~~~~~~
split.cpp:79:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i=1;i<g[root].size();i++)go(g[root][i],root,1,p[1].second);
| ~^~~~~~~~~~~~~~~
split.cpp:88:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int i=0;i<p1.size();i++){
| ~^~~~~~~~~~
split.cpp:92:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
92 | if(p1.size()!=n)root=fir(root,-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... |