#include "beechtree.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define ll long long
int n, m;
vector<int> parents, colors;
vector<vector<int>> children;
void dfs_subtree(int u, vector<int> & subtree)
{
subtree.push_back(u);
for(int v : children[u]){
dfs_subtree(v, subtree);
}
}
vector<int> get_subtree(int u)
{
vector<int> subtree;
subtree.reserve(n);
dfs_subtree(u, subtree);
return subtree;
}
bool check_permutation(int u, vector<int> & v){
if(v[0] != u) return false;
unordered_map<int, int> f;
for (int i = 1; i < v.size(); i++)
{
if(!f.count(colors[i])) f[colors[i]] = 0;
if(parents[v[i]] != v[f[colors[i]]]) return false;
f[colors[i]]++;
}
return true;
}
vector<int> beechtree(int N, int M, vector<int> P, vector<int> C){
n = N; m = M; parents = P; colors = C;
children.resize(N);
vector<int> ans;
ans.resize(N,0);
for(int child = 1; child < N; child++)
{
children[P[child]].push_back(child);
}
// brute force
vector<int> subtree;
for(int u = 0; u<n; u++)
{
subtree = get_subtree(u);
sort(subtree.begin(), subtree.end());
if(check_permutation(u, subtree)){
ans[u] = 1;
continue;
}
while(next_permutation(subtree.begin(), subtree.end())){
if(check_permutation(u, subtree)){
ans[u] = 1;
break;
}
}
}
return ans;
}
// 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";
// return 0;
// }
Compilation message
beechtree.cpp: In function 'bool check_permutation(int, std::vector<int>&)':
beechtree.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 1; i < v.size(); i++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2053 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
2nd lines differ - on the 2nd token, expected: '1', found: '0' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
2nd lines differ - on the 2nd token, expected: '1', found: '0' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2057 ms |
348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Execution timed out |
2067 ms |
29380 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2053 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
2nd lines differ - on the 5th token, expected: '1', found: '0' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2053 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
2nd lines differ - on the 5th token, expected: '1', found: '0' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2053 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |