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;
typedef vector<int> vi;
typedef pair<int,int> ii;
static vi answer;
static vi adj[100005];
static vi child[100005];
static vi tree[100005];
static int sz[100005];
static int pa[100005];
static bool vis[100005];
class UFDS {
public:
vi p, rak, setSize;
int numSets;
void create(int n){
setSize.assign(n, 1);
numSets = n;
rak.assign(n, 0);
p.assign(n, 0);
for(int i =0;i < n;i++){
p[i] = i;
}
}
int findSet(int i){
return (p[i] == i) ? i : (p[i] = findSet(p[i]));
}
bool isSameSet(int i, int j){
return findSet(i) == findSet(j);
}
int unionSet(int i, int j){
if(isSameSet(i,j))
return 0;
numSets--;
int x = findSet(i);
int y = findSet(j);
if(rak[x] > rak[y]) {
p[y] = x; setSize[x] += setSize[y];
}
else {
p[x] = y; setSize[y] += setSize[x];
if(rak[x] == rak[y]) rak[y]++;
}
return max(setSize[x],setSize[y]);
}
};
void dfs(int u){
vis[u] = true;
sz[u] = 1;
for(int v : adj[u]){
if(!vis[v]){
child[u].push_back(v);
tree[u].push_back(v);
tree[v].push_back(u);
pa[v] = u;
dfs(v);
sz[u] += sz[v];
}
}
}
void bfs(int root, int number){
if(number == 0) return;
queue<int> bf;
bf.push(root);
while(!bf.empty()){
int u = bf.front(); bf.pop();
for(int v : tree[u]){
if(answer[v] == 0){
answer[v] = answer[u];
number--; ///We fill only the required number of nodes
if(number == 0) return;
bf.push(v);
}
}
}
}
vi find_split(int n, int aaa, int bbb, int ccc, vi p, vi q) {
///WLOG, let A <= B <= C. A <= N/3, B <= N/2
vector<ii> v = {ii(aaa,1),ii(bbb,2),ii(ccc,3)};
sort(v.begin(),v.end());
int A = v[0].first;
int B = v[1].first;
int AColour = v[0].second;
int BColour = v[1].second;
int CColour = v[2].second;
answer.assign(n,0);
for(int i = 0;i < (int) p.size();i++){
int x = p[i], y = q[i];
adj[y].push_back(x);
adj[x].push_back(y);
}
///Generate an arbitrary spanning tree T
dfs(0);
///Check if there exists an edge which splits T into 2 parts which fit both A and B
for(int i = 0;i < (int) p.size();i++){
int x = p[i], y = q[i];
if(sz[y] < sz[x]) swap(x,y); ///x is child of y
int l = sz[x], r = n - sz[x];
if(l > r) swap(l,r), swap(x,y);
///A split is found, bfs to find nodes to put in set A and set B
if(l >= A && r >= B){
answer[x] = AColour;
answer[y] = BColour;
bfs(x,A-1);
bfs(y,B-1);
for(int i = 0;i < n;i++){
if(answer[i] == 0) answer[i] = CColour;
}
return answer;
}
}
///There must exist only one vertex R such that all of it's subtrees have a size smaller than A
int R = -1;
for(int u = 0;u < n;u++){
bool can = true;
for(int v : child[u]){
if(sz[v] >= A){
can = false;
break;
}
}
if(n - sz[u] >= A) can = false;
if(can){
R = u;
break;
}
}
assert(R != -1);
///For all edges in our spanning tree not connected to R, we merge them using UFDS
UFDS uf;
uf.create(n);
for(int i = 0;i < (int) p.size();i++){
int u = p[i]; int v = q[i];
if(u == R || v == R) continue;
if(pa[u] == v || pa[v] == u){
uf.unionSet(u,v);
}
}
///For all edges not in the spanning tree not connected to R, we merge them using UFDS
///If this creates a connected component not connected to R that is at least size A, we use that for set A
///The other set is connected via the Root R.
for(int i = 0;i < (int) p.size();i++){
int u = p[i]; int v = q[i];
if(u == R || v == R) continue;
if(pa[u] == v || pa[v] == u) continue;
tree[u].push_back(v);
tree[v].push_back(u);
///A split is found, find the stuff to put in set A and set B using bfs
if(uf.unionSet(u,v) >= A){ ///uf.unionSet returns the new set size of that set
int x = u;
int y = R;
answer[x] = AColour;
answer[y] = BColour;
bfs(x,A-1);
bfs(y,B-1);
for(int i = 0;i < n;i++){
if(answer[i] == 0) answer[i] = CColour;
}
return answer;
}
}
///There is no answer. We have exhausted all edges and we're left with a graph with root R, and subtrees of size < A.
///To make a set greater than A, we need to pass through R, which means we can't make sets for B.
return answer;
}
| # | 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... |