이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "split.h"
#include <bits/stdc++.h>
using namespace std;
int numNodes;
vector<int> ans;
vector<int> edges1;
vector<int> edges2;
vector<vector<int>> neigh;
vector<int> par;
vector<int> stSize;
void initTree(int n, int p) {
par[n] = p;
stSize[n] = 1;
for (int i = 0; i < (int)neigh[n].size(); i++) {
if (neigh[n][i] != p) {
initTree(neigh[n][i], n);
stSize[n] += stSize[neigh[n][i]];
}
}
}
int remFill;
vector<bool> fillVis;
void fillDown(int n, int v) {
if (remFill == 0)
return;
remFill--;
ans[n] = 0;
fillVis[n] = true;
for (int c : neigh[n]) {
if (c != par[n]) {
fillDown(c, v);
}
}
}
void fill(int n, int v) {
if (remFill == 0 || fillVis[n])
return;
fillVis[n] = true;
remFill--;
ans[n] = 1;
for (int c : neigh[n]) {
fill(c, v);
}
}
bool solveTree(int a, int b, int c) {
stSize.resize(numNodes);
par.resize(numNodes);
fillVis.resize(numNodes, false);
initTree(0, -1);
int aRoot = -1;
int bRoot = -1;
for (int i = 0; i < numNodes; i++) {
int numUp = numNodes - stSize[i];
if (stSize[i] >= a && numUp >= b) {
aRoot = i;
break;
}
if (stSize[i] >= b && numUp >= a) {
bRoot = i;
break;
}
}
fill(ans.begin(), ans.end(), 2);
if (aRoot != -1) {
remFill = a;
fillDown(aRoot, 0);
remFill = b;
fill(par[aRoot], 1);
return true;
}
if (bRoot != -1) {
remFill = b;
fillDown(bRoot, 1);
remFill = a;
fill(par[bRoot], 0);
return true;
}
return false;
}
vector<int> find_split(int _numNodes, int a, int b, int c, vector<int> p, vector<int> q) {
edges1 = move(p);
edges2 = move(q);
numNodes = _numNodes;
ans.resize(numNodes);
int numEdges = (int)edges1.size();
if (numEdges == numNodes) {
p.pop_back();
q.pop_back();
numEdges--;
}
neigh.resize(numNodes);
for (int i = 0; i < (int)edges1.size(); i++) {
neigh[edges1[i]].push_back(edges2[i]);
neigh[edges2[i]].push_back(edges1[i]);
}
pair<int, int> abc[] = { { a, 1 }, { b, 2 }, { c, 3 } };
sort(abc, abc + 3);
bool can = false;
if (numEdges == numNodes - 1) {
can = solveTree(abc[0].first, abc[1].first, abc[2].first);
} else {
exit(1);
}
if (!can) {
fill(ans.begin(), ans.end(), 0);
} else {
for (int& i : ans) {
i = abc[i].second;
}
}
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... |