#include "beechtree.h"
#include <algorithm>
#include <map>
using namespace std;
int n,m;
vector<int>p,c,ans,depth;
vector<vector<int>>v;
vector<int>subtree;
void dfs(int x){
subtree.push_back(x);
for(int z:v[x]){
dfs(z);
}
}
bool check(vector<int>s){
map<int,int>m;
for(int i=1;i<s.size();i++){
if(s[m[c[s[i]]]]!=p[s[i]]) return false;
m[c[s[i]]]++;
}
return true;
}
void sub1(){
for(int i=0;i<n;i++){
dfs(i);
sort(subtree.begin(),subtree.end());
do{
if(check(subtree)){
ans[i]=1;
break;
}
}while(next_permutation(subtree.begin(),subtree.end()));
subtree.clear();
}
}
void sub2(){
ans[n-1]=ans[n-2]=1;
int j=n-2;
while(j>0 && c[j]==c[j+1]){
ans[--j]=1;
}
}
void sub3(){
vector<vector<int>>cnt(m+1);
int k=0,g=0;
for(int i=1;i<n;i++){
if(depth[i]==2) g=i,k++;
cnt[c[i]].push_back(i);
dfs(i);
ans[i]=check(subtree);
vector<int>ev;
swap(ev,subtree);
}
for(int i=1;i<=m;i++){
if(i!=c[g] && cnt[c[i]].size()>1) return;
}
if(k>1 || cnt[c[g]].size()!=2) return;
ans[0]=1;
// vector<bool>vis(n,false);
// subtree.push_back(0);
// if(g>0){
// subtree.push_back(p[g]);
// if(cnt[c[g]][0]!=p[g]) subtree.push_back(cnt[c[g]][0]);
// }
// for(int x:subtree) vis[x]=true;
}
void dfs(int x,int c){
depth[x]=c;
for(int z:v[x]){
dfs(x,c+1);
}
}
vector<int> beechtree(int N, int M, std::vector<int> P, std::vector<int> C){
n=N,m=M;
swap(p,P);
swap(c,C);
v.resize(n);
bool sui2=true;
for(int i=1;i<n;i++){
if(p[i]!=i-1) sui2=false;
v[p[i]].push_back(i);
}
ans.assign(n,0);
depth.resize(n);
dfs(0,0);
if(n<=8) sub1();
else if(sui2) sub2();
else sub3();
return ans;
}
Compilation message
beechtree.cpp: In function 'bool check(std::vector<int>)':
beechtree.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=1;i<s.size();i++){
| ~^~~~~~~~~
beechtree.cpp: In function 'void dfs(int, int)':
beechtree.cpp:78:13: warning: unused variable 'z' [-Wunused-variable]
78 | for(int z:v[x]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1298 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1063 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1063 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1050 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1063 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1298 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1063 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1298 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1063 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1298 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |