#include "beechtree.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int n,m;
vector<vector<int>> pov;
vector<int> beautiful, c;
vector<int> dfs(int u){
int nn = 1 + pov[u].size();
if(nn == 1){return {};}
unordered_set<int> contains;
vector<vector<int>> a(nn);
vector<pair<int,int>> sub(nn);
for(int i = 0; i<nn-1; i++){
int v = pov[u][i];
if(contains.count(c[v])){
beautiful[u] = 0;
}
contains.insert(c[v]);
a[0].push_back(c[v]);
a[i+1] = dfs(v);
sub[i+1] = {-a[i+1].size(), i+1};
if(!beautiful[v]){beautiful[u] = 0;}
}
if(!beautiful[u]){return {};}
sub[0] = {-nn+1, 0};
sort(a[0].begin(), a[0].end());
sort(sub.begin(), sub.end());
for(int i = 1; i<nn;i++){
int l = 0;
int c_i = sub[i].second, prev_i = sub[i-1].second;
for(int j = 0;j<a[c_i].size();j++){
while(a[prev_i][l] < a[c_i][j]){
l++;
if(l >= a[prev_i].size()){
l--;break;
}
}
if(a[prev_i][l] != a[c_i][j]){
beautiful[u] = 0; return {};
}
}
}
return a[0];
}
vector<int> beechtree(int N, int M, vector<int> P, vector<int> C)
{
n = N;
c = C;
pov.resize(n);
unordered_map<int,int> comp;
for(int i = 1;i<n;i++){
pov[P[i]].push_back(i);
if(!comp.count(c[i])){comp[c[i]] = comp.size();}
c[i] = comp[c[i]];
}
m = comp.size();
beautiful.resize(n,1);
dfs(0);
return beautiful;
}
// int main(){
// vector<int> ans = beechtree(4, 2, {-1, 0, 0, 0}, {0, 1, 1, 2});
// for(int i = 0;i<ans.size(); i++){
// cout << ans[i] << ' ';
// }
// cout << '\n';
// ans = beechtree(18, 3, {-1, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 10, 11, 11}, {0, 1, 2, 3, 1, 2, 3, 1, 3, 3, 2, 1, 1, 2, 2, 1, 2, 3});
// for(int i = 0;i<ans.size(); i++){
// cout << ans[i] << ' ';
// }
// cout << '\n';
// vector<int> ans = beechtree(7, 2, {-1, 0, 1, 1, 0, 4, 5}, {0, 1, 1, 2, 2, 1, 1});
// for(int i = 0;i<ans.size(); i++){
// cout << ans[i] << ' ';
// }
// cout << '\n';
// }
Compilation message
beechtree.cpp: In function 'std::vector<int> dfs(int)':
beechtree.cpp:38:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int j = 0;j<a[c_i].size();j++){
| ~^~~~~~~~~~~~~~
beechtree.cpp:41:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(l >= a[prev_i].size()){
| ~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
213 ms |
140472 KB |
Output is correct |
8 |
Correct |
204 ms |
138696 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
1628 KB |
Output is correct |
14 |
Correct |
2 ms |
1624 KB |
Output is correct |
15 |
Correct |
3 ms |
1628 KB |
Output is correct |
16 |
Correct |
2 ms |
1748 KB |
Output is correct |
17 |
Correct |
163 ms |
133944 KB |
Output is correct |
18 |
Correct |
180 ms |
134224 KB |
Output is correct |
19 |
Correct |
196 ms |
138440 KB |
Output is correct |
20 |
Correct |
202 ms |
139520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
436 KB |
Output is correct |
7 |
Incorrect |
1 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
213 ms |
140472 KB |
Output is correct |
6 |
Correct |
204 ms |
138696 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
700 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
95 ms |
28844 KB |
Output is correct |
12 |
Correct |
87 ms |
19264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
432 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
4 |
Halted |
0 ms |
0 KB |
- |