# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1067121 | ZanP | Beech Tree (IOI23_beechtree) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
}
}
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;
f.resize(M);
for (int i = 1; i < v.size(); i++)
{
if(!f[colors[i]].count()) f[colors[i]] = 0;
if(parent[[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
for(int u = 0; u<n; u++)
{
subtree = get_subtree(u);
sort(subtree.begin(), subtree.end());
if(check_permutation(subtree)){
ans[u] = 1;
continue;
}
while(next_permutation(subtree.begin(), subtree.end())){
if(check_permutation(u, subtree)){
ans[u] = 1;
break;
}
}
}
return ans;
}