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>
using namespace std;
const int MXN = 100009;
bool vs[MXN];
vector<int> G[MXN], T[MXN];
int N, M, F1 = 1, F2 = 2, F3 = 3, A, B, C, sz[MXN], P[MXN], ans[MXN], cent, U[MXN], S[MXN], I[MXN];
int root(int x) {
if(U[x] == x) return x;
return U[x] = root(U[x]);
}
void merg(int u, int v) {
u = root(u); v = root(v);
if(u != v) U[u] = v, S[v] += S[u];
}
void dfs1(int x) {
vs[x] = 1; sz[x] = 1;
for(auto& it: G[x]) if(!vs[it]) {
dfs1(it);
sz[x] += sz[it];
T[x].push_back(it);
T[it].push_back(x);
}
}
int fnd(int rt, int x) {
int mxi = -1;
for(auto &it: T[x]) {
if(sz[it] > sz[x]) continue;
if(mxi == -1 || sz[mxi] < sz[it]) mxi = it;
}
if(mxi == -1 || sz[mxi] * 2 <= sz[rt]) return x;
return fnd(rt, mxi);
}
void dfs2(int x, int p, int c) {
sz[x] = 1; P[x] = p; I[x] = c;
for(auto& it: T[x]) if(it != p) {
dfs2(it, x, x == p ? ++c : c);
sz[x] += sz[it];
}
}
int lft;
void grd(int x, int p, int bck, int col) {
if(!lft || ans[x]) return;
ans[x] = col; --lft;
if(bck) for(auto& it: G[x]) grd(it, x, bck, col);
else for(auto& it: T[x]) if(it != p) grd(it, x, bck, col);
}
vector<int> makeans(bool fail) {
if(fail) {
vector<int> res(N, 0);
return res;
}
vector<int> res(N, F3);
for(int i=0; i<N; i++) {
if(ans[i] == 1) res[i] = F1;
if(ans[i] == 2) res[i] = F2;
}
return res;
}
void bfs(int st) {
int cnt = 0;
queue<int> que; que.push(st); ans[st] = -1;
while(cnt < A) {
queue<int> Q; Q.push(que.front()); que.pop();
while(Q.size()) {
int x = Q.front(); Q.pop();
ans[x] = 1; ++cnt;
for(auto& it: T[x]) {
if(it == cent) continue;
if(!ans[it]) {
ans[it] = -1;
Q.push(it);
}
}
for(auto& it: G[x]) if(I[x] != I[it]) {
if(it == cent) continue;
if(!ans[it]) {
ans[it] = -1;
que.push(it);
}
}
}
}
for(int i=0; i<N; i++) if(ans[i] == -1) ans[i] = 0;
}
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
if(a > b) swap(a, b), swap(F1, F2);
if(a > c) swap(a, c), swap(F1, F3);
if(b > c) swap(b, c), swap(F2, F3);
A = a; B = b; C = c; N = n; M = p.size();
for(int i=0; i<M; i++) {
G[p[i]].push_back(q[i]);
G[q[i]].push_back(p[i]);
}
dfs1(0);
cent = fnd(0, 0);
dfs2(cent, cent, 0);
for(int i=0; i<N; i++) {
if(i == cent) continue;
if(sz[i] >= A && N - sz[i] >= B) {
lft = A; grd(i, P[i], 0, 1);
lft = B; grd(P[i], P[i], 1, 2);
return makeans(0);
}
}
for(int i=0; i<N; i++) S[i] = 1, U[i] = i;
for(int i=0; i<M; i++) {
if(p[i] == cent || q[i] == cent) continue;
merg(p[i], q[i]);
}
int st = -1;
for(auto& it: T[cent]) if(S[root(it)] >= A) st = it;
if(st == -1) return makeans(1);
bfs(st);
lft = B; grd(cent, cent, 1, 2);
return makeans(0);
}
# | 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... |