#include <bits/stdc++.h>
using namespace std;
int n1,n2,k;
vector<int> adjl1[300005];
vector<int> adjl2[300005];
int preord1[300005];
int someval[300005];
int finord[300005];
int cur = 1;
int root1,root2;
int min1[300005];
int max1[300005];
int min2[300005];
int max2[300005];
int numleaves1[300005];
int numleaves2[300005];
void dfs1(int node){
preord1[node] = cur++;
if (node<=k){
numleaves1[node] = 1;
}
for (auto x : adjl1[node]){
dfs1(x);
numleaves1[node] += numleaves1[x];
}
}
bool cmp(int a, int b){
return someval[a]<someval[b];
}
void dfs2(int node){
if (node<=k){
someval[node] = preord1[node];
numleaves2[node] = 1;
return;
}
someval[node] = 999999999;
for (auto x : adjl2[node]){
dfs2(x);
someval[node] = min(someval[node],someval[x]);
numleaves2[node] += numleaves2[x];
}
sort(adjl2[node].begin(),adjl2[node].end(),cmp);
}
void dfs3(int node){
if (node<=k)finord[node] = cur++;
if (node<=k) {
min2[node] = finord[node];
max2[node] = finord[node];
}
else{
min2[node] = 999999999;
max2[node] = -1;
}
for (auto x : adjl2[node]){
dfs3(x);
min2[node] = min(min2[node],min2[x]);
max2[node] = max(max2[node],max2[x]);
}
}
void dfs4(int node){
if (node<=k) {
min1[node] = finord[node];
max1[node] = finord[node];
}
else{
min1[node] = 999999999;
max1[node] = -1;
}
for (auto x : adjl1[node]){
dfs4(x);
min1[node] = min(min1[node],min1[x]);
max1[node] = max(max1[node],max1[x]);
}
}
bool cmp2(pair<int,int>a,pair<int,int>b){
if (a.first<b.first) return true;
if (a.first>b.first) return false;
return a.second>b.second;
}
int main(){
scanf("%d%d%d",&n1,&n2,&k);
for (int x = 1; x<=n1; x++){
int t;
scanf("%d",&t);
if (t!=0){
adjl1[t].push_back(x);
}
else root1 = x;
}
for (int x = 1; x<=n2; x++){
int t;
scanf("%d",&t);
if (t!=0){
adjl2[t].push_back(x);
}
else root2 = x;
}
dfs1(root1);
//printf("1\n");
dfs2(root2);
//printf("2\n");
dfs3(root2);
//printf("3\n");
dfs4(root1);
//printf("4\n");
vector<pair<int,int> > stuff;
for (int x = k+1; x<=n1; x++){
stuff.push_back({min1[x],max1[x]});
if (max1[x]-min1[x]+1!=numleaves1[x]){
printf("NO");
return 0;
}
}
for (int x = k+1; x<=n2; x++){
stuff.push_back({min2[x],max2[x]});
if (max2[x]-min2[x]+1!=numleaves2[x]){
printf("NO");
return 0;
}
}
sort(stuff.begin(),stuff.end(),cmp2);
stack<int> ends;
for (auto x : stuff){
while (!ends.empty() && ends.top()<x.first)ends.pop();
if (ends.empty() || x.second<=ends.top()){
ends.push(x.second);
}
else{
printf("NO");
return 0;
}
}
printf("YES");
}
Compilation message
family.cpp: In function 'int main()':
family.cpp:89:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d%d%d",&n1,&n2,&k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
family.cpp:92:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d",&t);
| ~~~~~^~~~~~~~~
family.cpp:100:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d",&t);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14428 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14404 KB |
Output is correct |
7 |
Correct |
8 ms |
14364 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14380 KB |
Output is correct |
14 |
Correct |
8 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14412 KB |
Output is correct |
16 |
Correct |
8 ms |
14408 KB |
Output is correct |
17 |
Correct |
9 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
8 ms |
14412 KB |
Output is correct |
22 |
Correct |
8 ms |
14452 KB |
Output is correct |
23 |
Correct |
8 ms |
14332 KB |
Output is correct |
24 |
Correct |
8 ms |
14412 KB |
Output is correct |
25 |
Correct |
8 ms |
14412 KB |
Output is correct |
26 |
Correct |
9 ms |
14412 KB |
Output is correct |
27 |
Correct |
8 ms |
14412 KB |
Output is correct |
28 |
Correct |
8 ms |
14368 KB |
Output is correct |
29 |
Correct |
8 ms |
14412 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14428 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14404 KB |
Output is correct |
7 |
Correct |
8 ms |
14364 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14380 KB |
Output is correct |
14 |
Correct |
8 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14412 KB |
Output is correct |
16 |
Correct |
8 ms |
14408 KB |
Output is correct |
17 |
Correct |
9 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
8 ms |
14412 KB |
Output is correct |
22 |
Correct |
8 ms |
14452 KB |
Output is correct |
23 |
Correct |
8 ms |
14332 KB |
Output is correct |
24 |
Correct |
8 ms |
14412 KB |
Output is correct |
25 |
Correct |
8 ms |
14412 KB |
Output is correct |
26 |
Correct |
9 ms |
14412 KB |
Output is correct |
27 |
Correct |
8 ms |
14412 KB |
Output is correct |
28 |
Correct |
8 ms |
14368 KB |
Output is correct |
29 |
Correct |
8 ms |
14412 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
9 ms |
14412 KB |
Output is correct |
32 |
Correct |
9 ms |
14412 KB |
Output is correct |
33 |
Correct |
10 ms |
14412 KB |
Output is correct |
34 |
Correct |
9 ms |
14376 KB |
Output is correct |
35 |
Correct |
9 ms |
14412 KB |
Output is correct |
36 |
Correct |
9 ms |
14392 KB |
Output is correct |
37 |
Correct |
9 ms |
14412 KB |
Output is correct |
38 |
Correct |
8 ms |
14412 KB |
Output is correct |
39 |
Correct |
9 ms |
14412 KB |
Output is correct |
40 |
Correct |
8 ms |
14412 KB |
Output is correct |
41 |
Correct |
8 ms |
14440 KB |
Output is correct |
42 |
Correct |
9 ms |
14540 KB |
Output is correct |
43 |
Correct |
8 ms |
14436 KB |
Output is correct |
44 |
Correct |
9 ms |
14412 KB |
Output is correct |
45 |
Correct |
11 ms |
14420 KB |
Output is correct |
46 |
Correct |
9 ms |
14412 KB |
Output is correct |
47 |
Correct |
9 ms |
14452 KB |
Output is correct |
48 |
Correct |
9 ms |
14448 KB |
Output is correct |
49 |
Correct |
9 ms |
14336 KB |
Output is correct |
50 |
Correct |
9 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14428 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14404 KB |
Output is correct |
7 |
Correct |
8 ms |
14364 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14380 KB |
Output is correct |
14 |
Correct |
8 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14412 KB |
Output is correct |
16 |
Correct |
8 ms |
14408 KB |
Output is correct |
17 |
Correct |
9 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
8 ms |
14412 KB |
Output is correct |
22 |
Correct |
8 ms |
14452 KB |
Output is correct |
23 |
Correct |
8 ms |
14332 KB |
Output is correct |
24 |
Correct |
8 ms |
14412 KB |
Output is correct |
25 |
Correct |
8 ms |
14412 KB |
Output is correct |
26 |
Correct |
9 ms |
14412 KB |
Output is correct |
27 |
Correct |
8 ms |
14412 KB |
Output is correct |
28 |
Correct |
8 ms |
14368 KB |
Output is correct |
29 |
Correct |
8 ms |
14412 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
9 ms |
14412 KB |
Output is correct |
32 |
Correct |
9 ms |
14412 KB |
Output is correct |
33 |
Correct |
10 ms |
14412 KB |
Output is correct |
34 |
Correct |
9 ms |
14376 KB |
Output is correct |
35 |
Correct |
9 ms |
14412 KB |
Output is correct |
36 |
Correct |
9 ms |
14392 KB |
Output is correct |
37 |
Correct |
9 ms |
14412 KB |
Output is correct |
38 |
Correct |
8 ms |
14412 KB |
Output is correct |
39 |
Correct |
9 ms |
14412 KB |
Output is correct |
40 |
Correct |
8 ms |
14412 KB |
Output is correct |
41 |
Correct |
8 ms |
14440 KB |
Output is correct |
42 |
Correct |
9 ms |
14540 KB |
Output is correct |
43 |
Correct |
8 ms |
14436 KB |
Output is correct |
44 |
Correct |
9 ms |
14412 KB |
Output is correct |
45 |
Correct |
11 ms |
14420 KB |
Output is correct |
46 |
Correct |
9 ms |
14412 KB |
Output is correct |
47 |
Correct |
9 ms |
14452 KB |
Output is correct |
48 |
Correct |
9 ms |
14448 KB |
Output is correct |
49 |
Correct |
9 ms |
14336 KB |
Output is correct |
50 |
Correct |
9 ms |
14412 KB |
Output is correct |
51 |
Correct |
11 ms |
14656 KB |
Output is correct |
52 |
Correct |
11 ms |
14796 KB |
Output is correct |
53 |
Correct |
12 ms |
14796 KB |
Output is correct |
54 |
Correct |
11 ms |
14712 KB |
Output is correct |
55 |
Correct |
11 ms |
14668 KB |
Output is correct |
56 |
Correct |
11 ms |
14700 KB |
Output is correct |
57 |
Correct |
11 ms |
14668 KB |
Output is correct |
58 |
Correct |
11 ms |
14680 KB |
Output is correct |
59 |
Correct |
13 ms |
14656 KB |
Output is correct |
60 |
Correct |
12 ms |
14656 KB |
Output is correct |
61 |
Correct |
12 ms |
15052 KB |
Output is correct |
62 |
Correct |
11 ms |
14924 KB |
Output is correct |
63 |
Correct |
10 ms |
14632 KB |
Output is correct |
64 |
Correct |
10 ms |
14588 KB |
Output is correct |
65 |
Correct |
9 ms |
14412 KB |
Output is correct |
66 |
Correct |
11 ms |
14668 KB |
Output is correct |
67 |
Correct |
11 ms |
14724 KB |
Output is correct |
68 |
Correct |
10 ms |
14668 KB |
Output is correct |
69 |
Correct |
12 ms |
14736 KB |
Output is correct |
70 |
Correct |
11 ms |
14652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14428 KB |
Output is correct |
2 |
Correct |
8 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14412 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14404 KB |
Output is correct |
7 |
Correct |
8 ms |
14364 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14380 KB |
Output is correct |
14 |
Correct |
8 ms |
14412 KB |
Output is correct |
15 |
Correct |
8 ms |
14412 KB |
Output is correct |
16 |
Correct |
8 ms |
14408 KB |
Output is correct |
17 |
Correct |
9 ms |
14412 KB |
Output is correct |
18 |
Correct |
10 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
8 ms |
14412 KB |
Output is correct |
22 |
Correct |
8 ms |
14452 KB |
Output is correct |
23 |
Correct |
8 ms |
14332 KB |
Output is correct |
24 |
Correct |
8 ms |
14412 KB |
Output is correct |
25 |
Correct |
8 ms |
14412 KB |
Output is correct |
26 |
Correct |
9 ms |
14412 KB |
Output is correct |
27 |
Correct |
8 ms |
14412 KB |
Output is correct |
28 |
Correct |
8 ms |
14368 KB |
Output is correct |
29 |
Correct |
8 ms |
14412 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
9 ms |
14412 KB |
Output is correct |
32 |
Correct |
9 ms |
14412 KB |
Output is correct |
33 |
Correct |
10 ms |
14412 KB |
Output is correct |
34 |
Correct |
9 ms |
14376 KB |
Output is correct |
35 |
Correct |
9 ms |
14412 KB |
Output is correct |
36 |
Correct |
9 ms |
14392 KB |
Output is correct |
37 |
Correct |
9 ms |
14412 KB |
Output is correct |
38 |
Correct |
8 ms |
14412 KB |
Output is correct |
39 |
Correct |
9 ms |
14412 KB |
Output is correct |
40 |
Correct |
8 ms |
14412 KB |
Output is correct |
41 |
Correct |
8 ms |
14440 KB |
Output is correct |
42 |
Correct |
9 ms |
14540 KB |
Output is correct |
43 |
Correct |
8 ms |
14436 KB |
Output is correct |
44 |
Correct |
9 ms |
14412 KB |
Output is correct |
45 |
Correct |
11 ms |
14420 KB |
Output is correct |
46 |
Correct |
9 ms |
14412 KB |
Output is correct |
47 |
Correct |
9 ms |
14452 KB |
Output is correct |
48 |
Correct |
9 ms |
14448 KB |
Output is correct |
49 |
Correct |
9 ms |
14336 KB |
Output is correct |
50 |
Correct |
9 ms |
14412 KB |
Output is correct |
51 |
Correct |
11 ms |
14656 KB |
Output is correct |
52 |
Correct |
11 ms |
14796 KB |
Output is correct |
53 |
Correct |
12 ms |
14796 KB |
Output is correct |
54 |
Correct |
11 ms |
14712 KB |
Output is correct |
55 |
Correct |
11 ms |
14668 KB |
Output is correct |
56 |
Correct |
11 ms |
14700 KB |
Output is correct |
57 |
Correct |
11 ms |
14668 KB |
Output is correct |
58 |
Correct |
11 ms |
14680 KB |
Output is correct |
59 |
Correct |
13 ms |
14656 KB |
Output is correct |
60 |
Correct |
12 ms |
14656 KB |
Output is correct |
61 |
Correct |
12 ms |
15052 KB |
Output is correct |
62 |
Correct |
11 ms |
14924 KB |
Output is correct |
63 |
Correct |
10 ms |
14632 KB |
Output is correct |
64 |
Correct |
10 ms |
14588 KB |
Output is correct |
65 |
Correct |
9 ms |
14412 KB |
Output is correct |
66 |
Correct |
11 ms |
14668 KB |
Output is correct |
67 |
Correct |
11 ms |
14724 KB |
Output is correct |
68 |
Correct |
10 ms |
14668 KB |
Output is correct |
69 |
Correct |
12 ms |
14736 KB |
Output is correct |
70 |
Correct |
11 ms |
14652 KB |
Output is correct |
71 |
Correct |
50 ms |
19800 KB |
Output is correct |
72 |
Correct |
45 ms |
18628 KB |
Output is correct |
73 |
Correct |
45 ms |
18008 KB |
Output is correct |
74 |
Correct |
60 ms |
22572 KB |
Output is correct |
75 |
Correct |
59 ms |
21692 KB |
Output is correct |
76 |
Correct |
590 ms |
65332 KB |
Output is correct |
77 |
Correct |
589 ms |
52480 KB |
Output is correct |
78 |
Correct |
284 ms |
32152 KB |
Output is correct |
79 |
Correct |
478 ms |
51916 KB |
Output is correct |
80 |
Correct |
513 ms |
50076 KB |
Output is correct |
81 |
Correct |
41 ms |
18608 KB |
Output is correct |
82 |
Correct |
44 ms |
17996 KB |
Output is correct |
83 |
Correct |
27 ms |
17436 KB |
Output is correct |
84 |
Correct |
56 ms |
22320 KB |
Output is correct |
85 |
Correct |
50 ms |
20548 KB |
Output is correct |
86 |
Correct |
383 ms |
46016 KB |
Output is correct |
87 |
Correct |
401 ms |
50364 KB |
Output is correct |
88 |
Correct |
237 ms |
29968 KB |
Output is correct |
89 |
Correct |
401 ms |
46420 KB |
Output is correct |
90 |
Correct |
472 ms |
41920 KB |
Output is correct |