#include <bits/stdc++.h>
using namespace std;
const int MX=5e3+5;
int n,k;
int N1,N2;
vector<int> v[MX];
int lca1[MX][MX];
int lca2[MX][MX];
struct Tree{
vector<int> a[MX];
int dth[MX];
int p[MX][13];
int r;
int lca(int v,int u){
int i,j;
if(dth[v]>dth[u])swap(u,v);
for(i=12 ; i>=0 ; i--)if(dth[u]-dth[v]>=(1<<i))u=p[u][i];
if(u==v)return v;
for(i=12 ; i>=0 ; i--){
if(p[u][i]!=p[v][i]){
u=p[u][i];
v=p[v][i];
}
}
return p[v][0];
}
void dfs(int v){
for(auto u: a[v]){
if(dth[u])continue;
p[u][0]=v;
dth[u]=dth[v]+1;
dfs(u);
}
}
void build(){
dth[r]=1;
dfs(r);
for(int i=1 ; i<=12 ; i++){
for(int j=1 ; j<=n ; j++){
p[j][i]=p[p[j][i-1]][i-1];
}
}
}
void add(int u,int v){
a[u].push_back(v);
}
};
Tree T1,T2;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>N1>>N2>>k;
n=max(N1,N2);
int i,j;
for(i=1 ; i<=N1 ; i++){
int p;
cin>>p;
if(p){
T1.add(p,i);
}
else
T1.r=i;
}
for(i=1 ; i<=N2 ; i++){
int p;
cin>>p;
if(p){
T2.add(p,i);
}
else
T2.r=i;
}
T1.build();
T2.build();
for(i=1 ; i<=k ; i++){
for(j=i ; j<=k ; j++){
lca1[i][j]=lca1[j][i]=T1.dth[T1.lca(i,j)];
lca2[i][j]=lca2[j][i]=T2.dth[T2.lca(i,j)];
}
}
for(i=1 ; i<=k ; i++){
int mx=0;
for(j=1 ; j<=n ; j++)
v[j].clear();
for(j=1 ; j<=k ; j++){
v[lca1[i][j]].push_back(j);
}
int l=0;
for(j=1 ; j<=n ; j++){
for(auto x: v[j-1]){
mx=max(mx,lca2[i][x]);
l++;
}
for(auto x: v[j]){
if(mx>lca2[i][x]){
cout<<"NO";
return 0;
}
}
}
}
cout<<"YES";
return 0;
}
Compilation message
family.cpp: In member function 'int Tree::lca(int, int)':
family.cpp:16:11: warning: unused variable 'j' [-Wunused-variable]
16 | int i,j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
1664 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
2 ms |
1664 KB |
Output is correct |
20 |
Correct |
1 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1792 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
1 ms |
768 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
1152 KB |
Output is correct |
27 |
Correct |
1 ms |
768 KB |
Output is correct |
28 |
Correct |
1 ms |
768 KB |
Output is correct |
29 |
Correct |
1 ms |
896 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
1664 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
2 ms |
1664 KB |
Output is correct |
20 |
Correct |
1 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1792 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
1 ms |
768 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
1152 KB |
Output is correct |
27 |
Correct |
1 ms |
768 KB |
Output is correct |
28 |
Correct |
1 ms |
768 KB |
Output is correct |
29 |
Correct |
1 ms |
896 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
31 |
Correct |
1 ms |
896 KB |
Output is correct |
32 |
Correct |
1 ms |
768 KB |
Output is correct |
33 |
Correct |
1 ms |
896 KB |
Output is correct |
34 |
Correct |
1 ms |
768 KB |
Output is correct |
35 |
Correct |
1 ms |
768 KB |
Output is correct |
36 |
Correct |
3 ms |
2048 KB |
Output is correct |
37 |
Correct |
1 ms |
1024 KB |
Output is correct |
38 |
Correct |
4 ms |
2176 KB |
Output is correct |
39 |
Correct |
4 ms |
1664 KB |
Output is correct |
40 |
Correct |
1 ms |
1152 KB |
Output is correct |
41 |
Correct |
1 ms |
896 KB |
Output is correct |
42 |
Correct |
1 ms |
896 KB |
Output is correct |
43 |
Correct |
1 ms |
896 KB |
Output is correct |
44 |
Correct |
1 ms |
768 KB |
Output is correct |
45 |
Correct |
1 ms |
768 KB |
Output is correct |
46 |
Correct |
2 ms |
1664 KB |
Output is correct |
47 |
Correct |
2 ms |
1152 KB |
Output is correct |
48 |
Correct |
7 ms |
3200 KB |
Output is correct |
49 |
Correct |
1 ms |
1152 KB |
Output is correct |
50 |
Correct |
1 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
1664 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
2 ms |
1664 KB |
Output is correct |
20 |
Correct |
1 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1792 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
1 ms |
768 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
1152 KB |
Output is correct |
27 |
Correct |
1 ms |
768 KB |
Output is correct |
28 |
Correct |
1 ms |
768 KB |
Output is correct |
29 |
Correct |
1 ms |
896 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
31 |
Correct |
1 ms |
896 KB |
Output is correct |
32 |
Correct |
1 ms |
768 KB |
Output is correct |
33 |
Correct |
1 ms |
896 KB |
Output is correct |
34 |
Correct |
1 ms |
768 KB |
Output is correct |
35 |
Correct |
1 ms |
768 KB |
Output is correct |
36 |
Correct |
3 ms |
2048 KB |
Output is correct |
37 |
Correct |
1 ms |
1024 KB |
Output is correct |
38 |
Correct |
4 ms |
2176 KB |
Output is correct |
39 |
Correct |
4 ms |
1664 KB |
Output is correct |
40 |
Correct |
1 ms |
1152 KB |
Output is correct |
41 |
Correct |
1 ms |
896 KB |
Output is correct |
42 |
Correct |
1 ms |
896 KB |
Output is correct |
43 |
Correct |
1 ms |
896 KB |
Output is correct |
44 |
Correct |
1 ms |
768 KB |
Output is correct |
45 |
Correct |
1 ms |
768 KB |
Output is correct |
46 |
Correct |
2 ms |
1664 KB |
Output is correct |
47 |
Correct |
2 ms |
1152 KB |
Output is correct |
48 |
Correct |
7 ms |
3200 KB |
Output is correct |
49 |
Correct |
1 ms |
1152 KB |
Output is correct |
50 |
Correct |
1 ms |
896 KB |
Output is correct |
51 |
Correct |
2759 ms |
190968 KB |
Output is correct |
52 |
Correct |
2074 ms |
147420 KB |
Output is correct |
53 |
Correct |
1032 ms |
74232 KB |
Output is correct |
54 |
Correct |
746 ms |
66424 KB |
Output is correct |
55 |
Correct |
2456 ms |
189908 KB |
Output is correct |
56 |
Correct |
2740 ms |
186224 KB |
Output is correct |
57 |
Correct |
1696 ms |
151232 KB |
Output is correct |
58 |
Correct |
1400 ms |
112620 KB |
Output is correct |
59 |
Correct |
2578 ms |
180892 KB |
Output is correct |
60 |
Correct |
2023 ms |
174440 KB |
Output is correct |
61 |
Correct |
1016 ms |
73080 KB |
Output is correct |
62 |
Correct |
944 ms |
70708 KB |
Output is correct |
63 |
Correct |
1887 ms |
168528 KB |
Output is correct |
64 |
Correct |
206 ms |
23192 KB |
Output is correct |
65 |
Correct |
1 ms |
1024 KB |
Output is correct |
66 |
Correct |
1491 ms |
117404 KB |
Output is correct |
67 |
Correct |
2187 ms |
181396 KB |
Output is correct |
68 |
Correct |
250 ms |
27756 KB |
Output is correct |
69 |
Correct |
1273 ms |
102436 KB |
Output is correct |
70 |
Correct |
1682 ms |
158420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
768 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
1664 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
2 ms |
1664 KB |
Output is correct |
20 |
Correct |
1 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1792 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
1 ms |
768 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
1152 KB |
Output is correct |
27 |
Correct |
1 ms |
768 KB |
Output is correct |
28 |
Correct |
1 ms |
768 KB |
Output is correct |
29 |
Correct |
1 ms |
896 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
31 |
Correct |
1 ms |
896 KB |
Output is correct |
32 |
Correct |
1 ms |
768 KB |
Output is correct |
33 |
Correct |
1 ms |
896 KB |
Output is correct |
34 |
Correct |
1 ms |
768 KB |
Output is correct |
35 |
Correct |
1 ms |
768 KB |
Output is correct |
36 |
Correct |
3 ms |
2048 KB |
Output is correct |
37 |
Correct |
1 ms |
1024 KB |
Output is correct |
38 |
Correct |
4 ms |
2176 KB |
Output is correct |
39 |
Correct |
4 ms |
1664 KB |
Output is correct |
40 |
Correct |
1 ms |
1152 KB |
Output is correct |
41 |
Correct |
1 ms |
896 KB |
Output is correct |
42 |
Correct |
1 ms |
896 KB |
Output is correct |
43 |
Correct |
1 ms |
896 KB |
Output is correct |
44 |
Correct |
1 ms |
768 KB |
Output is correct |
45 |
Correct |
1 ms |
768 KB |
Output is correct |
46 |
Correct |
2 ms |
1664 KB |
Output is correct |
47 |
Correct |
2 ms |
1152 KB |
Output is correct |
48 |
Correct |
7 ms |
3200 KB |
Output is correct |
49 |
Correct |
1 ms |
1152 KB |
Output is correct |
50 |
Correct |
1 ms |
896 KB |
Output is correct |
51 |
Correct |
2759 ms |
190968 KB |
Output is correct |
52 |
Correct |
2074 ms |
147420 KB |
Output is correct |
53 |
Correct |
1032 ms |
74232 KB |
Output is correct |
54 |
Correct |
746 ms |
66424 KB |
Output is correct |
55 |
Correct |
2456 ms |
189908 KB |
Output is correct |
56 |
Correct |
2740 ms |
186224 KB |
Output is correct |
57 |
Correct |
1696 ms |
151232 KB |
Output is correct |
58 |
Correct |
1400 ms |
112620 KB |
Output is correct |
59 |
Correct |
2578 ms |
180892 KB |
Output is correct |
60 |
Correct |
2023 ms |
174440 KB |
Output is correct |
61 |
Correct |
1016 ms |
73080 KB |
Output is correct |
62 |
Correct |
944 ms |
70708 KB |
Output is correct |
63 |
Correct |
1887 ms |
168528 KB |
Output is correct |
64 |
Correct |
206 ms |
23192 KB |
Output is correct |
65 |
Correct |
1 ms |
1024 KB |
Output is correct |
66 |
Correct |
1491 ms |
117404 KB |
Output is correct |
67 |
Correct |
2187 ms |
181396 KB |
Output is correct |
68 |
Correct |
250 ms |
27756 KB |
Output is correct |
69 |
Correct |
1273 ms |
102436 KB |
Output is correct |
70 |
Correct |
1682 ms |
158420 KB |
Output is correct |
71 |
Runtime error |
25 ms |
5632 KB |
Execution killed with signal 11 |
72 |
Halted |
0 ms |
0 KB |
- |