# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
987426 | activedeltorre | Beech Tree (IOI23_beechtree) | C++17 | 0 ms | 0 KiB |
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 <cassert>
#include <cstdio>
//#include "beechtree.h"
#include <vector>
#include <queue>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
vector<int>cop[200005];
int cul[200005];
int imp[200005];
int dist[200005];
int par[200005];
vector<int>vecus;
map<int,int>mp;
map<int,int>mp2;
map<int,int>fre;
long long mod=1e9+7;
long long has[200005];
vector<long long> vactoriceanu[510];
int nmax=500;
int calc(int curr)
{
mp2.clear();
fre.clear();
int cur;
queue<int>qu;
dist[curr]=0;
qu.push(curr);
vecus.clear();
for(int i=1;i<=nmax;i++)
{
vactoriceanu[i].clear();
}
while(qu.size())
{
cur=qu.front();
vecus.push_back(cur);
qu.pop();
for(auto k:cop[cur])
{
dist[k]=dist[curr]+1;
qu.push(k);
}
}
int posible=0;
for(auto i:vecus)
{
if(i!=curr)
{
fre[cul[i]]++;
vactoriceanu[fre[cul[i]]].push_back(cul[i]);
}
mp2[has[i]]++;
posible=max(posible,imp[i]);
}
for(int i=1;i<=nmax;i++)
{
sort(vactoriceanu[i].begin(),vactoriceanu[i].end());
long long val=0;
for(auto j:vactoriceanu[i])
{
val=(val*23+j+13)%mod;
}
if(val!=0)
{
if(mp2[val]==0)
{
return 0;
}
else
{
mp2[val]--;
}
}
vactoriceanu[i].clear();
}
if(posible==1)
{
return 0;
}
return 1;
}
map<pair<int,int> ,int >frevals;
std::vector<int> beechtree(int N, int M, std::vector<int> P, std::vector<int> C)
{
int n=N,culs=M,i;
vector<int>ans;
for(i=1;i<n;i++)
{
par[i]=P[i];
cop[P[i]].push_back(i);
cul[i]=C[i];
}
for(i=0;i<n;i++)
{
for(auto k:cop[i])
{
has[i]=(has[i]*23+cul[k]+13)%mod;
frevals[{i,cul[k]}]++;
}
for(auto k:cop[i])
{
for(auto j:cop[k])
{
if(frevals[{k,cul[j]}]>frevals[{i,cul[j]}])
{
imp[i]=1;
}
}
}
}
for(i=0;i<n ;i++)
{
ans.push_back(calc(i));
}
return ans;
}
int main()
{
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> P(N);
for (int i = 0; i < N; ++i)
assert(1 == scanf("%d", &P[i]));
std::vector<int> C(N);
for (int i = 0; i < N; ++i)
assert(1 == scanf("%d", &C[i]));
fclose(stdin);
std::vector<int> res = beechtree(N, M, P, C);
int n = res.size();
for (int i = 0; i < n; ++i)
{
if (i > 0)
printf(" ");
printf("%d", res[i]);
}
printf("\n");
fclose(stdout);
return 0;
}