#include "beechtree.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
using namespace std;
const int N = 2e5 + 10;
vector <vector<pair<int, int>>> G;
vector <pair<int, int>> get_subtree(int v, int c) {
vector <pair<int, int>> cur;
cur.push_back({ v, c });
for (auto it : G[v]) {
for (auto& j : get_subtree(it.first, it.second)) {
cur.push_back(j);
}
}
return cur;
}
bool check(vector <pair<int, int>>& w, vector <int>& P, vector <int>& C) {
map <int, int> mp;
for (int i = 1; i < (int)w.size(); i++) {
if (P[w[i].first] != w[mp[w[i].second]].first) return false;
mp[w[i].second]++;
}
return true;
}
bool cmp(set <int> x, set <int> y) {
return (int)x.size() > (int)y.size();
}
vector <int> beechtree(int N, int M, vector <int> P, vector <int> C) {
bool subtask1 = (N <= 8);
bool subtask2 = true;
bool subtask3 = true;
bool subtask4 = true;
G.clear();
G.resize(N);
map <int, int> mp;
for (int i = 1; i < N; i++) {
if (P[i] != (i - 1)) {
subtask2 = false;
}
G[P[i]].push_back({ i, C[i] });
mp[C[i]]++;
if (P[i] != 0 && P[P[i]] != 0) {
subtask3 = false;
}
}
for (auto it : mp) {
if (it.second > 2) {
subtask4 = false;
}
}
if (subtask1) {
vector <int> ret = {};
for (int i = 0; i < N; i++) {
vector <pair<int, int>> w = get_subtree(i, C[i]);
sort(w.begin() + 1, w.end());
bool ch = false;
if (check(w, P, C)) ch = true;
while (next_permutation(w.begin() + 1, w.end())) {
if (check(w, P, C)) ch = true;
}
ret.push_back(ch);
}
return ret;
}
if (subtask2) {
vector <int> ret = {};
if (N == 1) return { 1 };
int cnt = 2;
for (int i = N - 2; i > 0; i--) {
if (C[i] != C[N - 1]) break;
cnt++;
}
for (int i = 0; i < N - cnt; i++) {
ret.push_back(0);
}
for (int i = 0; i < cnt; i++) {
ret.push_back(1);
}
return ret;
}
if (subtask3) {
bool esim = true;
vector <int> ret(N, 0);
vector <set<int>> sets;
for (int i = N - 1; i >= 1; i--) {
if (G[i].empty()) {
ret[i] = 1;
}
else {
bool ch = true;
set <int> ms;
for (auto it : G[i]) {
if (ms.find(it.second) != ms.end()) {
esim = false;
ch = false;
}
ms.insert(it.second);
}
if (ch) {
ret[i] = 1;
sets.push_back(ms);
}
}
}
if (!esim) return ret;
set <int> ms;
for (auto it : G[0]) {
if (ms.find(it.second) != ms.end()) {
return ret;
}
ms.insert(it.second);
}
sort(sets.begin(), sets.end(), cmp);
for (auto& it : sets) {
for (auto& j : it) {
if (ms.find(j) == ms.end()) {
return ret;
}
}
ms = it;
}
ret[0] = 1;
return ret;
}
if (subtask4) {
vector <int> ret(N, 0);
vector <bool> lvl1(N, 0);
vector <bool> different(N, true);
vector <set<int>> sets(N, set<int>());
for (int v = N - 1; v >= 0; v--) {
if (G[v].empty()) {
ret[v] = 1;
continue;
}
lvl1[v] = true;
for (auto &it : G[v]) {
if (!G[it.first].empty()) lvl1[v] = false;
}
if (lvl1[v]) {
for (auto &it : G[v]) {
if (sets[v].find(it.second) != sets[v].end()) {
different[v] = false;
}
sets[v].insert(it.second);
}
if (different[v]) {
ret[v] = 1;
continue;
}
}
bool lvl2 = true;
for (auto it : G[v]) {
if (!G[it.first].empty() && !lvl1[it.first]) {
lvl2 = false;
}
}
if (!lvl2) continue;
bool ch = true;
for (auto it : G[v]) {
if (!different[it.first]) ch = false;
}
set <int> ms;
for (auto it : G[0]) {
if (ms.find(it.second) != ms.end()) {
ch = false;
}
ms.insert(it.second);
}
vector <set<int>> cur_sets;
for (auto it : G[v]) {
cur_sets.push_back(sets[it.first]);
}
sort(cur_sets.begin(), cur_sets.end(), cmp);
for (auto& it : cur_sets) {
for (auto& j : it) {
if (ms.find(j) == ms.end()) {
ch = false;
}
}
ms = it;
}
ret[v] = ch;
}
}
}
/*
int main() {
for (auto it : beechtree(5, 2, { -1, 0, 1, 2, 3 }, { 0, 1, 2, 2, 2 })) {
cout << it << " ";
}
}
*/
Compilation message
beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:39:17: warning: control reaches end of non-void function [-Wreturn-type]
39 | map <int, int> mp;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
356 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
356 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
356 KB |
Output is correct |
11 |
Correct |
1 ms |
356 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
0 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
1 ms |
360 KB |
Output is correct |
17 |
Correct |
0 ms |
356 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
356 KB |
Output is correct |
21 |
Correct |
1 ms |
356 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
440 KB |
Output is correct |
25 |
Correct |
0 ms |
356 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
352 KB |
Output is correct |
28 |
Correct |
0 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
356 KB |
Output is correct |
30 |
Correct |
0 ms |
352 KB |
Output is correct |
31 |
Correct |
0 ms |
356 KB |
Output is correct |
32 |
Correct |
1 ms |
352 KB |
Output is correct |
33 |
Correct |
1 ms |
356 KB |
Output is correct |
34 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
125 ms |
25044 KB |
Output is correct |
8 |
Correct |
99 ms |
22832 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
392 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
58 ms |
17920 KB |
Output is correct |
18 |
Correct |
48 ms |
18120 KB |
Output is correct |
19 |
Correct |
93 ms |
22304 KB |
Output is correct |
20 |
Correct |
121 ms |
24008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
432 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
91 ms |
11888 KB |
Output is correct |
16 |
Correct |
85 ms |
11316 KB |
Output is correct |
17 |
Correct |
79 ms |
11352 KB |
Output is correct |
18 |
Correct |
95 ms |
11864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
356 KB |
Output is correct |
5 |
Correct |
125 ms |
25044 KB |
Output is correct |
6 |
Correct |
99 ms |
22832 KB |
Output is correct |
7 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
356 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
352 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
0 ms |
356 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
356 KB |
Output is correct |
17 |
Correct |
1 ms |
356 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
440 KB |
Output is correct |
21 |
Correct |
0 ms |
356 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
352 KB |
Output is correct |
24 |
Correct |
0 ms |
352 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
580 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
29 |
Correct |
1 ms |
608 KB |
Output is correct |
30 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
356 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
356 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
352 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
0 ms |
356 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
356 KB |
Output is correct |
17 |
Correct |
1 ms |
356 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
440 KB |
Output is correct |
21 |
Correct |
0 ms |
356 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
352 KB |
Output is correct |
24 |
Correct |
0 ms |
352 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
580 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
29 |
Correct |
1 ms |
608 KB |
Output is correct |
30 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
356 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |