#include <stdio.h>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int,int> Pi;
/* Union Find data structure */
int pa[300500], cnt[300500];
int find(int x){
if( pa[x] == x ) return x;
return pa[x] = find(pa[x]);
}
void uni(int a, int b){
a = find(a); b = find(b);
if( a == b ) return;
pa[a] = b;
cnt[b] += cnt[a];
}
struct event{
int t, len, level, node;
// Smaller event first. Deeper event first.
bool operator < (event e){
if( len != e.len ) return len < e.len;
return level > e.level;
}
};
int N[2],K;
vector<int> E[2][300500];
int root[2];
int sz[2][300500], leaf[2][300500], level[2][300500];
int timer = 0;
Pi dfs(int t, int x, int l){
level[t][x] = l;
if( 1 <= x && x <= K ){
timer++;
leaf[t][x] = x;
sz[t][x] = 1;
return {x,1};
}
for(auto e : E[t][x]){
Pi res = dfs(t, e, l+1);
leaf[t][x] = res.first;
sz[t][x] += res.second;
}
return {leaf[t][x], sz[t][x]};
}
int main(){
scanf("%d%d%d",&N[0],&N[1],&K);
for(int i=1;i<=K;i++){
pa[i] = i; cnt[i] = 1;
}
for(int t=0;t<2;t++){
for(int i=1;i<=N[t];i++){
int x; scanf("%d",&x);
if( x == 0 ) root[t] = i;
else E[t][x].push_back(i);
}
}
dfs( 0, root[0] , 0);
timer = 0;
dfs( 1, root[1] , 0);
vector<event> events;
for(int t=0;t<2;t++){
for(int i=K+1;i<=N[t];i++){
events.push_back({t, sz[t][i], level[t][i], i});
}
}
sort(events.begin(), events.end());
for(auto e : events){
//printf("[%d,%d] %d %d \n",e.t, e.node, e.len, e.level);
for(auto x : E[e.t][e.node]){
uni(leaf[e.t][e.node], leaf[e.t][x]);
}
//printf("%d %d\n",cnt[find(leaf[e.t][e.node])], e.len);
if( cnt[find(leaf[e.t][e.node])] != e.len ){
printf("NO\n");
return 0;
}
}
printf("YES\n");
}
Compilation message
family.cpp: In function 'int main()':
family.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&N[0],&N[1],&K);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
family.cpp:64:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x; scanf("%d",&x);
~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14568 KB |
Output is correct |
3 |
Correct |
15 ms |
14644 KB |
Output is correct |
4 |
Correct |
15 ms |
14644 KB |
Output is correct |
5 |
Correct |
14 ms |
14644 KB |
Output is correct |
6 |
Correct |
15 ms |
14676 KB |
Output is correct |
7 |
Correct |
15 ms |
14676 KB |
Output is correct |
8 |
Correct |
18 ms |
14680 KB |
Output is correct |
9 |
Correct |
15 ms |
14860 KB |
Output is correct |
10 |
Correct |
14 ms |
14860 KB |
Output is correct |
11 |
Correct |
15 ms |
14860 KB |
Output is correct |
12 |
Correct |
15 ms |
14884 KB |
Output is correct |
13 |
Correct |
15 ms |
14888 KB |
Output is correct |
14 |
Correct |
17 ms |
14988 KB |
Output is correct |
15 |
Correct |
16 ms |
14988 KB |
Output is correct |
16 |
Correct |
15 ms |
14996 KB |
Output is correct |
17 |
Correct |
16 ms |
14996 KB |
Output is correct |
18 |
Correct |
15 ms |
15004 KB |
Output is correct |
19 |
Correct |
16 ms |
15004 KB |
Output is correct |
20 |
Correct |
17 ms |
15004 KB |
Output is correct |
21 |
Correct |
16 ms |
15004 KB |
Output is correct |
22 |
Correct |
15 ms |
15004 KB |
Output is correct |
23 |
Correct |
19 ms |
15004 KB |
Output is correct |
24 |
Correct |
18 ms |
15004 KB |
Output is correct |
25 |
Correct |
14 ms |
15004 KB |
Output is correct |
26 |
Correct |
20 ms |
15004 KB |
Output is correct |
27 |
Correct |
22 ms |
15004 KB |
Output is correct |
28 |
Correct |
22 ms |
15004 KB |
Output is correct |
29 |
Correct |
18 ms |
15004 KB |
Output is correct |
30 |
Correct |
15 ms |
15004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14568 KB |
Output is correct |
3 |
Correct |
15 ms |
14644 KB |
Output is correct |
4 |
Correct |
15 ms |
14644 KB |
Output is correct |
5 |
Correct |
14 ms |
14644 KB |
Output is correct |
6 |
Correct |
15 ms |
14676 KB |
Output is correct |
7 |
Correct |
15 ms |
14676 KB |
Output is correct |
8 |
Correct |
18 ms |
14680 KB |
Output is correct |
9 |
Correct |
15 ms |
14860 KB |
Output is correct |
10 |
Correct |
14 ms |
14860 KB |
Output is correct |
11 |
Correct |
15 ms |
14860 KB |
Output is correct |
12 |
Correct |
15 ms |
14884 KB |
Output is correct |
13 |
Correct |
15 ms |
14888 KB |
Output is correct |
14 |
Correct |
17 ms |
14988 KB |
Output is correct |
15 |
Correct |
16 ms |
14988 KB |
Output is correct |
16 |
Correct |
15 ms |
14996 KB |
Output is correct |
17 |
Correct |
16 ms |
14996 KB |
Output is correct |
18 |
Correct |
15 ms |
15004 KB |
Output is correct |
19 |
Correct |
16 ms |
15004 KB |
Output is correct |
20 |
Correct |
17 ms |
15004 KB |
Output is correct |
21 |
Correct |
16 ms |
15004 KB |
Output is correct |
22 |
Correct |
15 ms |
15004 KB |
Output is correct |
23 |
Correct |
19 ms |
15004 KB |
Output is correct |
24 |
Correct |
18 ms |
15004 KB |
Output is correct |
25 |
Correct |
14 ms |
15004 KB |
Output is correct |
26 |
Correct |
20 ms |
15004 KB |
Output is correct |
27 |
Correct |
22 ms |
15004 KB |
Output is correct |
28 |
Correct |
22 ms |
15004 KB |
Output is correct |
29 |
Correct |
18 ms |
15004 KB |
Output is correct |
30 |
Correct |
15 ms |
15004 KB |
Output is correct |
31 |
Correct |
17 ms |
15004 KB |
Output is correct |
32 |
Correct |
15 ms |
15004 KB |
Output is correct |
33 |
Correct |
14 ms |
15004 KB |
Output is correct |
34 |
Correct |
14 ms |
15004 KB |
Output is correct |
35 |
Correct |
15 ms |
15004 KB |
Output is correct |
36 |
Correct |
15 ms |
15004 KB |
Output is correct |
37 |
Correct |
15 ms |
15004 KB |
Output is correct |
38 |
Correct |
15 ms |
15004 KB |
Output is correct |
39 |
Correct |
15 ms |
15004 KB |
Output is correct |
40 |
Correct |
15 ms |
15004 KB |
Output is correct |
41 |
Correct |
20 ms |
15004 KB |
Output is correct |
42 |
Correct |
15 ms |
15004 KB |
Output is correct |
43 |
Correct |
16 ms |
15104 KB |
Output is correct |
44 |
Correct |
14 ms |
15104 KB |
Output is correct |
45 |
Correct |
15 ms |
15104 KB |
Output is correct |
46 |
Correct |
15 ms |
15104 KB |
Output is correct |
47 |
Correct |
18 ms |
15104 KB |
Output is correct |
48 |
Correct |
15 ms |
15104 KB |
Output is correct |
49 |
Correct |
15 ms |
15104 KB |
Output is correct |
50 |
Correct |
16 ms |
15104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14568 KB |
Output is correct |
3 |
Correct |
15 ms |
14644 KB |
Output is correct |
4 |
Correct |
15 ms |
14644 KB |
Output is correct |
5 |
Correct |
14 ms |
14644 KB |
Output is correct |
6 |
Correct |
15 ms |
14676 KB |
Output is correct |
7 |
Correct |
15 ms |
14676 KB |
Output is correct |
8 |
Correct |
18 ms |
14680 KB |
Output is correct |
9 |
Correct |
15 ms |
14860 KB |
Output is correct |
10 |
Correct |
14 ms |
14860 KB |
Output is correct |
11 |
Correct |
15 ms |
14860 KB |
Output is correct |
12 |
Correct |
15 ms |
14884 KB |
Output is correct |
13 |
Correct |
15 ms |
14888 KB |
Output is correct |
14 |
Correct |
17 ms |
14988 KB |
Output is correct |
15 |
Correct |
16 ms |
14988 KB |
Output is correct |
16 |
Correct |
15 ms |
14996 KB |
Output is correct |
17 |
Correct |
16 ms |
14996 KB |
Output is correct |
18 |
Correct |
15 ms |
15004 KB |
Output is correct |
19 |
Correct |
16 ms |
15004 KB |
Output is correct |
20 |
Correct |
17 ms |
15004 KB |
Output is correct |
21 |
Correct |
16 ms |
15004 KB |
Output is correct |
22 |
Correct |
15 ms |
15004 KB |
Output is correct |
23 |
Correct |
19 ms |
15004 KB |
Output is correct |
24 |
Correct |
18 ms |
15004 KB |
Output is correct |
25 |
Correct |
14 ms |
15004 KB |
Output is correct |
26 |
Correct |
20 ms |
15004 KB |
Output is correct |
27 |
Correct |
22 ms |
15004 KB |
Output is correct |
28 |
Correct |
22 ms |
15004 KB |
Output is correct |
29 |
Correct |
18 ms |
15004 KB |
Output is correct |
30 |
Correct |
15 ms |
15004 KB |
Output is correct |
31 |
Correct |
17 ms |
15004 KB |
Output is correct |
32 |
Correct |
15 ms |
15004 KB |
Output is correct |
33 |
Correct |
14 ms |
15004 KB |
Output is correct |
34 |
Correct |
14 ms |
15004 KB |
Output is correct |
35 |
Correct |
15 ms |
15004 KB |
Output is correct |
36 |
Correct |
15 ms |
15004 KB |
Output is correct |
37 |
Correct |
15 ms |
15004 KB |
Output is correct |
38 |
Correct |
15 ms |
15004 KB |
Output is correct |
39 |
Correct |
15 ms |
15004 KB |
Output is correct |
40 |
Correct |
15 ms |
15004 KB |
Output is correct |
41 |
Correct |
20 ms |
15004 KB |
Output is correct |
42 |
Correct |
15 ms |
15004 KB |
Output is correct |
43 |
Correct |
16 ms |
15104 KB |
Output is correct |
44 |
Correct |
14 ms |
15104 KB |
Output is correct |
45 |
Correct |
15 ms |
15104 KB |
Output is correct |
46 |
Correct |
15 ms |
15104 KB |
Output is correct |
47 |
Correct |
18 ms |
15104 KB |
Output is correct |
48 |
Correct |
15 ms |
15104 KB |
Output is correct |
49 |
Correct |
15 ms |
15104 KB |
Output is correct |
50 |
Correct |
16 ms |
15104 KB |
Output is correct |
51 |
Correct |
21 ms |
15292 KB |
Output is correct |
52 |
Correct |
19 ms |
15444 KB |
Output is correct |
53 |
Correct |
18 ms |
15640 KB |
Output is correct |
54 |
Correct |
18 ms |
15640 KB |
Output is correct |
55 |
Correct |
18 ms |
15640 KB |
Output is correct |
56 |
Correct |
18 ms |
15640 KB |
Output is correct |
57 |
Correct |
17 ms |
15640 KB |
Output is correct |
58 |
Correct |
20 ms |
15648 KB |
Output is correct |
59 |
Correct |
18 ms |
15692 KB |
Output is correct |
60 |
Correct |
17 ms |
15752 KB |
Output is correct |
61 |
Correct |
19 ms |
16188 KB |
Output is correct |
62 |
Correct |
20 ms |
16240 KB |
Output is correct |
63 |
Correct |
25 ms |
16240 KB |
Output is correct |
64 |
Correct |
19 ms |
16240 KB |
Output is correct |
65 |
Correct |
16 ms |
16240 KB |
Output is correct |
66 |
Correct |
20 ms |
16240 KB |
Output is correct |
67 |
Correct |
20 ms |
16240 KB |
Output is correct |
68 |
Correct |
18 ms |
16340 KB |
Output is correct |
69 |
Correct |
19 ms |
16340 KB |
Output is correct |
70 |
Correct |
20 ms |
16340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14568 KB |
Output is correct |
3 |
Correct |
15 ms |
14644 KB |
Output is correct |
4 |
Correct |
15 ms |
14644 KB |
Output is correct |
5 |
Correct |
14 ms |
14644 KB |
Output is correct |
6 |
Correct |
15 ms |
14676 KB |
Output is correct |
7 |
Correct |
15 ms |
14676 KB |
Output is correct |
8 |
Correct |
18 ms |
14680 KB |
Output is correct |
9 |
Correct |
15 ms |
14860 KB |
Output is correct |
10 |
Correct |
14 ms |
14860 KB |
Output is correct |
11 |
Correct |
15 ms |
14860 KB |
Output is correct |
12 |
Correct |
15 ms |
14884 KB |
Output is correct |
13 |
Correct |
15 ms |
14888 KB |
Output is correct |
14 |
Correct |
17 ms |
14988 KB |
Output is correct |
15 |
Correct |
16 ms |
14988 KB |
Output is correct |
16 |
Correct |
15 ms |
14996 KB |
Output is correct |
17 |
Correct |
16 ms |
14996 KB |
Output is correct |
18 |
Correct |
15 ms |
15004 KB |
Output is correct |
19 |
Correct |
16 ms |
15004 KB |
Output is correct |
20 |
Correct |
17 ms |
15004 KB |
Output is correct |
21 |
Correct |
16 ms |
15004 KB |
Output is correct |
22 |
Correct |
15 ms |
15004 KB |
Output is correct |
23 |
Correct |
19 ms |
15004 KB |
Output is correct |
24 |
Correct |
18 ms |
15004 KB |
Output is correct |
25 |
Correct |
14 ms |
15004 KB |
Output is correct |
26 |
Correct |
20 ms |
15004 KB |
Output is correct |
27 |
Correct |
22 ms |
15004 KB |
Output is correct |
28 |
Correct |
22 ms |
15004 KB |
Output is correct |
29 |
Correct |
18 ms |
15004 KB |
Output is correct |
30 |
Correct |
15 ms |
15004 KB |
Output is correct |
31 |
Correct |
17 ms |
15004 KB |
Output is correct |
32 |
Correct |
15 ms |
15004 KB |
Output is correct |
33 |
Correct |
14 ms |
15004 KB |
Output is correct |
34 |
Correct |
14 ms |
15004 KB |
Output is correct |
35 |
Correct |
15 ms |
15004 KB |
Output is correct |
36 |
Correct |
15 ms |
15004 KB |
Output is correct |
37 |
Correct |
15 ms |
15004 KB |
Output is correct |
38 |
Correct |
15 ms |
15004 KB |
Output is correct |
39 |
Correct |
15 ms |
15004 KB |
Output is correct |
40 |
Correct |
15 ms |
15004 KB |
Output is correct |
41 |
Correct |
20 ms |
15004 KB |
Output is correct |
42 |
Correct |
15 ms |
15004 KB |
Output is correct |
43 |
Correct |
16 ms |
15104 KB |
Output is correct |
44 |
Correct |
14 ms |
15104 KB |
Output is correct |
45 |
Correct |
15 ms |
15104 KB |
Output is correct |
46 |
Correct |
15 ms |
15104 KB |
Output is correct |
47 |
Correct |
18 ms |
15104 KB |
Output is correct |
48 |
Correct |
15 ms |
15104 KB |
Output is correct |
49 |
Correct |
15 ms |
15104 KB |
Output is correct |
50 |
Correct |
16 ms |
15104 KB |
Output is correct |
51 |
Correct |
21 ms |
15292 KB |
Output is correct |
52 |
Correct |
19 ms |
15444 KB |
Output is correct |
53 |
Correct |
18 ms |
15640 KB |
Output is correct |
54 |
Correct |
18 ms |
15640 KB |
Output is correct |
55 |
Correct |
18 ms |
15640 KB |
Output is correct |
56 |
Correct |
18 ms |
15640 KB |
Output is correct |
57 |
Correct |
17 ms |
15640 KB |
Output is correct |
58 |
Correct |
20 ms |
15648 KB |
Output is correct |
59 |
Correct |
18 ms |
15692 KB |
Output is correct |
60 |
Correct |
17 ms |
15752 KB |
Output is correct |
61 |
Correct |
19 ms |
16188 KB |
Output is correct |
62 |
Correct |
20 ms |
16240 KB |
Output is correct |
63 |
Correct |
25 ms |
16240 KB |
Output is correct |
64 |
Correct |
19 ms |
16240 KB |
Output is correct |
65 |
Correct |
16 ms |
16240 KB |
Output is correct |
66 |
Correct |
20 ms |
16240 KB |
Output is correct |
67 |
Correct |
20 ms |
16240 KB |
Output is correct |
68 |
Correct |
18 ms |
16340 KB |
Output is correct |
69 |
Correct |
19 ms |
16340 KB |
Output is correct |
70 |
Correct |
20 ms |
16340 KB |
Output is correct |
71 |
Correct |
88 ms |
22092 KB |
Output is correct |
72 |
Correct |
94 ms |
22092 KB |
Output is correct |
73 |
Correct |
57 ms |
22092 KB |
Output is correct |
74 |
Correct |
88 ms |
25568 KB |
Output is correct |
75 |
Correct |
122 ms |
25612 KB |
Output is correct |
76 |
Correct |
694 ms |
73068 KB |
Output is correct |
77 |
Correct |
753 ms |
73068 KB |
Output is correct |
78 |
Correct |
411 ms |
73068 KB |
Output is correct |
79 |
Correct |
601 ms |
73068 KB |
Output is correct |
80 |
Correct |
671 ms |
77324 KB |
Output is correct |
81 |
Correct |
69 ms |
77324 KB |
Output is correct |
82 |
Correct |
68 ms |
77324 KB |
Output is correct |
83 |
Correct |
53 ms |
77324 KB |
Output is correct |
84 |
Correct |
95 ms |
77324 KB |
Output is correct |
85 |
Correct |
97 ms |
77324 KB |
Output is correct |
86 |
Correct |
443 ms |
77324 KB |
Output is correct |
87 |
Correct |
479 ms |
80784 KB |
Output is correct |
88 |
Correct |
374 ms |
80784 KB |
Output is correct |
89 |
Correct |
536 ms |
89092 KB |
Output is correct |
90 |
Correct |
672 ms |
98900 KB |
Output is correct |