#include "beechtree.h"
#include <bits/stdc++.h>
using namespace std;
int n,m,bad[200001],d[200001],p[200001][20],dp[2001][2001],ch[200001];
vector <pair <int, int>> ke[200001];
vector <int> res;
void dfs(int u){
for (auto [c,v]:ke[u]){
dfs(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;
}
void dfs2(int u){
for (auto [c,v]:ke[u]){
dfs2(v);
cout << u << ' ' << v << ' ' << c << '\n';
}
}
vector <int> beechtree(int N, int M, vector<int> P, vector<int> C){
n=N,m=M;
for (int i=1;i<n;i++)
ke[P[i]].push_back({C[i],i});
for (int i=0;i<n;i++){
sort(ke[i].begin(),ke[i].end());
for (int j=1;j<ke[i].size();j++)
if (ke[i][j].first==ke[i][j-1].first)
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 (auto [c,k]:ke[u])
ch[c]=1;
int cnt=0;
for (auto [c,k]:ke[v])
cnt+=ch[c];
for (auto [c,k]:ke[u])
ch[c]=0;
if (cnt!=ke[u].size()){
bad[lca(i,j)]=1;
continue;
}
dp[i][j]=(ke[i].size()>ke[j].size()?1:(ke[i].size()<ke[j].size()?-1:0));
if (!dp[i][j])
for (int k=0;k<ke[i].size();k++){
int u=ke[i][k].second,v=ke[j][k].second,ch=1;
if (dp[u][v])
dp[i][j]=dp[u][v];
}
dp[j][i]=-dp[i][j];
}
for (int i=1;i<n;i++)
for (int j=i+1;j<n;j++)
if (dp[i][j]*dp[P[i]][P[j]]==-1)
bad[lca(P[i],P[j])]=1;
for (int i=1;i<n;i++)
if (dp[0][i]<0)
bad[0]=1;
dfs(0);
for (int i=0;i<n;i++)
res.push_back(bad[i]^1);
dfs2(79);
return res;
}
Compilation message
beechtree.cpp: In function 'std::vector<int> beechtree(int, int, std::vector<int>, std::vector<int>)':
beechtree.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int j=1;j<ke[i].size();j++)
| ~^~~~~~~~~~~~~
beechtree.cpp:62:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if (cnt!=ke[u].size()){
| ~~~^~~~~~~~~~~~~~
beechtree.cpp:68:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int k=0;k<ke[i].size();k++){
| ~^~~~~~~~~~~~~
beechtree.cpp:69:61: warning: unused variable 'ch' [-Wunused-variable]
69 | int u=ke[i][k].second,v=ke[j][k].second,ch=1;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
5468 KB |
Output is correct |
3 |
Correct |
3 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
3 ms |
5396 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
3 ms |
7000 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4952 KB |
Output is correct |
16 |
Correct |
3 ms |
4952 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Incorrect |
2 ms |
4956 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Runtime error |
91 ms |
64020 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5464 KB |
Output is correct |
2 |
Correct |
3 ms |
5720 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
3 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
3 ms |
5724 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
4 ms |
5724 KB |
Output is correct |
11 |
Correct |
12 ms |
14684 KB |
Output is correct |
12 |
Correct |
8 ms |
12892 KB |
Output is correct |
13 |
Correct |
9 ms |
13660 KB |
Output is correct |
14 |
Correct |
9 ms |
13656 KB |
Output is correct |
15 |
Runtime error |
47 ms |
33744 KB |
Execution killed with signal 11 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Runtime error |
91 ms |
64020 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
5468 KB |
Output is correct |
3 |
Correct |
3 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
3 ms |
5396 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
7000 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4952 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Incorrect |
2 ms |
4956 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
5396 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
7000 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4952 KB |
Output is correct |
12 |
Correct |
3 ms |
4952 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Incorrect |
2 ms |
4956 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
5468 KB |
Output is correct |
3 |
Correct |
3 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
3 ms |
5396 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
7000 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4952 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Incorrect |
2 ms |
4956 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
3 ms |
5396 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
7000 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4952 KB |
Output is correct |
12 |
Correct |
3 ms |
4952 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Incorrect |
2 ms |
4956 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
5468 KB |
Output is correct |
3 |
Correct |
3 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
3 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
3 ms |
5396 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
7000 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4952 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
20 |
Correct |
3 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Incorrect |
2 ms |
4956 KB |
2nd lines differ - on the 1st token, expected: '1', found: '0' |
24 |
Halted |
0 ms |
0 KB |
- |