#include "beechtree.h"
#include <bits/stdc++.h>
using namespace std;
const int lim=200001,mxn=1e7+1;
int n,m,p[lim][20],bad[lim],sz[lim],tin[lim],tout[lim],t,id,pos[lim],order[lim],root[lim][2],last[lim],cur;
int le[mxn],ri[mxn],st[mxn];
vector <int> c,res,ke[lim],ve[lim];
void dfs(int u){
sz[u]=1;
tin[u]=++t;
for (int v:ke[u]){
for (int i=1;i<20;i++)
p[v][i]=(p[v][i-1]<0?-1:p[p[v][i-1]][i-1]);
dfs(v);
sz[u]+=sz[v];
}
tout[u]=t;
ve[sz[u]].push_back(u);
}
void dfs2(int u){
for (int v:ke[u]){
dfs2(v);
bad[u]|=bad[v];
}
}
int update(int node, int l, int r, int i){
cur++;
if (l==r){
st[cur]++;
return cur;
}
int tmp=cur,mid=(l+r)>>1;
le[tmp]=le[node];
ri[tmp]=ri[node];
if (mid<i)
ri[tmp]=update(ri[node],mid+1,r,i);
else
le[tmp]=update(le[node],l,mid,i);
st[tmp]=st[le[tmp]]+st[ri[tmp]];
return tmp;
}
int get(int node, int l, int r, int u, int v){
if (l>v||r<u||!node)
return 0;
if (l>=u&&r<=v)
return st[node];
int mid=(l+r)>>1;
return get(le[node],l,mid,u,v)+get(ri[node],mid+1,r,u,v);
}
int get(int i, int j, int k){
int res=get(root[i][k],1,n,tin[j],tout[j]);
if (!k||c[i]==c[j])
res--;
return res-k;
}
vector <int> beechtree(int N, int M, vector<int> P, vector<int> C){
n=N,m=M,c=C;
for (int i=1;i<n;i++){
ke[P[i]].push_back(i);
p[i][0]=P[i];
}
dfs(0);
for (int i=n;i>=0;i--){
sort(ve[i].begin(),ve[i].end(),[](int u, int v){return pos[p[u][0]]<pos[p[v][0]];});
for (int j:ve[i]){
pos[j]=++id;
order[id]=j;
}
}
for (int i=1;i<=n;i++){
int j=order[i];
root[j][0]=update(root[order[i-1]][0],1,n,tin[j]);
root[j][1]=update(last[c[j]],1,n,tin[j]);
last[c[j]]=root[j][1];
}
for (int i=1;i<n;i++){
int u=i;
for (int j=19;j>=0;j--)
if (p[u][j]>=0)
if (get(i,p[u][j],1)==get(P[i],p[u][j],0))
u=p[u][j];
if (p[u][0]>=0)
bad[p[u][0]]=1;
}
dfs2(0);
for (int i=0;i<n;i++)
res.push_back(bad[i]^1);
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
17016 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
16984 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
16984 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
17240 KB |
Output is correct |
2 |
Incorrect |
3 ms |
17016 KB |
2nd lines differ - on the 11th token, expected: '0', found: '1' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
16984 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
17016 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Incorrect |
3 ms |
16984 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
16984 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
17016 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Incorrect |
3 ms |
16984 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
16984 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
17016 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Incorrect |
3 ms |
16984 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
5 |
Halted |
0 ms |
0 KB |
- |