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;
#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]]!=-1&&p[p[i]]!=0)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){
set<int> st;
for(auto u:a[i]){
if(st.count(c[u])!=0){
ans[i]=0;
ans[0]=0;
}
st.insert(c[i]);
}
if(ans[0]==0)break;
for(auto u:st)b[i].PB(u);
int s=b[i].size();
arr.PB({s,i});
}
if(ans[0]==0)return ans;
sort(arr.begin(),arr.end());
reverse(arr.begin(),arr.end());
int k=arr.size();
map<int,int> mp;
REP(i,0,k){
int x=arr[i].F;
int y=arr[i].S;
int t=mp[b[y][0]];
for(auto u:b[y]){
if(mp[u]!=t)ans[0]=0;
mp[u]++;
}
if(ans[0]==0)break;
}
if(ans[0]==0)return ans;
ans[0]=1;
return ans;
}
return {};
}
Compilation message (stderr)
beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:101:17: warning: unused variable 'x' [-Wunused-variable]
101 | int x=arr[i].F;
| ^
# | 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... |