#include <bits/stdc++.h>
#define vi vector < int >
#include "split.h"
using namespace std;
const int N = 2e5 + 10;
int in[N], out[N], sub[N], P[N];
int countA, countB, countC, aa = 1, bb = 2, cc = 3;
bool vis[N];
int timer = 0;
vi edges[N], redges[N], ans;
void dfs(int x, int p) {
if(vis[x]) return;
vis[x] = sub[x] = 1;
for(int to : edges[x]) {
if(to != p) {
if(!vis[to]) {
P[to] = x;
dfs(to, x);
sub[x] += sub[to];
redges[to].push_back(x);
redges[x].push_back(to);
}
}
}
}
void reroot(int x, int p) {
in[x] = ++timer;
sub[x] = 1;
for(int to : redges[x]) {
if(to != p) {
reroot(to, x);
sub[x] += sub[to];
}
}
out[x] = ++timer;
}
void find_answer(int x, int p, int v) {
if(in[v] <= in[x] && out[v] >= out[x]) {
if(countA > 0) {
ans[x] = aa;
--countA;
}
} else {
if(countB > 0) {
ans[x] = bb;
--countB;
}
}
for(int to : redges[x]) {
if(to != p) {
find_answer(to, x, v);
}
}
}
int find_best(int x, int p) {
int a = countA, c = countC;
for(int to : redges[x]) {
if(to != p) {
if(sub[to] >= a && sub[to] <= c + a) {
return x;
}
} else {
if(sub[0] - sub[x] >= a && sub[0] - sub[x] <= c + a) {
return x;
}
}
}
for(int to : redges[x]) {
if(to != p && sub[to] > c + a) {
return find_best(to, x);
}
}
return -1;
}
vi find_split(int n, int a, int b, int c, vi p, vi q) {
ans.resize(n);
if(a > b) swap(a, b), swap(aa, bb);
if(b > c) swap(b, c), swap(bb, cc);
if(a > b) swap(a, b), swap(aa, bb);
countA = a, countB = b, countC = c;
for(int i = 0; i < q.size(); ++i) {
edges[q[i]].push_back(p[i]);
edges[p[i]].push_back(q[i]);
}
dfs(0, -1);
int root = find_best(0, -1), from = -1;
if(root == -1) return ans;
reroot(root, -1);
for(int x : redges[root]) {
if(sub[x] >= a && sub[x] <= c + a) {
from = x;
}
}
find_answer(root, -1, from);
for(int i = 0; i < n; ++i) if(ans[i] == 0) ans[i] = cc;
return ans;
}
Compilation message
split.cpp: In function 'void dfs(int, int)':
split.cpp:13:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
13 | vis[x] = sub[x] = 1;
| ~~~~~~~^~~
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:81:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i = 0; i < q.size(); ++i) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
ok, correct split |
2 |
Correct |
7 ms |
9728 KB |
ok, correct split |
3 |
Correct |
7 ms |
9728 KB |
ok, correct split |
4 |
Correct |
7 ms |
9728 KB |
ok, correct split |
5 |
Correct |
7 ms |
9728 KB |
ok, correct split |
6 |
Correct |
8 ms |
9728 KB |
ok, correct split |
7 |
Correct |
142 ms |
25976 KB |
ok, correct split |
8 |
Correct |
164 ms |
24184 KB |
ok, correct split |
9 |
Correct |
140 ms |
23544 KB |
ok, correct split |
10 |
Correct |
141 ms |
26232 KB |
ok, correct split |
11 |
Correct |
140 ms |
26236 KB |
ok, correct split |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9728 KB |
ok, correct split |
2 |
Correct |
7 ms |
9768 KB |
ok, correct split |
3 |
Correct |
7 ms |
9728 KB |
ok, correct split |
4 |
Correct |
172 ms |
23928 KB |
ok, correct split |
5 |
Correct |
128 ms |
20216 KB |
ok, correct split |
6 |
Correct |
137 ms |
26232 KB |
ok, correct split |
7 |
Correct |
149 ms |
24060 KB |
ok, correct split |
8 |
Correct |
180 ms |
22008 KB |
ok, correct split |
9 |
Correct |
182 ms |
20092 KB |
ok, correct split |
10 |
Correct |
92 ms |
20848 KB |
ok, correct split |
11 |
Correct |
99 ms |
20848 KB |
ok, correct split |
12 |
Correct |
104 ms |
20852 KB |
ok, correct split |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
ok, correct split |
2 |
Correct |
177 ms |
20216 KB |
ok, correct split |
3 |
Correct |
45 ms |
13944 KB |
ok, correct split |
4 |
Correct |
7 ms |
9728 KB |
ok, correct split |
5 |
Correct |
180 ms |
22064 KB |
ok, correct split |
6 |
Correct |
136 ms |
21880 KB |
ok, correct split |
7 |
Correct |
152 ms |
21880 KB |
ok, correct split |
8 |
Correct |
146 ms |
22808 KB |
ok, correct split |
9 |
Correct |
167 ms |
21880 KB |
ok, correct split |
10 |
Correct |
39 ms |
12928 KB |
ok, no valid answer |
11 |
Correct |
55 ms |
14584 KB |
ok, no valid answer |
12 |
Correct |
99 ms |
19748 KB |
ok, no valid answer |
13 |
Correct |
119 ms |
19576 KB |
ok, no valid answer |
14 |
Correct |
89 ms |
19952 KB |
ok, no valid answer |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
ok, correct split |
2 |
Correct |
7 ms |
9728 KB |
ok, no valid answer |
3 |
Correct |
7 ms |
9728 KB |
ok, correct split |
4 |
Correct |
9 ms |
9728 KB |
ok, correct split |
5 |
Correct |
7 ms |
9856 KB |
ok, correct split |
6 |
Correct |
8 ms |
9728 KB |
ok, correct split |
7 |
Correct |
7 ms |
9728 KB |
ok, correct split |
8 |
Correct |
8 ms |
9728 KB |
ok, correct split |
9 |
Correct |
9 ms |
10112 KB |
ok, correct split |
10 |
Correct |
13 ms |
9984 KB |
ok, correct split |
11 |
Correct |
9 ms |
9728 KB |
ok, correct split |
12 |
Correct |
12 ms |
9984 KB |
ok, correct split |
13 |
Correct |
7 ms |
9728 KB |
ok, correct split |
14 |
Correct |
8 ms |
9728 KB |
ok, correct split |
15 |
Correct |
7 ms |
9728 KB |
ok, correct split |
16 |
Correct |
7 ms |
9728 KB |
ok, correct split |
17 |
Correct |
8 ms |
9728 KB |
ok, correct split |
18 |
Correct |
7 ms |
9728 KB |
ok, correct split |
19 |
Correct |
8 ms |
9856 KB |
ok, correct split |
20 |
Correct |
8 ms |
9856 KB |
ok, correct split |
21 |
Correct |
9 ms |
9984 KB |
ok, correct split |
22 |
Correct |
9 ms |
9984 KB |
ok, correct split |
23 |
Correct |
9 ms |
10112 KB |
ok, correct split |
24 |
Correct |
9 ms |
9984 KB |
ok, correct split |
25 |
Correct |
9 ms |
9984 KB |
ok, correct split |
26 |
Incorrect |
9 ms |
9984 KB |
jury found a solution, contestant did not |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
ok, correct split |
2 |
Correct |
7 ms |
9728 KB |
ok, correct split |
3 |
Correct |
7 ms |
9728 KB |
ok, correct split |
4 |
Correct |
7 ms |
9728 KB |
ok, correct split |
5 |
Correct |
7 ms |
9728 KB |
ok, correct split |
6 |
Correct |
8 ms |
9728 KB |
ok, correct split |
7 |
Correct |
142 ms |
25976 KB |
ok, correct split |
8 |
Correct |
164 ms |
24184 KB |
ok, correct split |
9 |
Correct |
140 ms |
23544 KB |
ok, correct split |
10 |
Correct |
141 ms |
26232 KB |
ok, correct split |
11 |
Correct |
140 ms |
26236 KB |
ok, correct split |
12 |
Correct |
8 ms |
9728 KB |
ok, correct split |
13 |
Correct |
7 ms |
9768 KB |
ok, correct split |
14 |
Correct |
7 ms |
9728 KB |
ok, correct split |
15 |
Correct |
172 ms |
23928 KB |
ok, correct split |
16 |
Correct |
128 ms |
20216 KB |
ok, correct split |
17 |
Correct |
137 ms |
26232 KB |
ok, correct split |
18 |
Correct |
149 ms |
24060 KB |
ok, correct split |
19 |
Correct |
180 ms |
22008 KB |
ok, correct split |
20 |
Correct |
182 ms |
20092 KB |
ok, correct split |
21 |
Correct |
92 ms |
20848 KB |
ok, correct split |
22 |
Correct |
99 ms |
20848 KB |
ok, correct split |
23 |
Correct |
104 ms |
20852 KB |
ok, correct split |
24 |
Correct |
7 ms |
9728 KB |
ok, correct split |
25 |
Correct |
177 ms |
20216 KB |
ok, correct split |
26 |
Correct |
45 ms |
13944 KB |
ok, correct split |
27 |
Correct |
7 ms |
9728 KB |
ok, correct split |
28 |
Correct |
180 ms |
22064 KB |
ok, correct split |
29 |
Correct |
136 ms |
21880 KB |
ok, correct split |
30 |
Correct |
152 ms |
21880 KB |
ok, correct split |
31 |
Correct |
146 ms |
22808 KB |
ok, correct split |
32 |
Correct |
167 ms |
21880 KB |
ok, correct split |
33 |
Correct |
39 ms |
12928 KB |
ok, no valid answer |
34 |
Correct |
55 ms |
14584 KB |
ok, no valid answer |
35 |
Correct |
99 ms |
19748 KB |
ok, no valid answer |
36 |
Correct |
119 ms |
19576 KB |
ok, no valid answer |
37 |
Correct |
89 ms |
19952 KB |
ok, no valid answer |
38 |
Correct |
7 ms |
9728 KB |
ok, correct split |
39 |
Correct |
7 ms |
9728 KB |
ok, no valid answer |
40 |
Correct |
7 ms |
9728 KB |
ok, correct split |
41 |
Correct |
9 ms |
9728 KB |
ok, correct split |
42 |
Correct |
7 ms |
9856 KB |
ok, correct split |
43 |
Correct |
8 ms |
9728 KB |
ok, correct split |
44 |
Correct |
7 ms |
9728 KB |
ok, correct split |
45 |
Correct |
8 ms |
9728 KB |
ok, correct split |
46 |
Correct |
9 ms |
10112 KB |
ok, correct split |
47 |
Correct |
13 ms |
9984 KB |
ok, correct split |
48 |
Correct |
9 ms |
9728 KB |
ok, correct split |
49 |
Correct |
12 ms |
9984 KB |
ok, correct split |
50 |
Correct |
7 ms |
9728 KB |
ok, correct split |
51 |
Correct |
8 ms |
9728 KB |
ok, correct split |
52 |
Correct |
7 ms |
9728 KB |
ok, correct split |
53 |
Correct |
7 ms |
9728 KB |
ok, correct split |
54 |
Correct |
8 ms |
9728 KB |
ok, correct split |
55 |
Correct |
7 ms |
9728 KB |
ok, correct split |
56 |
Correct |
8 ms |
9856 KB |
ok, correct split |
57 |
Correct |
8 ms |
9856 KB |
ok, correct split |
58 |
Correct |
9 ms |
9984 KB |
ok, correct split |
59 |
Correct |
9 ms |
9984 KB |
ok, correct split |
60 |
Correct |
9 ms |
10112 KB |
ok, correct split |
61 |
Correct |
9 ms |
9984 KB |
ok, correct split |
62 |
Correct |
9 ms |
9984 KB |
ok, correct split |
63 |
Incorrect |
9 ms |
9984 KB |
jury found a solution, contestant did not |
64 |
Halted |
0 ms |
0 KB |
- |