#include <stdio.h>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int,int> Pi;
/* Union Find 자료구조 */
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;
// dfs를 돌면서 각 내부정점의 부트리에 속하는 아무 리프노드 하나와 총 리프개수를 저장
// 또한, 각 정점의 깊이도 같이 저장
// dfs함수의 리턴값은 (아무 리프노드, 리프노드 개수) 이다.
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);
// Union Find 초기화
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를 돌면서 각 내부정점의 부트리에 속하는 아무 리프노드 하나와 총 리프개수를 저장
// 또한, 각 정점의 깊이도 같이 저장
dfs( 0, root[0] , 0);
timer = 0;
dfs( 1, root[1] , 0);
// 모든 내부정점에 대해 event 만들어 주기
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});
}
}
// event들을 (길이,level)순으로 정렬
sort(events.begin(), events.end());
// 각 event를 정렬된 순서대로 보면서, 현재 정점의 부 트리에 속한 모든 리프노드를
// Union 해준다. 이후 현재 정점의 부 트리에 속한 리프노트 개수와 union된 집합의 크기를 비교한다.
for(auto e : events){
for(auto x : E[e.t][e.node]){
uni(leaf[e.t][e.node], leaf[e.t][x]);
}
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:61: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:71: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 |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
13 ms |
14608 KB |
Output is correct |
4 |
Correct |
14 ms |
14684 KB |
Output is correct |
5 |
Correct |
14 ms |
14684 KB |
Output is correct |
6 |
Correct |
18 ms |
14708 KB |
Output is correct |
7 |
Correct |
24 ms |
14708 KB |
Output is correct |
8 |
Correct |
19 ms |
14708 KB |
Output is correct |
9 |
Correct |
19 ms |
14708 KB |
Output is correct |
10 |
Correct |
17 ms |
14708 KB |
Output is correct |
11 |
Correct |
17 ms |
14736 KB |
Output is correct |
12 |
Correct |
20 ms |
14736 KB |
Output is correct |
13 |
Correct |
19 ms |
14804 KB |
Output is correct |
14 |
Correct |
16 ms |
14804 KB |
Output is correct |
15 |
Correct |
18 ms |
14804 KB |
Output is correct |
16 |
Correct |
17 ms |
14804 KB |
Output is correct |
17 |
Correct |
21 ms |
14804 KB |
Output is correct |
18 |
Correct |
19 ms |
14804 KB |
Output is correct |
19 |
Correct |
19 ms |
14804 KB |
Output is correct |
20 |
Correct |
19 ms |
14804 KB |
Output is correct |
21 |
Correct |
18 ms |
14804 KB |
Output is correct |
22 |
Correct |
18 ms |
14804 KB |
Output is correct |
23 |
Correct |
18 ms |
14804 KB |
Output is correct |
24 |
Correct |
18 ms |
14804 KB |
Output is correct |
25 |
Correct |
18 ms |
14804 KB |
Output is correct |
26 |
Correct |
18 ms |
14804 KB |
Output is correct |
27 |
Correct |
17 ms |
14804 KB |
Output is correct |
28 |
Correct |
19 ms |
14804 KB |
Output is correct |
29 |
Correct |
18 ms |
14804 KB |
Output is correct |
30 |
Correct |
18 ms |
14804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
13 ms |
14608 KB |
Output is correct |
4 |
Correct |
14 ms |
14684 KB |
Output is correct |
5 |
Correct |
14 ms |
14684 KB |
Output is correct |
6 |
Correct |
18 ms |
14708 KB |
Output is correct |
7 |
Correct |
24 ms |
14708 KB |
Output is correct |
8 |
Correct |
19 ms |
14708 KB |
Output is correct |
9 |
Correct |
19 ms |
14708 KB |
Output is correct |
10 |
Correct |
17 ms |
14708 KB |
Output is correct |
11 |
Correct |
17 ms |
14736 KB |
Output is correct |
12 |
Correct |
20 ms |
14736 KB |
Output is correct |
13 |
Correct |
19 ms |
14804 KB |
Output is correct |
14 |
Correct |
16 ms |
14804 KB |
Output is correct |
15 |
Correct |
18 ms |
14804 KB |
Output is correct |
16 |
Correct |
17 ms |
14804 KB |
Output is correct |
17 |
Correct |
21 ms |
14804 KB |
Output is correct |
18 |
Correct |
19 ms |
14804 KB |
Output is correct |
19 |
Correct |
19 ms |
14804 KB |
Output is correct |
20 |
Correct |
19 ms |
14804 KB |
Output is correct |
21 |
Correct |
18 ms |
14804 KB |
Output is correct |
22 |
Correct |
18 ms |
14804 KB |
Output is correct |
23 |
Correct |
18 ms |
14804 KB |
Output is correct |
24 |
Correct |
18 ms |
14804 KB |
Output is correct |
25 |
Correct |
18 ms |
14804 KB |
Output is correct |
26 |
Correct |
18 ms |
14804 KB |
Output is correct |
27 |
Correct |
17 ms |
14804 KB |
Output is correct |
28 |
Correct |
19 ms |
14804 KB |
Output is correct |
29 |
Correct |
18 ms |
14804 KB |
Output is correct |
30 |
Correct |
18 ms |
14804 KB |
Output is correct |
31 |
Correct |
19 ms |
14804 KB |
Output is correct |
32 |
Correct |
17 ms |
14804 KB |
Output is correct |
33 |
Correct |
18 ms |
14804 KB |
Output is correct |
34 |
Correct |
19 ms |
14804 KB |
Output is correct |
35 |
Correct |
16 ms |
14804 KB |
Output is correct |
36 |
Correct |
18 ms |
14804 KB |
Output is correct |
37 |
Correct |
22 ms |
14804 KB |
Output is correct |
38 |
Correct |
16 ms |
14804 KB |
Output is correct |
39 |
Correct |
19 ms |
14804 KB |
Output is correct |
40 |
Correct |
19 ms |
14804 KB |
Output is correct |
41 |
Correct |
17 ms |
14804 KB |
Output is correct |
42 |
Correct |
17 ms |
14804 KB |
Output is correct |
43 |
Correct |
17 ms |
14804 KB |
Output is correct |
44 |
Correct |
18 ms |
14804 KB |
Output is correct |
45 |
Correct |
18 ms |
14804 KB |
Output is correct |
46 |
Correct |
19 ms |
14804 KB |
Output is correct |
47 |
Correct |
18 ms |
14804 KB |
Output is correct |
48 |
Correct |
20 ms |
14804 KB |
Output is correct |
49 |
Correct |
19 ms |
14804 KB |
Output is correct |
50 |
Correct |
20 ms |
14804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
13 ms |
14608 KB |
Output is correct |
4 |
Correct |
14 ms |
14684 KB |
Output is correct |
5 |
Correct |
14 ms |
14684 KB |
Output is correct |
6 |
Correct |
18 ms |
14708 KB |
Output is correct |
7 |
Correct |
24 ms |
14708 KB |
Output is correct |
8 |
Correct |
19 ms |
14708 KB |
Output is correct |
9 |
Correct |
19 ms |
14708 KB |
Output is correct |
10 |
Correct |
17 ms |
14708 KB |
Output is correct |
11 |
Correct |
17 ms |
14736 KB |
Output is correct |
12 |
Correct |
20 ms |
14736 KB |
Output is correct |
13 |
Correct |
19 ms |
14804 KB |
Output is correct |
14 |
Correct |
16 ms |
14804 KB |
Output is correct |
15 |
Correct |
18 ms |
14804 KB |
Output is correct |
16 |
Correct |
17 ms |
14804 KB |
Output is correct |
17 |
Correct |
21 ms |
14804 KB |
Output is correct |
18 |
Correct |
19 ms |
14804 KB |
Output is correct |
19 |
Correct |
19 ms |
14804 KB |
Output is correct |
20 |
Correct |
19 ms |
14804 KB |
Output is correct |
21 |
Correct |
18 ms |
14804 KB |
Output is correct |
22 |
Correct |
18 ms |
14804 KB |
Output is correct |
23 |
Correct |
18 ms |
14804 KB |
Output is correct |
24 |
Correct |
18 ms |
14804 KB |
Output is correct |
25 |
Correct |
18 ms |
14804 KB |
Output is correct |
26 |
Correct |
18 ms |
14804 KB |
Output is correct |
27 |
Correct |
17 ms |
14804 KB |
Output is correct |
28 |
Correct |
19 ms |
14804 KB |
Output is correct |
29 |
Correct |
18 ms |
14804 KB |
Output is correct |
30 |
Correct |
18 ms |
14804 KB |
Output is correct |
31 |
Correct |
19 ms |
14804 KB |
Output is correct |
32 |
Correct |
17 ms |
14804 KB |
Output is correct |
33 |
Correct |
18 ms |
14804 KB |
Output is correct |
34 |
Correct |
19 ms |
14804 KB |
Output is correct |
35 |
Correct |
16 ms |
14804 KB |
Output is correct |
36 |
Correct |
18 ms |
14804 KB |
Output is correct |
37 |
Correct |
22 ms |
14804 KB |
Output is correct |
38 |
Correct |
16 ms |
14804 KB |
Output is correct |
39 |
Correct |
19 ms |
14804 KB |
Output is correct |
40 |
Correct |
19 ms |
14804 KB |
Output is correct |
41 |
Correct |
17 ms |
14804 KB |
Output is correct |
42 |
Correct |
17 ms |
14804 KB |
Output is correct |
43 |
Correct |
17 ms |
14804 KB |
Output is correct |
44 |
Correct |
18 ms |
14804 KB |
Output is correct |
45 |
Correct |
18 ms |
14804 KB |
Output is correct |
46 |
Correct |
19 ms |
14804 KB |
Output is correct |
47 |
Correct |
18 ms |
14804 KB |
Output is correct |
48 |
Correct |
20 ms |
14804 KB |
Output is correct |
49 |
Correct |
19 ms |
14804 KB |
Output is correct |
50 |
Correct |
20 ms |
14804 KB |
Output is correct |
51 |
Correct |
25 ms |
14956 KB |
Output is correct |
52 |
Correct |
21 ms |
14992 KB |
Output is correct |
53 |
Correct |
26 ms |
15212 KB |
Output is correct |
54 |
Correct |
19 ms |
15212 KB |
Output is correct |
55 |
Correct |
29 ms |
15212 KB |
Output is correct |
56 |
Correct |
21 ms |
15212 KB |
Output is correct |
57 |
Correct |
22 ms |
15212 KB |
Output is correct |
58 |
Correct |
25 ms |
15212 KB |
Output is correct |
59 |
Correct |
22 ms |
15212 KB |
Output is correct |
60 |
Correct |
21 ms |
15212 KB |
Output is correct |
61 |
Correct |
23 ms |
15340 KB |
Output is correct |
62 |
Correct |
23 ms |
15340 KB |
Output is correct |
63 |
Correct |
24 ms |
15340 KB |
Output is correct |
64 |
Correct |
22 ms |
15340 KB |
Output is correct |
65 |
Correct |
22 ms |
15340 KB |
Output is correct |
66 |
Correct |
21 ms |
15340 KB |
Output is correct |
67 |
Correct |
24 ms |
15340 KB |
Output is correct |
68 |
Correct |
21 ms |
15340 KB |
Output is correct |
69 |
Correct |
25 ms |
15340 KB |
Output is correct |
70 |
Correct |
22 ms |
15340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
13 ms |
14608 KB |
Output is correct |
4 |
Correct |
14 ms |
14684 KB |
Output is correct |
5 |
Correct |
14 ms |
14684 KB |
Output is correct |
6 |
Correct |
18 ms |
14708 KB |
Output is correct |
7 |
Correct |
24 ms |
14708 KB |
Output is correct |
8 |
Correct |
19 ms |
14708 KB |
Output is correct |
9 |
Correct |
19 ms |
14708 KB |
Output is correct |
10 |
Correct |
17 ms |
14708 KB |
Output is correct |
11 |
Correct |
17 ms |
14736 KB |
Output is correct |
12 |
Correct |
20 ms |
14736 KB |
Output is correct |
13 |
Correct |
19 ms |
14804 KB |
Output is correct |
14 |
Correct |
16 ms |
14804 KB |
Output is correct |
15 |
Correct |
18 ms |
14804 KB |
Output is correct |
16 |
Correct |
17 ms |
14804 KB |
Output is correct |
17 |
Correct |
21 ms |
14804 KB |
Output is correct |
18 |
Correct |
19 ms |
14804 KB |
Output is correct |
19 |
Correct |
19 ms |
14804 KB |
Output is correct |
20 |
Correct |
19 ms |
14804 KB |
Output is correct |
21 |
Correct |
18 ms |
14804 KB |
Output is correct |
22 |
Correct |
18 ms |
14804 KB |
Output is correct |
23 |
Correct |
18 ms |
14804 KB |
Output is correct |
24 |
Correct |
18 ms |
14804 KB |
Output is correct |
25 |
Correct |
18 ms |
14804 KB |
Output is correct |
26 |
Correct |
18 ms |
14804 KB |
Output is correct |
27 |
Correct |
17 ms |
14804 KB |
Output is correct |
28 |
Correct |
19 ms |
14804 KB |
Output is correct |
29 |
Correct |
18 ms |
14804 KB |
Output is correct |
30 |
Correct |
18 ms |
14804 KB |
Output is correct |
31 |
Correct |
19 ms |
14804 KB |
Output is correct |
32 |
Correct |
17 ms |
14804 KB |
Output is correct |
33 |
Correct |
18 ms |
14804 KB |
Output is correct |
34 |
Correct |
19 ms |
14804 KB |
Output is correct |
35 |
Correct |
16 ms |
14804 KB |
Output is correct |
36 |
Correct |
18 ms |
14804 KB |
Output is correct |
37 |
Correct |
22 ms |
14804 KB |
Output is correct |
38 |
Correct |
16 ms |
14804 KB |
Output is correct |
39 |
Correct |
19 ms |
14804 KB |
Output is correct |
40 |
Correct |
19 ms |
14804 KB |
Output is correct |
41 |
Correct |
17 ms |
14804 KB |
Output is correct |
42 |
Correct |
17 ms |
14804 KB |
Output is correct |
43 |
Correct |
17 ms |
14804 KB |
Output is correct |
44 |
Correct |
18 ms |
14804 KB |
Output is correct |
45 |
Correct |
18 ms |
14804 KB |
Output is correct |
46 |
Correct |
19 ms |
14804 KB |
Output is correct |
47 |
Correct |
18 ms |
14804 KB |
Output is correct |
48 |
Correct |
20 ms |
14804 KB |
Output is correct |
49 |
Correct |
19 ms |
14804 KB |
Output is correct |
50 |
Correct |
20 ms |
14804 KB |
Output is correct |
51 |
Correct |
25 ms |
14956 KB |
Output is correct |
52 |
Correct |
21 ms |
14992 KB |
Output is correct |
53 |
Correct |
26 ms |
15212 KB |
Output is correct |
54 |
Correct |
19 ms |
15212 KB |
Output is correct |
55 |
Correct |
29 ms |
15212 KB |
Output is correct |
56 |
Correct |
21 ms |
15212 KB |
Output is correct |
57 |
Correct |
22 ms |
15212 KB |
Output is correct |
58 |
Correct |
25 ms |
15212 KB |
Output is correct |
59 |
Correct |
22 ms |
15212 KB |
Output is correct |
60 |
Correct |
21 ms |
15212 KB |
Output is correct |
61 |
Correct |
23 ms |
15340 KB |
Output is correct |
62 |
Correct |
23 ms |
15340 KB |
Output is correct |
63 |
Correct |
24 ms |
15340 KB |
Output is correct |
64 |
Correct |
22 ms |
15340 KB |
Output is correct |
65 |
Correct |
22 ms |
15340 KB |
Output is correct |
66 |
Correct |
21 ms |
15340 KB |
Output is correct |
67 |
Correct |
24 ms |
15340 KB |
Output is correct |
68 |
Correct |
21 ms |
15340 KB |
Output is correct |
69 |
Correct |
25 ms |
15340 KB |
Output is correct |
70 |
Correct |
22 ms |
15340 KB |
Output is correct |
71 |
Correct |
101 ms |
20192 KB |
Output is correct |
72 |
Correct |
113 ms |
20192 KB |
Output is correct |
73 |
Correct |
82 ms |
20192 KB |
Output is correct |
74 |
Correct |
141 ms |
22296 KB |
Output is correct |
75 |
Correct |
123 ms |
22296 KB |
Output is correct |
76 |
Correct |
872 ms |
65352 KB |
Output is correct |
77 |
Correct |
1093 ms |
65352 KB |
Output is correct |
78 |
Correct |
612 ms |
65352 KB |
Output is correct |
79 |
Correct |
771 ms |
65352 KB |
Output is correct |
80 |
Correct |
833 ms |
65352 KB |
Output is correct |
81 |
Correct |
97 ms |
65352 KB |
Output is correct |
82 |
Correct |
84 ms |
65352 KB |
Output is correct |
83 |
Correct |
66 ms |
65352 KB |
Output is correct |
84 |
Correct |
119 ms |
65352 KB |
Output is correct |
85 |
Correct |
121 ms |
65352 KB |
Output is correct |
86 |
Correct |
563 ms |
65352 KB |
Output is correct |
87 |
Correct |
622 ms |
65352 KB |
Output is correct |
88 |
Correct |
529 ms |
65352 KB |
Output is correct |
89 |
Correct |
657 ms |
65352 KB |
Output is correct |
90 |
Correct |
629 ms |
65352 KB |
Output is correct |