이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N=1e5+2;
vector<int> ma[N],ch[N],ans;
bool vis[N];
int n;
int sz[N],up[N],h[N];
pair<int,int> comp[3];
bool f=0;
void df1(int x,int p)
{
vis[x]=1;
if(comp[p].first>0)
{
ans[x]=comp[p].second;
comp[p].first--;
}
for(int y:ch[x])
if(ans[y]==comp[2].second and !vis[y])
df1(y,p);
}
void df2(int x,int p)
{
if(comp[p].first==0)
return;
vis[x]=1;
if(comp[p].first>0)
{
ans[x]=comp[p].second;
comp[p].first--;
}
for(int y:ma[x])
if(!vis[y] and ans[y]==comp[2].second)
df2(y,p);
}
void dfs_ch(int x)
{
vis[x]=1;
for(int y:ma[x])
{
if(!vis[y])
{
dfs_ch(y);
ch[x].push_back(y);
}
}
}
void dfs(int x,int q=-1)
{
sz[x]=1;
bool p=1;
for(int y:ma[x])
if(vis[y])
up[x]=min(up[x],h[y]);
for(int y:ch[x])
{
up[y]=h[y]=h[x]+1;
dfs(y,x);
p&=(sz[y]<comp[0].first);
sz[x]+=sz[y];
up[x]=min(up[x],up[y]);
}
if(p and !f and sz[x]>=comp[0].first)
{
int P1=sz[x];
int P2=n-P1;
vector<int> b;
for(int y:ch[x])
{
if(up[y]<h[x] and (P1-sz[y])>=comp[0].first)
P1-=sz[y],P2+=sz[y];
else
b.push_back(y);
}
if(P2>=comp[1].first)
{
for(int i=0;i<n;i++)
vis[i]=0;
vis[x]=1;
ans[x]=comp[0].second;
comp[0].first--;
for(auto i:b)
df1(i,0);
for(int i=0;i<n;i++)
if(ans[i]==comp[2].second)
{
df2(i,1);
break;
}
f=1;
}
}
}
vector<int> find_split(int n1, int a, int b, int c, vector<int> p, vector<int> q)
{
n=n1;
comp[0]={a,1},comp[1]={b,2},comp[2]={c,3};
sort(comp,comp+3);
int m=p.size();
for(int i=0;i<m;i++)
ma[p[i]].push_back(q[i]),ma[q[i]].push_back(p[i]);
ans.resize(n,comp[2].second);
for(int j=0;j<1;j++)
{
for(int i=0;i<n;i++)
vis[i]=0;
h[j]=0;
dfs_ch(j);
for(int i=0;i<n;i++)
{
ans[i]=comp[2].second;;
vis[i]=0;
}
dfs(j);
if(f)
return ans;
}
if(!f)
for(int i=0;i<n;i++)
ans[i]=0;
return ans;
}
# | 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... |