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 <bits/stdc++.h>
using namespace std;
std::vector<int> beechtree(int N, int M, std::vector<int> P, std::vector<int> C){
vector<multiset<pair<int, int>>> c(N);
vector<map<int, int>> childs(N);
vector<vector<int>> g(N);
vector<int> ans(N, 1), sz(N, 1);
for(int i=1; i<N; i++){
g[P[i]].push_back(i);
}
auto check = [&](auto a, auto b){
for(auto [k, v]:childs[b]){
if(childs[a][k] < v){
return false;
}
}
return true;
};
auto add = [&](auto p, auto u){
bool ok = 1;
auto it = c[u].insert(p);
if(next(it)!=c[u].end()){
ok&=check(next(it)->second, it->second);
}
if(it!=c[u].begin()){
ok&=check(it->second, prev(it)->second);
}
return ok;
};
function<void(int)> dfs = [&](int u){
for(auto v:g[u]){
dfs(v);
sz[u]+=sz[v];
ans[u]&=ans[v];
ans[u]&= !childs[u].count(C[v]);
childs[u][C[v]] = sz[v];
if(c[u].size() <c[v].size()){
swap(c[u], c[v]);
}
// insert
for(auto p:c[v]){
ans[u]&=add(p, u);
}
}
ans[u]&=add(make_pair(sz[u], u), u);
};
dfs(0);
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |