//雪花飄飄北風嘯嘯
//天地一片蒼茫
#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());
int n1,n2,k;
vector<int> al[600010];
int root1,root2;
vector<int> cc[600010];
int ss[600010];
vector<int> proc;
int dfs(int i){
if (i<=k || (300005<i && i<=k+300005)){
ss[i]=1;
return i%300005;
}
else{
for (auto &it:al[i]){
cc[i].pub(dfs(it));
ss[i]+=ss[it];
}
proc.pub(i);
return cc[i][0];
}
}
struct DSU{
int p[300005];
int s[300005];
DSU(){
rep(x,0,300005) p[x]=x,s[x]=1;
}
int par(int i){
if (p[i]==i) return i;
else return p[i]=par(p[i]);
}
void unions(int i,int j){
i=par(i),j=par(j);
if (i==j) return;
if (s[i]>s[j]) swap(i,j);
p[i]=j;
s[j]+=s[i];
}
} dsu=DSU();
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n1>>n2>>k;
int t;
rep(x,1,n1+1){
cin>>t;
if (t==0) root1=x;
else al[t].pub(x);
}
rep(x,1,n2+1){
cin>>t;
if (t==0) root2=x+300005;
else al[t+300005].pub(x+300005);
}
dfs(root1);
dfs(root2);
//rep(x,1,n1+1) cout<<ss[x]<<" "; cout<<endl;
sort(all(proc),[](int i,int j){
return ss[i]<ss[j];
});
for (auto &it:proc){
if (sz(cc[it])==1) continue;
//cout<<it<<endl;
//for (auto &it2:cc[it]) cout<<it2<<" "; cout<<endl;
rep(x,0,sz(cc[it])-1){
dsu.unions(cc[it][x],cc[it][x+1]);
}
if (dsu.s[dsu.par(cc[it][0])]!=ss[it]){
cout<<"NO"<<endl;
return 0;
}
}
cout<<"YES"<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
30796 KB |
Output is correct |
2 |
Correct |
22 ms |
30788 KB |
Output is correct |
3 |
Correct |
20 ms |
30840 KB |
Output is correct |
4 |
Correct |
19 ms |
30836 KB |
Output is correct |
5 |
Correct |
20 ms |
30748 KB |
Output is correct |
6 |
Correct |
21 ms |
30796 KB |
Output is correct |
7 |
Correct |
20 ms |
30760 KB |
Output is correct |
8 |
Correct |
20 ms |
30796 KB |
Output is correct |
9 |
Correct |
21 ms |
30796 KB |
Output is correct |
10 |
Correct |
20 ms |
30856 KB |
Output is correct |
11 |
Correct |
20 ms |
30856 KB |
Output is correct |
12 |
Correct |
21 ms |
30808 KB |
Output is correct |
13 |
Correct |
20 ms |
30796 KB |
Output is correct |
14 |
Correct |
23 ms |
30796 KB |
Output is correct |
15 |
Correct |
20 ms |
30888 KB |
Output is correct |
16 |
Correct |
21 ms |
30852 KB |
Output is correct |
17 |
Correct |
20 ms |
30796 KB |
Output is correct |
18 |
Correct |
20 ms |
30776 KB |
Output is correct |
19 |
Correct |
20 ms |
30884 KB |
Output is correct |
20 |
Correct |
20 ms |
30856 KB |
Output is correct |
21 |
Correct |
22 ms |
30856 KB |
Output is correct |
22 |
Correct |
20 ms |
30844 KB |
Output is correct |
23 |
Correct |
20 ms |
30860 KB |
Output is correct |
24 |
Correct |
21 ms |
30860 KB |
Output is correct |
25 |
Correct |
20 ms |
30796 KB |
Output is correct |
26 |
Correct |
19 ms |
30796 KB |
Output is correct |
27 |
Correct |
20 ms |
30844 KB |
Output is correct |
28 |
Correct |
20 ms |
30860 KB |
Output is correct |
29 |
Correct |
20 ms |
30796 KB |
Output is correct |
30 |
Correct |
20 ms |
30768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
30796 KB |
Output is correct |
2 |
Correct |
22 ms |
30788 KB |
Output is correct |
3 |
Correct |
20 ms |
30840 KB |
Output is correct |
4 |
Correct |
19 ms |
30836 KB |
Output is correct |
5 |
Correct |
20 ms |
30748 KB |
Output is correct |
6 |
Correct |
21 ms |
30796 KB |
Output is correct |
7 |
Correct |
20 ms |
30760 KB |
Output is correct |
8 |
Correct |
20 ms |
30796 KB |
Output is correct |
9 |
Correct |
21 ms |
30796 KB |
Output is correct |
10 |
Correct |
20 ms |
30856 KB |
Output is correct |
11 |
Correct |
20 ms |
30856 KB |
Output is correct |
12 |
Correct |
21 ms |
30808 KB |
Output is correct |
13 |
Correct |
20 ms |
30796 KB |
Output is correct |
14 |
Correct |
23 ms |
30796 KB |
Output is correct |
15 |
Correct |
20 ms |
30888 KB |
Output is correct |
16 |
Correct |
21 ms |
30852 KB |
Output is correct |
17 |
Correct |
20 ms |
30796 KB |
Output is correct |
18 |
Correct |
20 ms |
30776 KB |
Output is correct |
19 |
Correct |
20 ms |
30884 KB |
Output is correct |
20 |
Correct |
20 ms |
30856 KB |
Output is correct |
21 |
Correct |
22 ms |
30856 KB |
Output is correct |
22 |
Correct |
20 ms |
30844 KB |
Output is correct |
23 |
Correct |
20 ms |
30860 KB |
Output is correct |
24 |
Correct |
21 ms |
30860 KB |
Output is correct |
25 |
Correct |
20 ms |
30796 KB |
Output is correct |
26 |
Correct |
19 ms |
30796 KB |
Output is correct |
27 |
Correct |
20 ms |
30844 KB |
Output is correct |
28 |
Correct |
20 ms |
30860 KB |
Output is correct |
29 |
Correct |
20 ms |
30796 KB |
Output is correct |
30 |
Correct |
20 ms |
30768 KB |
Output is correct |
31 |
Correct |
20 ms |
30756 KB |
Output is correct |
32 |
Correct |
20 ms |
30832 KB |
Output is correct |
33 |
Correct |
20 ms |
30812 KB |
Output is correct |
34 |
Correct |
21 ms |
30796 KB |
Output is correct |
35 |
Correct |
20 ms |
30796 KB |
Output is correct |
36 |
Correct |
20 ms |
30856 KB |
Output is correct |
37 |
Correct |
20 ms |
30856 KB |
Output is correct |
38 |
Correct |
20 ms |
30796 KB |
Output is correct |
39 |
Correct |
20 ms |
30796 KB |
Output is correct |
40 |
Correct |
23 ms |
30844 KB |
Output is correct |
41 |
Correct |
19 ms |
30764 KB |
Output is correct |
42 |
Correct |
20 ms |
30796 KB |
Output is correct |
43 |
Correct |
20 ms |
30756 KB |
Output is correct |
44 |
Correct |
20 ms |
30856 KB |
Output is correct |
45 |
Correct |
20 ms |
30788 KB |
Output is correct |
46 |
Correct |
19 ms |
30796 KB |
Output is correct |
47 |
Correct |
20 ms |
30780 KB |
Output is correct |
48 |
Correct |
20 ms |
30824 KB |
Output is correct |
49 |
Correct |
24 ms |
30808 KB |
Output is correct |
50 |
Correct |
20 ms |
30856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
30796 KB |
Output is correct |
2 |
Correct |
22 ms |
30788 KB |
Output is correct |
3 |
Correct |
20 ms |
30840 KB |
Output is correct |
4 |
Correct |
19 ms |
30836 KB |
Output is correct |
5 |
Correct |
20 ms |
30748 KB |
Output is correct |
6 |
Correct |
21 ms |
30796 KB |
Output is correct |
7 |
Correct |
20 ms |
30760 KB |
Output is correct |
8 |
Correct |
20 ms |
30796 KB |
Output is correct |
9 |
Correct |
21 ms |
30796 KB |
Output is correct |
10 |
Correct |
20 ms |
30856 KB |
Output is correct |
11 |
Correct |
20 ms |
30856 KB |
Output is correct |
12 |
Correct |
21 ms |
30808 KB |
Output is correct |
13 |
Correct |
20 ms |
30796 KB |
Output is correct |
14 |
Correct |
23 ms |
30796 KB |
Output is correct |
15 |
Correct |
20 ms |
30888 KB |
Output is correct |
16 |
Correct |
21 ms |
30852 KB |
Output is correct |
17 |
Correct |
20 ms |
30796 KB |
Output is correct |
18 |
Correct |
20 ms |
30776 KB |
Output is correct |
19 |
Correct |
20 ms |
30884 KB |
Output is correct |
20 |
Correct |
20 ms |
30856 KB |
Output is correct |
21 |
Correct |
22 ms |
30856 KB |
Output is correct |
22 |
Correct |
20 ms |
30844 KB |
Output is correct |
23 |
Correct |
20 ms |
30860 KB |
Output is correct |
24 |
Correct |
21 ms |
30860 KB |
Output is correct |
25 |
Correct |
20 ms |
30796 KB |
Output is correct |
26 |
Correct |
19 ms |
30796 KB |
Output is correct |
27 |
Correct |
20 ms |
30844 KB |
Output is correct |
28 |
Correct |
20 ms |
30860 KB |
Output is correct |
29 |
Correct |
20 ms |
30796 KB |
Output is correct |
30 |
Correct |
20 ms |
30768 KB |
Output is correct |
31 |
Correct |
20 ms |
30756 KB |
Output is correct |
32 |
Correct |
20 ms |
30832 KB |
Output is correct |
33 |
Correct |
20 ms |
30812 KB |
Output is correct |
34 |
Correct |
21 ms |
30796 KB |
Output is correct |
35 |
Correct |
20 ms |
30796 KB |
Output is correct |
36 |
Correct |
20 ms |
30856 KB |
Output is correct |
37 |
Correct |
20 ms |
30856 KB |
Output is correct |
38 |
Correct |
20 ms |
30796 KB |
Output is correct |
39 |
Correct |
20 ms |
30796 KB |
Output is correct |
40 |
Correct |
23 ms |
30844 KB |
Output is correct |
41 |
Correct |
19 ms |
30764 KB |
Output is correct |
42 |
Correct |
20 ms |
30796 KB |
Output is correct |
43 |
Correct |
20 ms |
30756 KB |
Output is correct |
44 |
Correct |
20 ms |
30856 KB |
Output is correct |
45 |
Correct |
20 ms |
30788 KB |
Output is correct |
46 |
Correct |
19 ms |
30796 KB |
Output is correct |
47 |
Correct |
20 ms |
30780 KB |
Output is correct |
48 |
Correct |
20 ms |
30824 KB |
Output is correct |
49 |
Correct |
24 ms |
30808 KB |
Output is correct |
50 |
Correct |
20 ms |
30856 KB |
Output is correct |
51 |
Correct |
22 ms |
30964 KB |
Output is correct |
52 |
Correct |
23 ms |
31120 KB |
Output is correct |
53 |
Correct |
23 ms |
31180 KB |
Output is correct |
54 |
Correct |
22 ms |
31088 KB |
Output is correct |
55 |
Correct |
22 ms |
30984 KB |
Output is correct |
56 |
Correct |
26 ms |
31020 KB |
Output is correct |
57 |
Correct |
22 ms |
31052 KB |
Output is correct |
58 |
Correct |
24 ms |
31064 KB |
Output is correct |
59 |
Correct |
22 ms |
31052 KB |
Output is correct |
60 |
Correct |
21 ms |
31044 KB |
Output is correct |
61 |
Correct |
23 ms |
31504 KB |
Output is correct |
62 |
Correct |
24 ms |
31436 KB |
Output is correct |
63 |
Correct |
21 ms |
30992 KB |
Output is correct |
64 |
Correct |
22 ms |
31068 KB |
Output is correct |
65 |
Correct |
20 ms |
30832 KB |
Output is correct |
66 |
Correct |
24 ms |
31052 KB |
Output is correct |
67 |
Correct |
26 ms |
31072 KB |
Output is correct |
68 |
Correct |
23 ms |
31180 KB |
Output is correct |
69 |
Correct |
24 ms |
31148 KB |
Output is correct |
70 |
Correct |
22 ms |
30992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
30796 KB |
Output is correct |
2 |
Correct |
22 ms |
30788 KB |
Output is correct |
3 |
Correct |
20 ms |
30840 KB |
Output is correct |
4 |
Correct |
19 ms |
30836 KB |
Output is correct |
5 |
Correct |
20 ms |
30748 KB |
Output is correct |
6 |
Correct |
21 ms |
30796 KB |
Output is correct |
7 |
Correct |
20 ms |
30760 KB |
Output is correct |
8 |
Correct |
20 ms |
30796 KB |
Output is correct |
9 |
Correct |
21 ms |
30796 KB |
Output is correct |
10 |
Correct |
20 ms |
30856 KB |
Output is correct |
11 |
Correct |
20 ms |
30856 KB |
Output is correct |
12 |
Correct |
21 ms |
30808 KB |
Output is correct |
13 |
Correct |
20 ms |
30796 KB |
Output is correct |
14 |
Correct |
23 ms |
30796 KB |
Output is correct |
15 |
Correct |
20 ms |
30888 KB |
Output is correct |
16 |
Correct |
21 ms |
30852 KB |
Output is correct |
17 |
Correct |
20 ms |
30796 KB |
Output is correct |
18 |
Correct |
20 ms |
30776 KB |
Output is correct |
19 |
Correct |
20 ms |
30884 KB |
Output is correct |
20 |
Correct |
20 ms |
30856 KB |
Output is correct |
21 |
Correct |
22 ms |
30856 KB |
Output is correct |
22 |
Correct |
20 ms |
30844 KB |
Output is correct |
23 |
Correct |
20 ms |
30860 KB |
Output is correct |
24 |
Correct |
21 ms |
30860 KB |
Output is correct |
25 |
Correct |
20 ms |
30796 KB |
Output is correct |
26 |
Correct |
19 ms |
30796 KB |
Output is correct |
27 |
Correct |
20 ms |
30844 KB |
Output is correct |
28 |
Correct |
20 ms |
30860 KB |
Output is correct |
29 |
Correct |
20 ms |
30796 KB |
Output is correct |
30 |
Correct |
20 ms |
30768 KB |
Output is correct |
31 |
Correct |
20 ms |
30756 KB |
Output is correct |
32 |
Correct |
20 ms |
30832 KB |
Output is correct |
33 |
Correct |
20 ms |
30812 KB |
Output is correct |
34 |
Correct |
21 ms |
30796 KB |
Output is correct |
35 |
Correct |
20 ms |
30796 KB |
Output is correct |
36 |
Correct |
20 ms |
30856 KB |
Output is correct |
37 |
Correct |
20 ms |
30856 KB |
Output is correct |
38 |
Correct |
20 ms |
30796 KB |
Output is correct |
39 |
Correct |
20 ms |
30796 KB |
Output is correct |
40 |
Correct |
23 ms |
30844 KB |
Output is correct |
41 |
Correct |
19 ms |
30764 KB |
Output is correct |
42 |
Correct |
20 ms |
30796 KB |
Output is correct |
43 |
Correct |
20 ms |
30756 KB |
Output is correct |
44 |
Correct |
20 ms |
30856 KB |
Output is correct |
45 |
Correct |
20 ms |
30788 KB |
Output is correct |
46 |
Correct |
19 ms |
30796 KB |
Output is correct |
47 |
Correct |
20 ms |
30780 KB |
Output is correct |
48 |
Correct |
20 ms |
30824 KB |
Output is correct |
49 |
Correct |
24 ms |
30808 KB |
Output is correct |
50 |
Correct |
20 ms |
30856 KB |
Output is correct |
51 |
Correct |
22 ms |
30964 KB |
Output is correct |
52 |
Correct |
23 ms |
31120 KB |
Output is correct |
53 |
Correct |
23 ms |
31180 KB |
Output is correct |
54 |
Correct |
22 ms |
31088 KB |
Output is correct |
55 |
Correct |
22 ms |
30984 KB |
Output is correct |
56 |
Correct |
26 ms |
31020 KB |
Output is correct |
57 |
Correct |
22 ms |
31052 KB |
Output is correct |
58 |
Correct |
24 ms |
31064 KB |
Output is correct |
59 |
Correct |
22 ms |
31052 KB |
Output is correct |
60 |
Correct |
21 ms |
31044 KB |
Output is correct |
61 |
Correct |
23 ms |
31504 KB |
Output is correct |
62 |
Correct |
24 ms |
31436 KB |
Output is correct |
63 |
Correct |
21 ms |
30992 KB |
Output is correct |
64 |
Correct |
22 ms |
31068 KB |
Output is correct |
65 |
Correct |
20 ms |
30832 KB |
Output is correct |
66 |
Correct |
24 ms |
31052 KB |
Output is correct |
67 |
Correct |
26 ms |
31072 KB |
Output is correct |
68 |
Correct |
23 ms |
31180 KB |
Output is correct |
69 |
Correct |
24 ms |
31148 KB |
Output is correct |
70 |
Correct |
22 ms |
30992 KB |
Output is correct |
71 |
Correct |
63 ms |
36548 KB |
Output is correct |
72 |
Correct |
59 ms |
34588 KB |
Output is correct |
73 |
Correct |
44 ms |
34996 KB |
Output is correct |
74 |
Correct |
80 ms |
40520 KB |
Output is correct |
75 |
Correct |
68 ms |
39552 KB |
Output is correct |
76 |
Correct |
529 ms |
93432 KB |
Output is correct |
77 |
Correct |
531 ms |
78172 KB |
Output is correct |
78 |
Correct |
243 ms |
48184 KB |
Output is correct |
79 |
Correct |
438 ms |
79820 KB |
Output is correct |
80 |
Correct |
454 ms |
75632 KB |
Output is correct |
81 |
Correct |
64 ms |
36544 KB |
Output is correct |
82 |
Correct |
53 ms |
34492 KB |
Output is correct |
83 |
Correct |
45 ms |
35080 KB |
Output is correct |
84 |
Correct |
69 ms |
40252 KB |
Output is correct |
85 |
Correct |
67 ms |
39460 KB |
Output is correct |
86 |
Correct |
427 ms |
72240 KB |
Output is correct |
87 |
Correct |
387 ms |
72116 KB |
Output is correct |
88 |
Correct |
245 ms |
48204 KB |
Output is correct |
89 |
Correct |
426 ms |
78764 KB |
Output is correct |
90 |
Correct |
460 ms |
75860 KB |
Output is correct |