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 <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <cassert>
#include "split.h"
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
vector<vector<int>> v;
vector<int> nei[N],ch[N];
int A,B;
int mn[N];
int sz[N];
int hei[N];
bool seen[N];
int color[N];
bool found = false;
int K;
void dfs_color1(int u,int c){
if (seen[u])
return;
if (K<=0)
return;
color[u] = c;
seen[u] = true;
K--;
for (int i : nei[u]){
if (seen[i])
continue;
dfs_color1(i,c);
}
}
void dfs_color2(int u,int c){
if (seen[u])
return;
if (K<=0)
return;
color[u] = c;
seen[u] = true;
K--;
for (int i : ch[u]){
if (seen[i])
continue;
dfs_color2(i,c);
}
}
void dfs(int u,int h = 1){
sz[u] = 1;
hei[u] = h++;
seen[u] = true;
mn[u] = hei[u];
for (int i : nei[u]){
if (seen[i]){
mn[u] = min(mn[u],hei[i]);
continue;
}
ch[u].push_back(i);
dfs(i,h);
sz[u] += sz[i];
mn[u] = min(mn[u],mn[i]);
}
}
// #################################### reset seen
bool called = false;
void call(int u,set<int> s,int c,int Sz){
if (called)
return;
called = true;
memset(seen,false,sizeof seen);
seen[u] = true;
color[u] = c;
Sz--;
K = Sz;
for (int i : s)
dfs_color2(i,c);
}
void dfs2(int u){
int p1 = sz[u];
int p2 = sz[1] - sz[u];
set<int> s;
for (int i : ch[u]){
s.insert(i);
dfs2(i);
if (found)
return;
}
if (p1>=A and p2>=B){
call(u,s,v[0][1],A);
K = B;
dfs_color1(1,v[1][1]);
found = true;
return;
}
else if (p1>=B and p2>=A){
call(u,s,v[1][1],B);
K = A;
dfs_color1(1,v[0][1]);
found = true;
return;
}
for (int i : ch[u]){
if (found)
return;
if (p1 - sz[i] >= A and mn[i] < hei[u]){
s.erase(i);
p1 -= sz[i];
p2 += sz[i];
}
if (p1>=A and p2>=B){
call(u,s,v[0][1],A);
K = B;
dfs_color1(1,v[1][1]);
found = true;
return;
}
if (p1>=B and p2>=A){
call(u,s,v[1][1],B);
K = A;
dfs_color1(1,v[0][1]);
found = true;
return;
}
}
}
vector<int> find_split(int n,int a,int b,int c,vector<int> p,vector<int> q){
int m = p.size();
for (int i=0;i<m;i++){
p[i]++;
q[i]++;
nei[p[i]].push_back(q[i]);
nei[q[i]].push_back(p[i]);
}
v.push_back({a,1});
v.push_back({b,2});
v.push_back({c,3});
sort(begin(v),end(v));
A = v[0][0];
B = v[1][0];
dfs(1,1);
memset(seen,false,sizeof seen);
dfs2(1);
vector<int> col;
if (!found){
for (int i=1;i<=n;i++)
col.push_back(0);
return col;
}
for (int i=1;i<=n;i++){
if (color[i]==0)
col.push_back(v[2][1]);
else
col.push_back(color[i]);
}
return col;
}
# | 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... |