이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include "split.h"
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
vector<vector<int>> v;
vector<int> nei[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_color(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_color(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;
}
dfs(i,h);
sz[u] += sz[i];
if (mn[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_color(i,c);
}
void dfs2(int u){
seen[u] = true;
int p1 = sz[u];
int p2 = sz[1] - sz[u];
set<int> s;
for (int i : nei[u])
if (!seen[i]){
if (found)
return;
s.insert(i);
dfs2(i);
}
if (p1>=A and p2>=B){
call(u,s,v[0][1],A);
K = B;
dfs_color(1,v[1][1]);
found = true;
return;
}
else if (p1>=B and p2>=A){
call(u,s,v[1][1],B);
K = A;
dfs_color(1,v[0][1]);
found = true;
return;
}
for (int i : nei[u]){
if (seen[i])
continue;
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_color(1,v[1][1]);
found = true;
return;
}
else if (p1>=B and p2>=A){
call(u,s,v[1][1],B);
K = A;
dfs_color(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... |