#include "beechtree.h"
#include<bits/stdc++.h>
using namespace std;
#define REP(a,b,c) for(int a=b;a<c;a++)
#define PB push_back
#define F first
#define S second
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pi;
std::vector<int> beechtree(int n, int m, std::vector<int> p, std::vector<int> c)
{
if(n<=8){
vi ans(n,0);
vii a(n);
REP(i,0,n)a[i].PB(i);
for(int i=n-1;i>=1;i--)for(auto u:a[i])a[p[i]].PB(u);
vii b=a;
REP(i,0,n){
int s=a[i].size();
if(s==1){
ans[i]=1;
continue;
}
map<int,int> mp;
bool flag=1;
REP(j,1,s){
if(b[i][mp[c[b[i][j]]]]!=p[b[i][j]]){
flag=0;
break;
}
mp[c[b[i][j]]]++;
}
if(flag){
ans[i]=1;
continue;
}
next_permutation(b[i].begin()+1,b[i].end());
while(b[i]!=a[i]){
mp.clear();
flag=1;
REP(j,1,s){
if(b[i][mp[c[b[i][j]]]]!=p[b[i][j]]){
flag=0;
break;
}
mp[c[b[i][j]]]++;
}
if(flag)break;
next_permutation(b[i].begin()+1,b[i].end());
}
if(flag)ans[i]=1;
}
return ans;
}
bool flag=1;
REP(i,1,n)if(p[i]!=i-1)flag=0;
if(flag){
vi ans(n,0);
ans[n-1]=1;
ans[n-2]=1;
int k=c[n-1];
for(int i=n-2;i>=1;i--){
if(c[i]!=k)break;
ans[p[i]]=1;
}
return ans;
}
flag=1;
REP(i,1,n)if(p[p[i]]!=0&&p[p[i]]!=-1)flag=0;
if(flag){
vi ans(n,-1);
REP(i,1,n)if(p[i]!=0)ans[i]=1;
vii a(n);
REP(i,1,n)a[p[i]].PB(i);
vii b(n);vector<pi> arr;
REP(i,0,n)if(p[i]==0||p[i]==-1){
if(a[i].size()==0)continue;
set<int> st;
for(auto u:a[i]){
if(st.count(c[u])!=0){
ans[i]=0;
ans[0]=0;
}
st.insert(c[u]);
}
for(auto u:st)b[i].PB(u);
int s=b[i].size();
arr.PB({s,i});
}
REP(i,1,n)if(ans[i]==-1)ans[i]=1;
if(ans[0]==0)return ans;
sort(arr.begin()+1,arr.end());
reverse(arr.begin()+1,arr.end());
int k=arr.size();
map<int,int> mp;
REP(i,0,k){
int x=arr[i].F;
int y=arr[i].S;
for(auto u:b[y]){
if(mp[u]!=i)ans[0]=0;
mp[u]++;
}
if(ans[0]==0)break;
}
if(ans[0]==0)return ans;
ans[0]=1;
return ans;
}
vi ans(n,-1);
vii a(n);
REP(i,1,n)a[p[i]].PB(i);
vi tmp(n,0);
REP(i,0,n)if(a[i].size()==0){
ans[i]=1;
if(i!=0)tmp[p[i]]++;
}
vi arr;
REP(i,0,n)if(tmp[i]==a[i].size())arr.PB(i);
int s=arr.size();
REP(i,0,s){
set<int> st;
int pos=arr[i];
for(auto u:a[pos]){
if(st.count(c[u])){
ans[pos]=0;
if(p[pos]!=-1)ans[p[pos]]=0;
}
st.insert(c[u]);
}
if(ans[pos]!=0)ans[pos]=1;
if(p[pos]!=-1)tmp[p[pos]]++;
}
vi brr;
REP(i,0,n)if(tmp[i]==a[i].size())brr.PB(i);
int t=brr.size();
REP(i,0,t){
if(ans[i]==0)continue;
set<int> st;
int pos=brr[i];
for(auto u:a[pos]){
if(st.count(c[u]))ans[pos]=0;
st.insert(c[u]);
}
bool flag=0;
for(auto u:a[pos]){
if(a[u].size()!=0){
if(flag)ans[pos]=0;
flag=1;
for(auto v:a[u])if(st.count(c[v])==0)ans[pos]=0;
}
}
if(ans[pos]!=0)ans[pos]=1;
}
REP(i,0,n)if(ans[i]==-1)ans[i]=0;
return ans;
}
Compilation message
beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:102:17: warning: unused variable 'x' [-Wunused-variable]
102 | int x=arr[i].F;
| ^
beechtree.cpp:123:24: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | REP(i,0,n)if(tmp[i]==a[i].size())arr.PB(i);
beechtree.cpp:139:24: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | REP(i,0,n)if(tmp[i]==a[i].size())brr.PB(i);
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 2nd token, expected: '1', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
40 ms |
4312 KB |
Output is correct |
8 |
Correct |
40 ms |
4176 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
472 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
39 ms |
4320 KB |
Output is correct |
18 |
Correct |
40 ms |
4184 KB |
Output is correct |
19 |
Correct |
39 ms |
4188 KB |
Output is correct |
20 |
Correct |
40 ms |
4176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
42 ms |
8548 KB |
Output is correct |
16 |
Correct |
35 ms |
8028 KB |
Output is correct |
17 |
Correct |
35 ms |
7948 KB |
Output is correct |
18 |
Correct |
39 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
40 ms |
4312 KB |
Output is correct |
6 |
Correct |
40 ms |
4176 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 52nd token, expected: '1', found: '0' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 2nd token, expected: '1', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Incorrect |
1 ms |
344 KB |
2nd lines differ - on the 6th token, expected: '1', found: '0' |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 2nd token, expected: '1', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Incorrect |
1 ms |
344 KB |
2nd lines differ - on the 6th token, expected: '1', found: '0' |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
2nd lines differ - on the 2nd token, expected: '1', found: '0' |
3 |
Halted |
0 ms |
0 KB |
- |