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;
#ifdef LOCAL
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
const int MAXN = 2005;
const int inf=1000000500ll;
const long long oo =1000000000000000500ll;
const int MOD = (int)1e9+7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int,int> pi;
int par[MAXN];
vector<int>childs[MAXN];
int st[MAXN],en[MAXN];
int TIME;
bool unbalanced[MAXN];
int dep[MAXN];
int tobad[MAXN];
bool child(int par, int child){
if(st[par]<=st[child] && en[par]>=en[child])return 1;
return 0;
}
void dfs(int x, int p, int lvl){
dep[x]=lvl;
st[x]=TIME++;
par[x]=p;
for(auto v:childs[x]){
dfs(v,x,lvl+1);
if(tobad[v]){
tobad[x]=min(tobad[x],tobad[v]);
}
}
if(childs[x].size() == 1){
unbalanced[x]=1;
tobad[x]=dep[x];
}
en[x]=TIME-1;
}
vector<int> V[MAXN];
void solve(int x){
V[dep[x]].push_back(x);
if(childs[x].empty()){
return;
}
if(childs[x].size() == 1){
solve(childs[x][0]);
return;
}
if(dep[tobad[childs[x][0]]]>dep[tobad[childs[x][1]]]){ //
solve(childs[x][0]);
solve(childs[x][1]);
} else{
solve(childs[x][1]);
solve(childs[x][0]);
}
}
std::vector<int> beechtree(int n, int M, std::vector<int> P, std::vector<int> C)
{
vector<int> res=vector<int>(n,0);
for(int i=0;i<n;i++)tobad[i]=inf;
for(int i=1;i<n;i++){
childs[P[i]].push_back(i);
}
dfs(0,-1,0);
for(int i=0;i<n;i++){ // check for this guy
for(int x=0;x<=n;x++)V[x].clear();
vector<int>vec;
for(int x=0;x<n;x++){
if(child(i,x))vec.push_back(x);
}
sort(vec.begin(),vec.end());
bool bad=0;
for(auto x:vec){
if(childs[x].size() > 2){ // literally no way
bad=1;
break;
}
if(childs[x].size() == 2){
if(childs[x][0] == childs[x][1]){
bad=1;
break;
}
}
}
if(bad){
res[i]=0;
continue;
}
solve(i);
// now check if(V) is valid
vector<int>nodes;
for(int l=dep[i];l<n;l++){
for(auto x:V[l]){
nodes.push_back(x);
}
}
int cnt[2];
memset(cnt,0,sizeof cnt);
for(int x=1;x<nodes.size();x++){
int to=cnt[C[nodes[i]]];
if(par[nodes[i]] != nodes[to]){
bad=1;
}
cnt[C[nodes[i]]]++;
}
if(bad){
res[i]=0;
} else {
res[i]=1;
}
}
return res;
}
Compilation message (stderr)
beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:106:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int x=1;x<nodes.size();x++){
| ~^~~~~~~~~~~~~
# | 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... |