/*input
7 8 4
7 7 6 6 0 5 5
6 7 7 8 0 5 5 5
*/
#include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define f first
#define s second
#define sz(x) (int)x.size()
const int maxn=3e5+5;
vector<int> v[maxn],v2[maxn];
int sz[maxn],sz2[maxn];
int id[maxn],id2[maxn];
int k;
void dfs(int u){
if(u<k) id[u]=u,sz[u]=1;
for(int x:v[u]){
dfs(x);
id[u]=id[x];
sz[u]+=sz[x];
}
}
void dfs2(int u){
if(u<k) id2[u]=u,sz2[u]=1;
for(int x:v2[u]){
dfs2(x);
id2[u]=id2[x];
sz2[u]+=sz2[x];
}
}
int par[maxn],rank1[maxn];
int find(int u){
if(par[u]==u) return u;
return par[u]=find(par[u]);
}
void merge(int a,int b){
a=find(a),b=find(b);
if(a==b) return;
par[a]=b;
rank1[b]+=rank1[a];
}
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int n,m;
cin>>n>>m>>k;
int r1,r2;
REP(i,n){
int x;
cin>>x;
--x;
if(x!=-1) v[x].pb(i);
else r1=i;
}
REP(i,m){
int x;
cin>>x;
--x;
if(x!=-1) v2[x].pb(i);
else r2=i;
}
dfs(r1),dfs2(r2);
vector<pii> va,vb;
REP(i,n) if(sz(v[i])>1){
va.pb({sz[i],i});
}
REP(i,m) if(sz(v2[i])>1){
vb.pb({sz2[i],i});
}
sort(ALL(va)),sort(ALL(vb));
int p=0;
REP(i,k) par[i]=i,rank1[i]=1;
REP(i,sz(va)){
while(p<sz(vb) and vb[p].f<=va[i].f){
for(auto x:v2[vb[p].s]) merge(id2[x],id2[vb[p].s]);
++p;
}
for(auto x:v[va[i].s]) merge(id[x],id[va[i].s]);
if(rank1[find(id[va[i].s])]!=va[i].f){
cout<<"NO\n";
return 0;
}
}
cout<<"YES\n";
}
Compilation message
family.cpp: In function 'int main()':
family.cpp:70:17: warning: 'r2' may be used uninitialized in this function [-Wmaybe-uninitialized]
70 | dfs(r1),dfs2(r2);
| ~~~~^~~~
family.cpp:70:8: warning: 'r1' may be used uninitialized in this function [-Wmaybe-uninitialized]
70 | dfs(r1),dfs2(r2);
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 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 |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
8 ms |
14452 KB |
Output is correct |
15 |
Correct |
10 ms |
14456 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
8 ms |
14412 KB |
Output is correct |
19 |
Correct |
8 ms |
14412 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14412 KB |
Output is correct |
22 |
Correct |
9 ms |
14412 KB |
Output is correct |
23 |
Correct |
9 ms |
14436 KB |
Output is correct |
24 |
Correct |
9 ms |
14420 KB |
Output is correct |
25 |
Correct |
9 ms |
14364 KB |
Output is correct |
26 |
Correct |
9 ms |
14388 KB |
Output is correct |
27 |
Correct |
9 ms |
14412 KB |
Output is correct |
28 |
Correct |
9 ms |
14412 KB |
Output is correct |
29 |
Correct |
9 ms |
14412 KB |
Output is correct |
30 |
Correct |
9 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 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 |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
8 ms |
14452 KB |
Output is correct |
15 |
Correct |
10 ms |
14456 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
8 ms |
14412 KB |
Output is correct |
19 |
Correct |
8 ms |
14412 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14412 KB |
Output is correct |
22 |
Correct |
9 ms |
14412 KB |
Output is correct |
23 |
Correct |
9 ms |
14436 KB |
Output is correct |
24 |
Correct |
9 ms |
14420 KB |
Output is correct |
25 |
Correct |
9 ms |
14364 KB |
Output is correct |
26 |
Correct |
9 ms |
14388 KB |
Output is correct |
27 |
Correct |
9 ms |
14412 KB |
Output is correct |
28 |
Correct |
9 ms |
14412 KB |
Output is correct |
29 |
Correct |
9 ms |
14412 KB |
Output is correct |
30 |
Correct |
9 ms |
14412 KB |
Output is correct |
31 |
Correct |
8 ms |
14412 KB |
Output is correct |
32 |
Correct |
8 ms |
14412 KB |
Output is correct |
33 |
Correct |
9 ms |
14412 KB |
Output is correct |
34 |
Correct |
10 ms |
14428 KB |
Output is correct |
35 |
Correct |
9 ms |
14412 KB |
Output is correct |
36 |
Correct |
9 ms |
14444 KB |
Output is correct |
37 |
Correct |
9 ms |
14380 KB |
Output is correct |
38 |
Correct |
8 ms |
14460 KB |
Output is correct |
39 |
Correct |
9 ms |
14412 KB |
Output is correct |
40 |
Correct |
9 ms |
14412 KB |
Output is correct |
41 |
Correct |
8 ms |
14548 KB |
Output is correct |
42 |
Correct |
9 ms |
14412 KB |
Output is correct |
43 |
Correct |
9 ms |
14340 KB |
Output is correct |
44 |
Correct |
9 ms |
14412 KB |
Output is correct |
45 |
Correct |
9 ms |
14412 KB |
Output is correct |
46 |
Correct |
9 ms |
14412 KB |
Output is correct |
47 |
Correct |
9 ms |
14412 KB |
Output is correct |
48 |
Correct |
9 ms |
14412 KB |
Output is correct |
49 |
Correct |
9 ms |
14412 KB |
Output is correct |
50 |
Correct |
8 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 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 |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
8 ms |
14452 KB |
Output is correct |
15 |
Correct |
10 ms |
14456 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
8 ms |
14412 KB |
Output is correct |
19 |
Correct |
8 ms |
14412 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14412 KB |
Output is correct |
22 |
Correct |
9 ms |
14412 KB |
Output is correct |
23 |
Correct |
9 ms |
14436 KB |
Output is correct |
24 |
Correct |
9 ms |
14420 KB |
Output is correct |
25 |
Correct |
9 ms |
14364 KB |
Output is correct |
26 |
Correct |
9 ms |
14388 KB |
Output is correct |
27 |
Correct |
9 ms |
14412 KB |
Output is correct |
28 |
Correct |
9 ms |
14412 KB |
Output is correct |
29 |
Correct |
9 ms |
14412 KB |
Output is correct |
30 |
Correct |
9 ms |
14412 KB |
Output is correct |
31 |
Correct |
8 ms |
14412 KB |
Output is correct |
32 |
Correct |
8 ms |
14412 KB |
Output is correct |
33 |
Correct |
9 ms |
14412 KB |
Output is correct |
34 |
Correct |
10 ms |
14428 KB |
Output is correct |
35 |
Correct |
9 ms |
14412 KB |
Output is correct |
36 |
Correct |
9 ms |
14444 KB |
Output is correct |
37 |
Correct |
9 ms |
14380 KB |
Output is correct |
38 |
Correct |
8 ms |
14460 KB |
Output is correct |
39 |
Correct |
9 ms |
14412 KB |
Output is correct |
40 |
Correct |
9 ms |
14412 KB |
Output is correct |
41 |
Correct |
8 ms |
14548 KB |
Output is correct |
42 |
Correct |
9 ms |
14412 KB |
Output is correct |
43 |
Correct |
9 ms |
14340 KB |
Output is correct |
44 |
Correct |
9 ms |
14412 KB |
Output is correct |
45 |
Correct |
9 ms |
14412 KB |
Output is correct |
46 |
Correct |
9 ms |
14412 KB |
Output is correct |
47 |
Correct |
9 ms |
14412 KB |
Output is correct |
48 |
Correct |
9 ms |
14412 KB |
Output is correct |
49 |
Correct |
9 ms |
14412 KB |
Output is correct |
50 |
Correct |
8 ms |
14412 KB |
Output is correct |
51 |
Correct |
12 ms |
14540 KB |
Output is correct |
52 |
Correct |
11 ms |
14668 KB |
Output is correct |
53 |
Correct |
11 ms |
14636 KB |
Output is correct |
54 |
Correct |
11 ms |
14624 KB |
Output is correct |
55 |
Correct |
10 ms |
14540 KB |
Output is correct |
56 |
Correct |
10 ms |
14540 KB |
Output is correct |
57 |
Correct |
10 ms |
14516 KB |
Output is correct |
58 |
Correct |
10 ms |
14532 KB |
Output is correct |
59 |
Correct |
10 ms |
14532 KB |
Output is correct |
60 |
Correct |
10 ms |
14600 KB |
Output is correct |
61 |
Correct |
11 ms |
14860 KB |
Output is correct |
62 |
Correct |
11 ms |
14928 KB |
Output is correct |
63 |
Correct |
10 ms |
14596 KB |
Output is correct |
64 |
Correct |
10 ms |
14540 KB |
Output is correct |
65 |
Correct |
9 ms |
14412 KB |
Output is correct |
66 |
Correct |
10 ms |
14668 KB |
Output is correct |
67 |
Correct |
10 ms |
14584 KB |
Output is correct |
68 |
Correct |
10 ms |
14592 KB |
Output is correct |
69 |
Correct |
11 ms |
14636 KB |
Output is correct |
70 |
Correct |
10 ms |
14528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 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 |
14412 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14412 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
8 ms |
14412 KB |
Output is correct |
9 |
Correct |
8 ms |
14412 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
8 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14412 KB |
Output is correct |
14 |
Correct |
8 ms |
14452 KB |
Output is correct |
15 |
Correct |
10 ms |
14456 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
8 ms |
14412 KB |
Output is correct |
19 |
Correct |
8 ms |
14412 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14412 KB |
Output is correct |
22 |
Correct |
9 ms |
14412 KB |
Output is correct |
23 |
Correct |
9 ms |
14436 KB |
Output is correct |
24 |
Correct |
9 ms |
14420 KB |
Output is correct |
25 |
Correct |
9 ms |
14364 KB |
Output is correct |
26 |
Correct |
9 ms |
14388 KB |
Output is correct |
27 |
Correct |
9 ms |
14412 KB |
Output is correct |
28 |
Correct |
9 ms |
14412 KB |
Output is correct |
29 |
Correct |
9 ms |
14412 KB |
Output is correct |
30 |
Correct |
9 ms |
14412 KB |
Output is correct |
31 |
Correct |
8 ms |
14412 KB |
Output is correct |
32 |
Correct |
8 ms |
14412 KB |
Output is correct |
33 |
Correct |
9 ms |
14412 KB |
Output is correct |
34 |
Correct |
10 ms |
14428 KB |
Output is correct |
35 |
Correct |
9 ms |
14412 KB |
Output is correct |
36 |
Correct |
9 ms |
14444 KB |
Output is correct |
37 |
Correct |
9 ms |
14380 KB |
Output is correct |
38 |
Correct |
8 ms |
14460 KB |
Output is correct |
39 |
Correct |
9 ms |
14412 KB |
Output is correct |
40 |
Correct |
9 ms |
14412 KB |
Output is correct |
41 |
Correct |
8 ms |
14548 KB |
Output is correct |
42 |
Correct |
9 ms |
14412 KB |
Output is correct |
43 |
Correct |
9 ms |
14340 KB |
Output is correct |
44 |
Correct |
9 ms |
14412 KB |
Output is correct |
45 |
Correct |
9 ms |
14412 KB |
Output is correct |
46 |
Correct |
9 ms |
14412 KB |
Output is correct |
47 |
Correct |
9 ms |
14412 KB |
Output is correct |
48 |
Correct |
9 ms |
14412 KB |
Output is correct |
49 |
Correct |
9 ms |
14412 KB |
Output is correct |
50 |
Correct |
8 ms |
14412 KB |
Output is correct |
51 |
Correct |
12 ms |
14540 KB |
Output is correct |
52 |
Correct |
11 ms |
14668 KB |
Output is correct |
53 |
Correct |
11 ms |
14636 KB |
Output is correct |
54 |
Correct |
11 ms |
14624 KB |
Output is correct |
55 |
Correct |
10 ms |
14540 KB |
Output is correct |
56 |
Correct |
10 ms |
14540 KB |
Output is correct |
57 |
Correct |
10 ms |
14516 KB |
Output is correct |
58 |
Correct |
10 ms |
14532 KB |
Output is correct |
59 |
Correct |
10 ms |
14532 KB |
Output is correct |
60 |
Correct |
10 ms |
14600 KB |
Output is correct |
61 |
Correct |
11 ms |
14860 KB |
Output is correct |
62 |
Correct |
11 ms |
14928 KB |
Output is correct |
63 |
Correct |
10 ms |
14596 KB |
Output is correct |
64 |
Correct |
10 ms |
14540 KB |
Output is correct |
65 |
Correct |
9 ms |
14412 KB |
Output is correct |
66 |
Correct |
10 ms |
14668 KB |
Output is correct |
67 |
Correct |
10 ms |
14584 KB |
Output is correct |
68 |
Correct |
10 ms |
14592 KB |
Output is correct |
69 |
Correct |
11 ms |
14636 KB |
Output is correct |
70 |
Correct |
10 ms |
14528 KB |
Output is correct |
71 |
Correct |
35 ms |
17688 KB |
Output is correct |
72 |
Correct |
36 ms |
17092 KB |
Output is correct |
73 |
Correct |
23 ms |
16588 KB |
Output is correct |
74 |
Correct |
36 ms |
19736 KB |
Output is correct |
75 |
Correct |
36 ms |
19088 KB |
Output is correct |
76 |
Correct |
300 ms |
48892 KB |
Output is correct |
77 |
Correct |
294 ms |
38596 KB |
Output is correct |
78 |
Correct |
205 ms |
27376 KB |
Output is correct |
79 |
Correct |
246 ms |
41036 KB |
Output is correct |
80 |
Correct |
259 ms |
36708 KB |
Output is correct |
81 |
Correct |
35 ms |
17632 KB |
Output is correct |
82 |
Correct |
34 ms |
17032 KB |
Output is correct |
83 |
Correct |
22 ms |
16708 KB |
Output is correct |
84 |
Correct |
36 ms |
19600 KB |
Output is correct |
85 |
Correct |
36 ms |
18980 KB |
Output is correct |
86 |
Correct |
241 ms |
42096 KB |
Output is correct |
87 |
Correct |
240 ms |
42168 KB |
Output is correct |
88 |
Correct |
170 ms |
27316 KB |
Output is correct |
89 |
Correct |
248 ms |
40260 KB |
Output is correct |
90 |
Correct |
260 ms |
36920 KB |
Output is correct |