# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1061293 | tolbi | Beech Tree (IOI23_beechtree) | C++17 | 147 ms | 102776 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 <bits/stdc++.h>
using namespace std;
bool valid(vector<vector<int>> &arr){
vector<int> in(arr.size());
for (int i = 0; i < arr.size(); i++){
for (auto it : arr[i]){
in[it]++;
}
}
queue<int> q;
int say = 0;
for (int i = 0; i < arr.size(); i++){
if (in[i]==0) q.push(i);
}
while (q.size()){
int node = q.front();
say++;
q.pop();
for (auto it : arr[node]){
in[it]--;
if (in[it]==0) q.push(it);
}
}
return (say==arr.size());
}
std::vector<int> beechtree(int N, int M, std::vector<int> P, std::vector<int> C)
{
vector<int> ans(N);
vector<vector<int>> arr(N);
for (int i = 1; i < N; i++){
arr[P[i]].push_back(i);
}
vector<set<int>> v(N);
function<void(int)> dfs = [&](int node)->void{
bool boolean=true;
map<int,vector<int>> mp;
map<int,int> vv;
int ind = 0;
for (auto it : arr[node]){
dfs(it);
if (ans[it]==0){
boolean=false;
}
if (boolean){
for (auto it2 : v[it]){
mp[it2].push_back(ind);
}
vv[C[it]]=ind;
ind++;
}
}
if (vv.size()!=arr[node].size()){
boolean=false;
}
if (boolean){
vector<vector<int>> gr(arr[node].size());
for (auto it : mp){
int hh = vv[it.first];
for (auto it2 : it.second){
if (hh==it2) continue;
gr[it2].push_back(hh);
}
}
if (!valid(gr)){
boolean=false;
}
}
if (boolean){
for (auto it : mp){
v[node].insert(it.first);
}
for (auto it : arr[node]){
v[node].insert(C[it]);
}
if (v[node].size()!=arr[node].size()){
boolean=false;
while (v[node].size()) v[node].erase(v[node].begin());
return;
}
ans[node]=1;
}
};
dfs(0);
return ans;
}
Compilation message (stderr)
# | 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... |