#include "beechtree.h"
#include <bits/stdc++.h>
using namespace std;
int n,m,bad[200001],cnt[200001],sz[200001],pos[200001],id,p[200001][20],d[200001],ch[200001];
vector <int> par,c,res,T[200001],ke[200001],ve[200001];
void dfs(int u){
sz[u]=1;
for (int v:ke[u]){
dfs(v);
sz[u]+=sz[v];
}
ve[sz[u]].push_back(u);
}
void dfs2(int u){
T[u].push_back(u);
for (int v:ke[u]){
dfs2(v);
for (int i:T[v])
T[u].push_back(i);
}
sort(T[u].begin(),T[u].end(),[](int i, int j){return pos[i]<pos[j];});
for (int i=1;i<T[u].size()&&!bad[u];i++)
for (int j=i+1;j<T[u].size()&&!bad[u];j++){
int x=T[u][i],y=T[u][j];
if (c[x]==c[y]&&pos[par[x]]>pos[par[y]])
bad[u]=1;
}
}
void dfs3(int u){
for (int v:ke[u]){
dfs3(v);
bad[u]|=bad[v];
}
}
int lca(int u, int v){
u++,v++;
if (d[u]<d[v])
swap(u,v);
int x=d[u]-d[v];
for (int i=0;i<20;i++)
if ((x>>i)&1)
u=p[u][i];
if (u==v)
return u-1;
for (int i=19;i>=0;i--)
if (p[u][i]!=p[v][i]){
u=p[u][i];
v=p[v][i];
}
return p[u][0]-1;
}
vector <int> beechtree(int N, int M, vector<int> P, vector<int> C){
n=N,m=M,par=P,c=C;
for (int i=1;i<n;i++)
ke[P[i]].push_back(i);
for (int i=0;i<n;i++){
sort(ke[i].begin(),ke[i].end(),[](int i, int j){return make_pair(c[i],i)<make_pair(c[j],j);});
for (int j=1;j<ke[i].size();j++)
if (c[ke[i][j]]==c[ke[i][j-1]])
bad[i]=1;
p[i+1][0]=P[i]+1;
d[i+1]=d[P[i]+1]+1;
for (int j=1;j<20;j++)
p[i+1][j]=p[p[i+1][j-1]][j-1];
}
for (int i=n-1;i>=0;i--)
for (int j=n-1;j>i;j--){
int u=i,v=j;
if (ke[i].size()>ke[j].size())
swap(u,v);
for (int k:ke[u])
ch[c[k]]=1;
int cnt=0;
for (int k:ke[v])
cnt+=ch[c[k]];
for (int k:ke[u])
ch[c[k]]=0;
if (cnt!=ke[u].size()){
bad[lca(i,j)]=1;
continue;
}
}
dfs(0);
for (int i=n;i>=0;i--){
sort(ve[i].begin(),ve[i].end(),[](int u, int v){return pos[par[u]]<pos[par[v]];});
for (int j:ve[i])
pos[j]=++id;
}
dfs2(0);
dfs3(0);
for (int i=0;i<n;i++)
res.push_back(bad[i]^1);
return res;
}
Compilation message
beechtree.cpp: In function 'void dfs2(int)':
beechtree.cpp:22:19: 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<T[u].size()&&!bad[u];i++)
| ~^~~~~~~~~~~~
beechtree.cpp:23:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int j=i+1;j<T[u].size()&&!bad[u];j++){
| ~^~~~~~~~~~~~
beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int j=1;j<ke[i].size();j++)
| ~^~~~~~~~~~~~~
beechtree.cpp:78:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | if (cnt!=ke[u].size()){
| ~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19032 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
4 ms |
19032 KB |
Output is correct |
6 |
Correct |
4 ms |
19032 KB |
Output is correct |
7 |
Correct |
4 ms |
19032 KB |
Output is correct |
8 |
Correct |
4 ms |
19064 KB |
Output is correct |
9 |
Incorrect |
4 ms |
19036 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19032 KB |
Output is correct |
4 |
Correct |
4 ms |
19036 KB |
Output is correct |
5 |
Correct |
4 ms |
19032 KB |
Output is correct |
6 |
Correct |
4 ms |
19032 KB |
Output is correct |
7 |
Execution timed out |
2048 ms |
47184 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19036 KB |
Output is correct |
2 |
Correct |
5 ms |
19056 KB |
Output is correct |
3 |
Correct |
4 ms |
19032 KB |
Output is correct |
4 |
Correct |
5 ms |
19056 KB |
Output is correct |
5 |
Correct |
5 ms |
19036 KB |
Output is correct |
6 |
Correct |
5 ms |
19036 KB |
Output is correct |
7 |
Incorrect |
4 ms |
19072 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19032 KB |
Output is correct |
4 |
Correct |
4 ms |
19064 KB |
Output is correct |
5 |
Execution timed out |
2048 ms |
47184 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19068 KB |
Output is correct |
4 |
Correct |
4 ms |
19032 KB |
Output is correct |
5 |
Correct |
4 ms |
19036 KB |
Output is correct |
6 |
Correct |
4 ms |
19032 KB |
Output is correct |
7 |
Correct |
4 ms |
19036 KB |
Output is correct |
8 |
Correct |
4 ms |
19032 KB |
Output is correct |
9 |
Correct |
4 ms |
19032 KB |
Output is correct |
10 |
Correct |
4 ms |
19032 KB |
Output is correct |
11 |
Correct |
4 ms |
19064 KB |
Output is correct |
12 |
Incorrect |
4 ms |
19036 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19032 KB |
Output is correct |
4 |
Correct |
4 ms |
19064 KB |
Output is correct |
5 |
Incorrect |
4 ms |
19036 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19068 KB |
Output is correct |
4 |
Correct |
4 ms |
19032 KB |
Output is correct |
5 |
Correct |
4 ms |
19036 KB |
Output is correct |
6 |
Correct |
4 ms |
19032 KB |
Output is correct |
7 |
Correct |
4 ms |
19036 KB |
Output is correct |
8 |
Correct |
4 ms |
19032 KB |
Output is correct |
9 |
Correct |
4 ms |
19032 KB |
Output is correct |
10 |
Correct |
4 ms |
19032 KB |
Output is correct |
11 |
Correct |
4 ms |
19064 KB |
Output is correct |
12 |
Incorrect |
4 ms |
19036 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19032 KB |
Output is correct |
4 |
Correct |
4 ms |
19064 KB |
Output is correct |
5 |
Incorrect |
4 ms |
19036 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19032 KB |
Output is correct |
2 |
Correct |
4 ms |
19036 KB |
Output is correct |
3 |
Correct |
4 ms |
19068 KB |
Output is correct |
4 |
Correct |
4 ms |
19032 KB |
Output is correct |
5 |
Correct |
4 ms |
19036 KB |
Output is correct |
6 |
Correct |
4 ms |
19032 KB |
Output is correct |
7 |
Correct |
4 ms |
19036 KB |
Output is correct |
8 |
Correct |
4 ms |
19032 KB |
Output is correct |
9 |
Correct |
4 ms |
19032 KB |
Output is correct |
10 |
Correct |
4 ms |
19032 KB |
Output is correct |
11 |
Correct |
4 ms |
19064 KB |
Output is correct |
12 |
Incorrect |
4 ms |
19036 KB |
2nd lines differ - on the 1st token, expected: '0', found: '1' |
13 |
Halted |
0 ms |
0 KB |
- |