#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[3];
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
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++){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |