#include <bits/stdc++.h>
#include "split.h"
using namespace std;
const int N = 2e5 + 10;
int in[N], out[N], sub[N];
int timer, countA, countB, countC;
#define vi vector < int >
int dfs(int x, int p, vector < vi > &edges) {
in[x] = ++timer;
sub[x] = 1;
for(int to : edges[x]) {
if(to != p) {
sub[x] += dfs(to, x, edges);
}
}
out[x] = ++timer;
return sub[x];
}
int find_good(int x, int p, vector < vi > &edges) {
int n = edges.size();
for(int to : edges[x]) {
if(to != p && sub[to] >= countA && sub[to] <= countA + countC) {
return x;
}
}
for(int to : edges[x]) {
if(to != p && sub[to] > countA + countC) {
return find_good(to, x, edges);
}
}
return -1;
}
void find_children(int x, int p, int v, vi &ans, vector < vi > &edges) {
if(countA > 0 && in[x] >= in[v] && out[v] >= out[x]) {
ans[x] = 1;
--countA;
}
for(int to : edges[x]) {
if(to != p) {
find_children(to, x, v, ans, edges);
}
}
}
vi find_split(int n, int a, int b, int c, vi p, vi q) {
vector < vi > edges(n, vi(0, 0));
vi ans(n);
if(p.size() != n - 1) return ans;
for(int i = 0; i < n - 1; ++i) {
edges[p[i]].push_back(q[i]);
edges[q[i]].push_back(p[i]);
}
if(a > b) swap(a, b);
if(b > c) swap(b, c);
if(a > b) swap(a, b);
countA = a, countB = b, countC = c;
timer = 0; dfs(0, -1, edges);
int good = find_good(0, -1, edges); if(good == -1) return ans;
timer = 0; dfs(good, -1, edges);
int subTree;
for(int i = 0; i < n; ++i) {
if(sub[i] >= a && sub[i] <= a + c) {
subTree = i;
}
}
find_children(good, -1, subTree, ans, edges);
for(int i = 0; i < n; ++i) {
if(ans[i] == 0) {
if(!(in[subTree] <= in[i] && out[subTree] >= out[i]) && countB > 0) {
ans[i] = 2;
--countB;
}
}
}
for(int i = 0; i < n; ++i) {
if(ans[i] == 0) {
ans[i] = 3;
}
}
return ans;
}
Compilation message
split.cpp: In function 'int find_good(int, int, std::vector<std::vector<int> >&)':
split.cpp:20:9: warning: unused variable 'n' [-Wunused-variable]
20 | int n = edges.size();
| ^
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:47:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
47 | if(p.size() != n - 1) return ans;
| ~~~~~~~~~^~~~~~~~
split.cpp:65:18: warning: 'subTree' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | find_children(good, -1, subTree, ans, edges);
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
ok, correct split |
2 |
Incorrect |
1 ms |
384 KB |
jury found a solution, contestant did not |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
ok, correct split |
2 |
Incorrect |
0 ms |
384 KB |
jury found a solution, contestant did not |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
invalid split: #1=1, #2=2, #3=2 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
jury found a solution, contestant did not |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
ok, correct split |
2 |
Incorrect |
1 ms |
384 KB |
jury found a solution, contestant did not |
3 |
Halted |
0 ms |
0 KB |
- |