//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
void rage(){
cout<<"NO"<<endl;
exit(0);
}
int n,m,k;
int root;
vector<int> al[300005];
int canon[300005]; //canonical index
int idx[300005]; //mapping back to normal index
ii span[300005]; //the span of canonical indexes
set<int> s[300005]; //the l endpoint of canonical index of children
int tkd[300005][20];
int IDX=0;
void dfs1(int i){
if (i<=k){
canon[i]=IDX;
idx[IDX]=i;
span[i]=ii(canon[i],canon[i]);
IDX++;
}
else{
span[i]=ii(1e9,-1e9);
for (auto &it:al[i]){
int curr=tkd[it][0]=i;
rep(x,0,20){
if (curr==-1) break;
curr=tkd[it][x+1]=tkd[curr][x];
}
dfs1(it);
span[i]=ii(
min(span[i].fi,span[it].fi),
max(span[i].se,span[it].se)
);
s[i].insert(span[it].fi);
}
s[i].insert(span[i].se+1);
}
}
set<ii> r[300005]; //ranges of the canonical indexes
ii span2[300005];
void dfs2(int i){
if (i<=k){
r[i].insert(ii(canon[i],canon[i]));
span2[i]=ii(canon[i],canon[i]);
}
else{
span2[i]=ii(1e9,-1e9);
for (auto &it:al[i]){
dfs2(it);
span2[i]=ii(
min(span2[i].fi,span2[it].fi),
max(span2[i].se,span2[it].se)
);
if (sz(r[it])>sz(r[i])) swap(r[it],r[i]);
for (auto it:r[it]){
auto temp=r[i].lb(it);
if (temp!=r[i].end() && (*temp).fi==it.se+1){
it.se=(*temp).se;
r[i].erase(temp);
}
temp=r[i].lb(it);
if (temp!=r[i].begin() && (*prev(temp)).se+1==it.fi){
it.fi=(*prev(temp)).fi;
r[i].erase(prev(temp));
}
r[i].insert(it);
}
}
}
//cout<<i<<" "<<span2[i].fi<<" "<<span2[i].se<<endl;
//for (auto &it:r[i]) cout<<it.fi<<" "<<it.se<<endl;
int curr=idx[span2[i].fi];
rep(x,20,0){
int temp=tkd[curr][x];
if (temp!=-1 && (span2[i].fi<span[temp].fi || span[temp].se<span2[i].se)){
curr=temp;
}
}
curr=tkd[curr][0];
//cout<<curr<<" "<<span2[curr].fi<<" "<<span2[curr].se<<endl;
//cout<<endl;
for (auto &it:r[i]){
if (!s[curr].count(it.fi)) rage();
if (!s[curr].count(it.se+1)) rage();
}
//cout<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n>>m>>k;
int t;
rep(x,1,n+1){
cin>>t;
if (t==0) root=x;
else al[t].pub(x);
}
memset(tkd,-1,sizeof(tkd));
dfs1(root);
//rep(x,1,n+1) cout<<span[x].fi<<" "<<span[x].se<<endl;
// rep(x,1,n+1){
// rep(y,0,2) cout<<tkd[x][y]<<" "; cout<<endl;
// }
rep(x,1,300005) al[x].clear();
rep(x,1,m+1){
cin>>t;
if (t==0) root=x;
else al[t].pub(x);
}
dfs2(root);
cout<<"YES"<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
58944 KB |
Output is correct |
2 |
Correct |
33 ms |
58940 KB |
Output is correct |
3 |
Correct |
35 ms |
58956 KB |
Output is correct |
4 |
Correct |
32 ms |
59000 KB |
Output is correct |
5 |
Correct |
29 ms |
59040 KB |
Output is correct |
6 |
Correct |
36 ms |
58948 KB |
Output is correct |
7 |
Correct |
29 ms |
59000 KB |
Output is correct |
8 |
Correct |
29 ms |
59048 KB |
Output is correct |
9 |
Correct |
31 ms |
59020 KB |
Output is correct |
10 |
Correct |
33 ms |
59016 KB |
Output is correct |
11 |
Correct |
31 ms |
59004 KB |
Output is correct |
12 |
Correct |
30 ms |
58956 KB |
Output is correct |
13 |
Correct |
30 ms |
58960 KB |
Output is correct |
14 |
Correct |
35 ms |
58976 KB |
Output is correct |
15 |
Correct |
38 ms |
59040 KB |
Output is correct |
16 |
Correct |
31 ms |
59112 KB |
Output is correct |
17 |
Correct |
33 ms |
59084 KB |
Output is correct |
18 |
Correct |
29 ms |
58972 KB |
Output is correct |
19 |
Correct |
32 ms |
59068 KB |
Output is correct |
20 |
Correct |
30 ms |
59024 KB |
Output is correct |
21 |
Correct |
31 ms |
59020 KB |
Output is correct |
22 |
Correct |
30 ms |
59044 KB |
Output is correct |
23 |
Correct |
31 ms |
59012 KB |
Output is correct |
24 |
Correct |
32 ms |
59096 KB |
Output is correct |
25 |
Correct |
32 ms |
59032 KB |
Output is correct |
26 |
Correct |
32 ms |
58984 KB |
Output is correct |
27 |
Correct |
34 ms |
58956 KB |
Output is correct |
28 |
Correct |
31 ms |
59028 KB |
Output is correct |
29 |
Correct |
31 ms |
59052 KB |
Output is correct |
30 |
Correct |
33 ms |
59032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
58944 KB |
Output is correct |
2 |
Correct |
33 ms |
58940 KB |
Output is correct |
3 |
Correct |
35 ms |
58956 KB |
Output is correct |
4 |
Correct |
32 ms |
59000 KB |
Output is correct |
5 |
Correct |
29 ms |
59040 KB |
Output is correct |
6 |
Correct |
36 ms |
58948 KB |
Output is correct |
7 |
Correct |
29 ms |
59000 KB |
Output is correct |
8 |
Correct |
29 ms |
59048 KB |
Output is correct |
9 |
Correct |
31 ms |
59020 KB |
Output is correct |
10 |
Correct |
33 ms |
59016 KB |
Output is correct |
11 |
Correct |
31 ms |
59004 KB |
Output is correct |
12 |
Correct |
30 ms |
58956 KB |
Output is correct |
13 |
Correct |
30 ms |
58960 KB |
Output is correct |
14 |
Correct |
35 ms |
58976 KB |
Output is correct |
15 |
Correct |
38 ms |
59040 KB |
Output is correct |
16 |
Correct |
31 ms |
59112 KB |
Output is correct |
17 |
Correct |
33 ms |
59084 KB |
Output is correct |
18 |
Correct |
29 ms |
58972 KB |
Output is correct |
19 |
Correct |
32 ms |
59068 KB |
Output is correct |
20 |
Correct |
30 ms |
59024 KB |
Output is correct |
21 |
Correct |
31 ms |
59020 KB |
Output is correct |
22 |
Correct |
30 ms |
59044 KB |
Output is correct |
23 |
Correct |
31 ms |
59012 KB |
Output is correct |
24 |
Correct |
32 ms |
59096 KB |
Output is correct |
25 |
Correct |
32 ms |
59032 KB |
Output is correct |
26 |
Correct |
32 ms |
58984 KB |
Output is correct |
27 |
Correct |
34 ms |
58956 KB |
Output is correct |
28 |
Correct |
31 ms |
59028 KB |
Output is correct |
29 |
Correct |
31 ms |
59052 KB |
Output is correct |
30 |
Correct |
33 ms |
59032 KB |
Output is correct |
31 |
Correct |
30 ms |
58984 KB |
Output is correct |
32 |
Correct |
31 ms |
59008 KB |
Output is correct |
33 |
Correct |
37 ms |
59044 KB |
Output is correct |
34 |
Correct |
30 ms |
58972 KB |
Output is correct |
35 |
Correct |
33 ms |
58996 KB |
Output is correct |
36 |
Correct |
32 ms |
59076 KB |
Output is correct |
37 |
Correct |
30 ms |
58992 KB |
Output is correct |
38 |
Correct |
30 ms |
59020 KB |
Output is correct |
39 |
Correct |
32 ms |
58996 KB |
Output is correct |
40 |
Correct |
30 ms |
59088 KB |
Output is correct |
41 |
Correct |
30 ms |
58996 KB |
Output is correct |
42 |
Correct |
33 ms |
59016 KB |
Output is correct |
43 |
Correct |
35 ms |
58928 KB |
Output is correct |
44 |
Correct |
30 ms |
58960 KB |
Output is correct |
45 |
Correct |
31 ms |
58988 KB |
Output is correct |
46 |
Correct |
31 ms |
59032 KB |
Output is correct |
47 |
Correct |
31 ms |
58992 KB |
Output is correct |
48 |
Correct |
33 ms |
59084 KB |
Output is correct |
49 |
Correct |
35 ms |
59064 KB |
Output is correct |
50 |
Correct |
30 ms |
58992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
58944 KB |
Output is correct |
2 |
Correct |
33 ms |
58940 KB |
Output is correct |
3 |
Correct |
35 ms |
58956 KB |
Output is correct |
4 |
Correct |
32 ms |
59000 KB |
Output is correct |
5 |
Correct |
29 ms |
59040 KB |
Output is correct |
6 |
Correct |
36 ms |
58948 KB |
Output is correct |
7 |
Correct |
29 ms |
59000 KB |
Output is correct |
8 |
Correct |
29 ms |
59048 KB |
Output is correct |
9 |
Correct |
31 ms |
59020 KB |
Output is correct |
10 |
Correct |
33 ms |
59016 KB |
Output is correct |
11 |
Correct |
31 ms |
59004 KB |
Output is correct |
12 |
Correct |
30 ms |
58956 KB |
Output is correct |
13 |
Correct |
30 ms |
58960 KB |
Output is correct |
14 |
Correct |
35 ms |
58976 KB |
Output is correct |
15 |
Correct |
38 ms |
59040 KB |
Output is correct |
16 |
Correct |
31 ms |
59112 KB |
Output is correct |
17 |
Correct |
33 ms |
59084 KB |
Output is correct |
18 |
Correct |
29 ms |
58972 KB |
Output is correct |
19 |
Correct |
32 ms |
59068 KB |
Output is correct |
20 |
Correct |
30 ms |
59024 KB |
Output is correct |
21 |
Correct |
31 ms |
59020 KB |
Output is correct |
22 |
Correct |
30 ms |
59044 KB |
Output is correct |
23 |
Correct |
31 ms |
59012 KB |
Output is correct |
24 |
Correct |
32 ms |
59096 KB |
Output is correct |
25 |
Correct |
32 ms |
59032 KB |
Output is correct |
26 |
Correct |
32 ms |
58984 KB |
Output is correct |
27 |
Correct |
34 ms |
58956 KB |
Output is correct |
28 |
Correct |
31 ms |
59028 KB |
Output is correct |
29 |
Correct |
31 ms |
59052 KB |
Output is correct |
30 |
Correct |
33 ms |
59032 KB |
Output is correct |
31 |
Correct |
30 ms |
58984 KB |
Output is correct |
32 |
Correct |
31 ms |
59008 KB |
Output is correct |
33 |
Correct |
37 ms |
59044 KB |
Output is correct |
34 |
Correct |
30 ms |
58972 KB |
Output is correct |
35 |
Correct |
33 ms |
58996 KB |
Output is correct |
36 |
Correct |
32 ms |
59076 KB |
Output is correct |
37 |
Correct |
30 ms |
58992 KB |
Output is correct |
38 |
Correct |
30 ms |
59020 KB |
Output is correct |
39 |
Correct |
32 ms |
58996 KB |
Output is correct |
40 |
Correct |
30 ms |
59088 KB |
Output is correct |
41 |
Correct |
30 ms |
58996 KB |
Output is correct |
42 |
Correct |
33 ms |
59016 KB |
Output is correct |
43 |
Correct |
35 ms |
58928 KB |
Output is correct |
44 |
Correct |
30 ms |
58960 KB |
Output is correct |
45 |
Correct |
31 ms |
58988 KB |
Output is correct |
46 |
Correct |
31 ms |
59032 KB |
Output is correct |
47 |
Correct |
31 ms |
58992 KB |
Output is correct |
48 |
Correct |
33 ms |
59084 KB |
Output is correct |
49 |
Correct |
35 ms |
59064 KB |
Output is correct |
50 |
Correct |
30 ms |
58992 KB |
Output is correct |
51 |
Correct |
34 ms |
59768 KB |
Output is correct |
52 |
Correct |
37 ms |
59828 KB |
Output is correct |
53 |
Correct |
39 ms |
59808 KB |
Output is correct |
54 |
Correct |
41 ms |
59724 KB |
Output is correct |
55 |
Correct |
38 ms |
59972 KB |
Output is correct |
56 |
Correct |
39 ms |
59876 KB |
Output is correct |
57 |
Correct |
38 ms |
59912 KB |
Output is correct |
58 |
Correct |
35 ms |
59692 KB |
Output is correct |
59 |
Correct |
35 ms |
59852 KB |
Output is correct |
60 |
Correct |
35 ms |
59836 KB |
Output is correct |
61 |
Correct |
34 ms |
60152 KB |
Output is correct |
62 |
Correct |
34 ms |
60128 KB |
Output is correct |
63 |
Correct |
42 ms |
60220 KB |
Output is correct |
64 |
Correct |
37 ms |
59312 KB |
Output is correct |
65 |
Correct |
30 ms |
59124 KB |
Output is correct |
66 |
Correct |
36 ms |
59708 KB |
Output is correct |
67 |
Correct |
36 ms |
59572 KB |
Output is correct |
68 |
Correct |
37 ms |
59380 KB |
Output is correct |
69 |
Correct |
36 ms |
59588 KB |
Output is correct |
70 |
Correct |
36 ms |
59604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
58944 KB |
Output is correct |
2 |
Correct |
33 ms |
58940 KB |
Output is correct |
3 |
Correct |
35 ms |
58956 KB |
Output is correct |
4 |
Correct |
32 ms |
59000 KB |
Output is correct |
5 |
Correct |
29 ms |
59040 KB |
Output is correct |
6 |
Correct |
36 ms |
58948 KB |
Output is correct |
7 |
Correct |
29 ms |
59000 KB |
Output is correct |
8 |
Correct |
29 ms |
59048 KB |
Output is correct |
9 |
Correct |
31 ms |
59020 KB |
Output is correct |
10 |
Correct |
33 ms |
59016 KB |
Output is correct |
11 |
Correct |
31 ms |
59004 KB |
Output is correct |
12 |
Correct |
30 ms |
58956 KB |
Output is correct |
13 |
Correct |
30 ms |
58960 KB |
Output is correct |
14 |
Correct |
35 ms |
58976 KB |
Output is correct |
15 |
Correct |
38 ms |
59040 KB |
Output is correct |
16 |
Correct |
31 ms |
59112 KB |
Output is correct |
17 |
Correct |
33 ms |
59084 KB |
Output is correct |
18 |
Correct |
29 ms |
58972 KB |
Output is correct |
19 |
Correct |
32 ms |
59068 KB |
Output is correct |
20 |
Correct |
30 ms |
59024 KB |
Output is correct |
21 |
Correct |
31 ms |
59020 KB |
Output is correct |
22 |
Correct |
30 ms |
59044 KB |
Output is correct |
23 |
Correct |
31 ms |
59012 KB |
Output is correct |
24 |
Correct |
32 ms |
59096 KB |
Output is correct |
25 |
Correct |
32 ms |
59032 KB |
Output is correct |
26 |
Correct |
32 ms |
58984 KB |
Output is correct |
27 |
Correct |
34 ms |
58956 KB |
Output is correct |
28 |
Correct |
31 ms |
59028 KB |
Output is correct |
29 |
Correct |
31 ms |
59052 KB |
Output is correct |
30 |
Correct |
33 ms |
59032 KB |
Output is correct |
31 |
Correct |
30 ms |
58984 KB |
Output is correct |
32 |
Correct |
31 ms |
59008 KB |
Output is correct |
33 |
Correct |
37 ms |
59044 KB |
Output is correct |
34 |
Correct |
30 ms |
58972 KB |
Output is correct |
35 |
Correct |
33 ms |
58996 KB |
Output is correct |
36 |
Correct |
32 ms |
59076 KB |
Output is correct |
37 |
Correct |
30 ms |
58992 KB |
Output is correct |
38 |
Correct |
30 ms |
59020 KB |
Output is correct |
39 |
Correct |
32 ms |
58996 KB |
Output is correct |
40 |
Correct |
30 ms |
59088 KB |
Output is correct |
41 |
Correct |
30 ms |
58996 KB |
Output is correct |
42 |
Correct |
33 ms |
59016 KB |
Output is correct |
43 |
Correct |
35 ms |
58928 KB |
Output is correct |
44 |
Correct |
30 ms |
58960 KB |
Output is correct |
45 |
Correct |
31 ms |
58988 KB |
Output is correct |
46 |
Correct |
31 ms |
59032 KB |
Output is correct |
47 |
Correct |
31 ms |
58992 KB |
Output is correct |
48 |
Correct |
33 ms |
59084 KB |
Output is correct |
49 |
Correct |
35 ms |
59064 KB |
Output is correct |
50 |
Correct |
30 ms |
58992 KB |
Output is correct |
51 |
Correct |
34 ms |
59768 KB |
Output is correct |
52 |
Correct |
37 ms |
59828 KB |
Output is correct |
53 |
Correct |
39 ms |
59808 KB |
Output is correct |
54 |
Correct |
41 ms |
59724 KB |
Output is correct |
55 |
Correct |
38 ms |
59972 KB |
Output is correct |
56 |
Correct |
39 ms |
59876 KB |
Output is correct |
57 |
Correct |
38 ms |
59912 KB |
Output is correct |
58 |
Correct |
35 ms |
59692 KB |
Output is correct |
59 |
Correct |
35 ms |
59852 KB |
Output is correct |
60 |
Correct |
35 ms |
59836 KB |
Output is correct |
61 |
Correct |
34 ms |
60152 KB |
Output is correct |
62 |
Correct |
34 ms |
60128 KB |
Output is correct |
63 |
Correct |
42 ms |
60220 KB |
Output is correct |
64 |
Correct |
37 ms |
59312 KB |
Output is correct |
65 |
Correct |
30 ms |
59124 KB |
Output is correct |
66 |
Correct |
36 ms |
59708 KB |
Output is correct |
67 |
Correct |
36 ms |
59572 KB |
Output is correct |
68 |
Correct |
37 ms |
59380 KB |
Output is correct |
69 |
Correct |
36 ms |
59588 KB |
Output is correct |
70 |
Correct |
36 ms |
59604 KB |
Output is correct |
71 |
Correct |
89 ms |
66680 KB |
Output is correct |
72 |
Correct |
91 ms |
67396 KB |
Output is correct |
73 |
Correct |
73 ms |
62564 KB |
Output is correct |
74 |
Correct |
99 ms |
70744 KB |
Output is correct |
75 |
Correct |
94 ms |
69136 KB |
Output is correct |
76 |
Correct |
662 ms |
134384 KB |
Output is correct |
77 |
Correct |
601 ms |
111532 KB |
Output is correct |
78 |
Correct |
503 ms |
107716 KB |
Output is correct |
79 |
Correct |
524 ms |
114176 KB |
Output is correct |
80 |
Correct |
517 ms |
106176 KB |
Output is correct |
81 |
Correct |
89 ms |
66332 KB |
Output is correct |
82 |
Correct |
72 ms |
65724 KB |
Output is correct |
83 |
Correct |
54 ms |
62656 KB |
Output is correct |
84 |
Correct |
90 ms |
70316 KB |
Output is correct |
85 |
Correct |
86 ms |
68552 KB |
Output is correct |
86 |
Correct |
633 ms |
129680 KB |
Output is correct |
87 |
Correct |
655 ms |
129528 KB |
Output is correct |
88 |
Correct |
324 ms |
98236 KB |
Output is correct |
89 |
Correct |
398 ms |
112456 KB |
Output is correct |
90 |
Correct |
448 ms |
105924 KB |
Output is correct |