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 ll long long
#define fast cin.tie(0);cout.tie(0);
#define order ios::sync_with_stdio(0);ios_base::sync_with_stdio(0);
#define pb push_back
using namespace std;
int rv[3],num[3],n;
vector<int>v[200009];
int vis[200009],col[200009];
void dfs(int x,int par,int id)
{
if(num[id]==0)id++;
if(vis[x])return;
vis[x]=1;
col[x]=rv[id];
num[id]--;
for(int i=0;i<v[x].size();i++)
{
int y=v[x][i];
if(y==par)continue;
dfs(y,x,id);
}
}
int sz[200009];
pair<int,int>ed={-1,-1};
vector<int> res;
int pa[200009];
void dfss(int x,int par)
{
pa[x]=par;
sz[x]=1;
for(int i=0;i<v[x].size();i++)
{
int y=v[x][i];
if(y==par)continue;
dfss(y,x);
sz[x]+=sz[y];
}
}
void dfs1(int x,int par,int id)
{
if(num[id]==0)id=2;
res[x]=id;
num[id]--;
for(int i=0;i<v[x].size();i++)
{
int y=v[x][i];
if(y==par)continue;
dfs1(y,x,id);
}
}
void bld(int x)
{
if(sz[x]<n-sz[x])
{
dfs1(x,pa[x],0);
dfs1(pa[x],x,1);
}
else
{
dfs1(x,pa[x],1);
dfs1(pa[x],x,0);
}
}
void solve()
{
dfss(0,0);
for(int i=0;i<n;i++)res.push_back(0);
for(int i=0;i<n;i++)
{
int s1=sz[i],s2=n-sz[i];
if(s1>s2)swap(s1,s2);
if(s1>=num[0]&&s2>=num[1])
{
bld(i);
for(i=0;i<n;i++)res[i]=rv[res[i]];
break;
}
}
}
vector<int> find_split(int nn, int a, int b, int c, vector<int> p, vector<int> q)
{
n=nn;
num[0]=a;
num[1]=b;
num[2]=c;
int mx=max(a,max(b,c));
int mn=min(a,min(b,c));
for(int i=0;i<3;i++)
{
if(num[i]==mx)
{
//cout<<i<<" ";
rv[2]=i+1;
if(num[(i+1)%3]==mn){rv[0]=(i+1)%3+1;rv[1]=(i+2)%3+1;}
else {rv[1]=(i+1)%3+1;rv[0]=(i+2)%3+1;}
break;
}
}
sort(num,num+3);
for(int i=0;i<p.size();i++)
{
v[p[i]].pb(q[i]);
v[q[i]].pb(p[i]);
}
if(p.size()==n-1)
{
solve();return res;
}
int rt=0;
for(int i=0;i<n;i++)
{
if(v[i].size()==1)
{
rt=i;
break;
}
}
dfs(rt,rt,0);
for(int i=0;i<n;i++)res.push_back(col[i]);
//cout<<rv[0]<<" "<<rv[1]<<" "<<rv[2]<<"\n";
return res;
}
Compilation message (stderr)
split.cpp: In function 'void dfs(int, int, int)':
split.cpp:18:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
split.cpp: In function 'void dfss(int, int)':
split.cpp:33:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i=0;i<v[x].size();i++)
| ~^~~~~~~~~~~~
split.cpp: In function 'void dfs1(int, int, 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<v[x].size();i++)
| ~^~~~~~~~~~~~
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<p.size();i++)
| ~^~~~~~~~~
split.cpp:107:16: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
107 | if(p.size()==n-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... |