#include<bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef pair<int,int> pii;
const int N = 300005;
int k, par[N], cnt[N];
vector<pair<int,pii> > v;
int Find (int X) {
if(par[X] == X) return X;
return par[X] = Find(par[X]);
}
void Union (int X, int Y) {
X = Find(X);
Y = Find(Y);
if(X == Y) return;
par[Y] = X;
cnt[X] += cnt[Y];
}
struct tree {
int n, r, l[N], f[N];
vector<int> adj[N];
void input() {
for(int i=1;i<=n;i++) {
int T;
scanf("%d",&T);
if(!T) r = i;
else adj[T].push_back(i);
}
}
int calc (int J, int I) {
if(I <= k) {
f[I] = I;
l[I] = 1;
return I;
}
l[I] = 0;
for(auto &T : adj[I]) {
f[I] = calc(J, T);
l[I] += l[T];
}
if((int)adj[I].size() != 1) {
v.push_back({l[I], {J, I}});
}
return f[I];
}
void solve (int I) {
for(auto &T : adj[I]) {
Union(f[T], f[I]);
}
if(cnt[Find(f[I])] != l[I]) {
puts("NO");
exit(0);
}
}
} t[2];
int main()
{
scanf("%d%d%d",&t[0].n,&t[1].n,&k);
t[0].input();
t[1].input();
t[0].calc(0, t[0].r);
t[1].calc(1, t[1].r);
sort(v.begin(), v.end());
for(int i=1;i<=k;i++) {
par[i] = i;
cnt[i] = 1;
}
for(auto &T : v) {
int A, B;
tie(A, B) = T.Y;
t[A].solve(B);
}
puts("YES");
}
Compilation message
family.cpp: In function 'int main()':
family.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&t[0].n,&t[1].n,&k);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
family.cpp: In member function 'void tree::input()':
family.cpp:32:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&T);
~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14576 KB |
Output is correct |
3 |
Correct |
14 ms |
14616 KB |
Output is correct |
4 |
Correct |
18 ms |
14616 KB |
Output is correct |
5 |
Correct |
18 ms |
14616 KB |
Output is correct |
6 |
Correct |
16 ms |
14616 KB |
Output is correct |
7 |
Correct |
14 ms |
14616 KB |
Output is correct |
8 |
Correct |
14 ms |
14616 KB |
Output is correct |
9 |
Correct |
14 ms |
14616 KB |
Output is correct |
10 |
Correct |
13 ms |
14644 KB |
Output is correct |
11 |
Correct |
14 ms |
14644 KB |
Output is correct |
12 |
Correct |
17 ms |
14728 KB |
Output is correct |
13 |
Correct |
16 ms |
14728 KB |
Output is correct |
14 |
Correct |
14 ms |
14816 KB |
Output is correct |
15 |
Correct |
15 ms |
14816 KB |
Output is correct |
16 |
Correct |
14 ms |
14816 KB |
Output is correct |
17 |
Correct |
17 ms |
14868 KB |
Output is correct |
18 |
Correct |
15 ms |
14868 KB |
Output is correct |
19 |
Correct |
14 ms |
14868 KB |
Output is correct |
20 |
Correct |
16 ms |
14868 KB |
Output is correct |
21 |
Correct |
18 ms |
15012 KB |
Output is correct |
22 |
Correct |
19 ms |
15012 KB |
Output is correct |
23 |
Correct |
16 ms |
15012 KB |
Output is correct |
24 |
Correct |
14 ms |
15012 KB |
Output is correct |
25 |
Correct |
14 ms |
15012 KB |
Output is correct |
26 |
Correct |
16 ms |
15012 KB |
Output is correct |
27 |
Correct |
15 ms |
15012 KB |
Output is correct |
28 |
Correct |
14 ms |
15012 KB |
Output is correct |
29 |
Correct |
18 ms |
15012 KB |
Output is correct |
30 |
Correct |
18 ms |
15012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14576 KB |
Output is correct |
3 |
Correct |
14 ms |
14616 KB |
Output is correct |
4 |
Correct |
18 ms |
14616 KB |
Output is correct |
5 |
Correct |
18 ms |
14616 KB |
Output is correct |
6 |
Correct |
16 ms |
14616 KB |
Output is correct |
7 |
Correct |
14 ms |
14616 KB |
Output is correct |
8 |
Correct |
14 ms |
14616 KB |
Output is correct |
9 |
Correct |
14 ms |
14616 KB |
Output is correct |
10 |
Correct |
13 ms |
14644 KB |
Output is correct |
11 |
Correct |
14 ms |
14644 KB |
Output is correct |
12 |
Correct |
17 ms |
14728 KB |
Output is correct |
13 |
Correct |
16 ms |
14728 KB |
Output is correct |
14 |
Correct |
14 ms |
14816 KB |
Output is correct |
15 |
Correct |
15 ms |
14816 KB |
Output is correct |
16 |
Correct |
14 ms |
14816 KB |
Output is correct |
17 |
Correct |
17 ms |
14868 KB |
Output is correct |
18 |
Correct |
15 ms |
14868 KB |
Output is correct |
19 |
Correct |
14 ms |
14868 KB |
Output is correct |
20 |
Correct |
16 ms |
14868 KB |
Output is correct |
21 |
Correct |
18 ms |
15012 KB |
Output is correct |
22 |
Correct |
19 ms |
15012 KB |
Output is correct |
23 |
Correct |
16 ms |
15012 KB |
Output is correct |
24 |
Correct |
14 ms |
15012 KB |
Output is correct |
25 |
Correct |
14 ms |
15012 KB |
Output is correct |
26 |
Correct |
16 ms |
15012 KB |
Output is correct |
27 |
Correct |
15 ms |
15012 KB |
Output is correct |
28 |
Correct |
14 ms |
15012 KB |
Output is correct |
29 |
Correct |
18 ms |
15012 KB |
Output is correct |
30 |
Correct |
18 ms |
15012 KB |
Output is correct |
31 |
Correct |
16 ms |
15012 KB |
Output is correct |
32 |
Correct |
16 ms |
15012 KB |
Output is correct |
33 |
Correct |
16 ms |
15012 KB |
Output is correct |
34 |
Correct |
17 ms |
15012 KB |
Output is correct |
35 |
Correct |
16 ms |
15012 KB |
Output is correct |
36 |
Correct |
18 ms |
15012 KB |
Output is correct |
37 |
Correct |
17 ms |
15012 KB |
Output is correct |
38 |
Correct |
15 ms |
15012 KB |
Output is correct |
39 |
Correct |
17 ms |
15012 KB |
Output is correct |
40 |
Correct |
17 ms |
15012 KB |
Output is correct |
41 |
Correct |
16 ms |
15012 KB |
Output is correct |
42 |
Correct |
17 ms |
15012 KB |
Output is correct |
43 |
Correct |
16 ms |
15012 KB |
Output is correct |
44 |
Correct |
15 ms |
15012 KB |
Output is correct |
45 |
Correct |
14 ms |
15012 KB |
Output is correct |
46 |
Correct |
17 ms |
15012 KB |
Output is correct |
47 |
Correct |
19 ms |
15012 KB |
Output is correct |
48 |
Correct |
15 ms |
15012 KB |
Output is correct |
49 |
Correct |
15 ms |
15012 KB |
Output is correct |
50 |
Correct |
18 ms |
15012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14576 KB |
Output is correct |
3 |
Correct |
14 ms |
14616 KB |
Output is correct |
4 |
Correct |
18 ms |
14616 KB |
Output is correct |
5 |
Correct |
18 ms |
14616 KB |
Output is correct |
6 |
Correct |
16 ms |
14616 KB |
Output is correct |
7 |
Correct |
14 ms |
14616 KB |
Output is correct |
8 |
Correct |
14 ms |
14616 KB |
Output is correct |
9 |
Correct |
14 ms |
14616 KB |
Output is correct |
10 |
Correct |
13 ms |
14644 KB |
Output is correct |
11 |
Correct |
14 ms |
14644 KB |
Output is correct |
12 |
Correct |
17 ms |
14728 KB |
Output is correct |
13 |
Correct |
16 ms |
14728 KB |
Output is correct |
14 |
Correct |
14 ms |
14816 KB |
Output is correct |
15 |
Correct |
15 ms |
14816 KB |
Output is correct |
16 |
Correct |
14 ms |
14816 KB |
Output is correct |
17 |
Correct |
17 ms |
14868 KB |
Output is correct |
18 |
Correct |
15 ms |
14868 KB |
Output is correct |
19 |
Correct |
14 ms |
14868 KB |
Output is correct |
20 |
Correct |
16 ms |
14868 KB |
Output is correct |
21 |
Correct |
18 ms |
15012 KB |
Output is correct |
22 |
Correct |
19 ms |
15012 KB |
Output is correct |
23 |
Correct |
16 ms |
15012 KB |
Output is correct |
24 |
Correct |
14 ms |
15012 KB |
Output is correct |
25 |
Correct |
14 ms |
15012 KB |
Output is correct |
26 |
Correct |
16 ms |
15012 KB |
Output is correct |
27 |
Correct |
15 ms |
15012 KB |
Output is correct |
28 |
Correct |
14 ms |
15012 KB |
Output is correct |
29 |
Correct |
18 ms |
15012 KB |
Output is correct |
30 |
Correct |
18 ms |
15012 KB |
Output is correct |
31 |
Correct |
16 ms |
15012 KB |
Output is correct |
32 |
Correct |
16 ms |
15012 KB |
Output is correct |
33 |
Correct |
16 ms |
15012 KB |
Output is correct |
34 |
Correct |
17 ms |
15012 KB |
Output is correct |
35 |
Correct |
16 ms |
15012 KB |
Output is correct |
36 |
Correct |
18 ms |
15012 KB |
Output is correct |
37 |
Correct |
17 ms |
15012 KB |
Output is correct |
38 |
Correct |
15 ms |
15012 KB |
Output is correct |
39 |
Correct |
17 ms |
15012 KB |
Output is correct |
40 |
Correct |
17 ms |
15012 KB |
Output is correct |
41 |
Correct |
16 ms |
15012 KB |
Output is correct |
42 |
Correct |
17 ms |
15012 KB |
Output is correct |
43 |
Correct |
16 ms |
15012 KB |
Output is correct |
44 |
Correct |
15 ms |
15012 KB |
Output is correct |
45 |
Correct |
14 ms |
15012 KB |
Output is correct |
46 |
Correct |
17 ms |
15012 KB |
Output is correct |
47 |
Correct |
19 ms |
15012 KB |
Output is correct |
48 |
Correct |
15 ms |
15012 KB |
Output is correct |
49 |
Correct |
15 ms |
15012 KB |
Output is correct |
50 |
Correct |
18 ms |
15012 KB |
Output is correct |
51 |
Correct |
16 ms |
15176 KB |
Output is correct |
52 |
Correct |
17 ms |
15356 KB |
Output is correct |
53 |
Correct |
21 ms |
15540 KB |
Output is correct |
54 |
Correct |
15 ms |
15540 KB |
Output is correct |
55 |
Correct |
17 ms |
15540 KB |
Output is correct |
56 |
Correct |
18 ms |
15552 KB |
Output is correct |
57 |
Correct |
19 ms |
15632 KB |
Output is correct |
58 |
Correct |
18 ms |
15740 KB |
Output is correct |
59 |
Correct |
16 ms |
15740 KB |
Output is correct |
60 |
Correct |
19 ms |
15740 KB |
Output is correct |
61 |
Correct |
16 ms |
16040 KB |
Output is correct |
62 |
Correct |
17 ms |
16092 KB |
Output is correct |
63 |
Correct |
19 ms |
16092 KB |
Output is correct |
64 |
Correct |
19 ms |
16092 KB |
Output is correct |
65 |
Correct |
17 ms |
16092 KB |
Output is correct |
66 |
Correct |
20 ms |
16092 KB |
Output is correct |
67 |
Correct |
18 ms |
16092 KB |
Output is correct |
68 |
Correct |
20 ms |
16092 KB |
Output is correct |
69 |
Correct |
23 ms |
16256 KB |
Output is correct |
70 |
Correct |
19 ms |
16256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14576 KB |
Output is correct |
3 |
Correct |
14 ms |
14616 KB |
Output is correct |
4 |
Correct |
18 ms |
14616 KB |
Output is correct |
5 |
Correct |
18 ms |
14616 KB |
Output is correct |
6 |
Correct |
16 ms |
14616 KB |
Output is correct |
7 |
Correct |
14 ms |
14616 KB |
Output is correct |
8 |
Correct |
14 ms |
14616 KB |
Output is correct |
9 |
Correct |
14 ms |
14616 KB |
Output is correct |
10 |
Correct |
13 ms |
14644 KB |
Output is correct |
11 |
Correct |
14 ms |
14644 KB |
Output is correct |
12 |
Correct |
17 ms |
14728 KB |
Output is correct |
13 |
Correct |
16 ms |
14728 KB |
Output is correct |
14 |
Correct |
14 ms |
14816 KB |
Output is correct |
15 |
Correct |
15 ms |
14816 KB |
Output is correct |
16 |
Correct |
14 ms |
14816 KB |
Output is correct |
17 |
Correct |
17 ms |
14868 KB |
Output is correct |
18 |
Correct |
15 ms |
14868 KB |
Output is correct |
19 |
Correct |
14 ms |
14868 KB |
Output is correct |
20 |
Correct |
16 ms |
14868 KB |
Output is correct |
21 |
Correct |
18 ms |
15012 KB |
Output is correct |
22 |
Correct |
19 ms |
15012 KB |
Output is correct |
23 |
Correct |
16 ms |
15012 KB |
Output is correct |
24 |
Correct |
14 ms |
15012 KB |
Output is correct |
25 |
Correct |
14 ms |
15012 KB |
Output is correct |
26 |
Correct |
16 ms |
15012 KB |
Output is correct |
27 |
Correct |
15 ms |
15012 KB |
Output is correct |
28 |
Correct |
14 ms |
15012 KB |
Output is correct |
29 |
Correct |
18 ms |
15012 KB |
Output is correct |
30 |
Correct |
18 ms |
15012 KB |
Output is correct |
31 |
Correct |
16 ms |
15012 KB |
Output is correct |
32 |
Correct |
16 ms |
15012 KB |
Output is correct |
33 |
Correct |
16 ms |
15012 KB |
Output is correct |
34 |
Correct |
17 ms |
15012 KB |
Output is correct |
35 |
Correct |
16 ms |
15012 KB |
Output is correct |
36 |
Correct |
18 ms |
15012 KB |
Output is correct |
37 |
Correct |
17 ms |
15012 KB |
Output is correct |
38 |
Correct |
15 ms |
15012 KB |
Output is correct |
39 |
Correct |
17 ms |
15012 KB |
Output is correct |
40 |
Correct |
17 ms |
15012 KB |
Output is correct |
41 |
Correct |
16 ms |
15012 KB |
Output is correct |
42 |
Correct |
17 ms |
15012 KB |
Output is correct |
43 |
Correct |
16 ms |
15012 KB |
Output is correct |
44 |
Correct |
15 ms |
15012 KB |
Output is correct |
45 |
Correct |
14 ms |
15012 KB |
Output is correct |
46 |
Correct |
17 ms |
15012 KB |
Output is correct |
47 |
Correct |
19 ms |
15012 KB |
Output is correct |
48 |
Correct |
15 ms |
15012 KB |
Output is correct |
49 |
Correct |
15 ms |
15012 KB |
Output is correct |
50 |
Correct |
18 ms |
15012 KB |
Output is correct |
51 |
Correct |
16 ms |
15176 KB |
Output is correct |
52 |
Correct |
17 ms |
15356 KB |
Output is correct |
53 |
Correct |
21 ms |
15540 KB |
Output is correct |
54 |
Correct |
15 ms |
15540 KB |
Output is correct |
55 |
Correct |
17 ms |
15540 KB |
Output is correct |
56 |
Correct |
18 ms |
15552 KB |
Output is correct |
57 |
Correct |
19 ms |
15632 KB |
Output is correct |
58 |
Correct |
18 ms |
15740 KB |
Output is correct |
59 |
Correct |
16 ms |
15740 KB |
Output is correct |
60 |
Correct |
19 ms |
15740 KB |
Output is correct |
61 |
Correct |
16 ms |
16040 KB |
Output is correct |
62 |
Correct |
17 ms |
16092 KB |
Output is correct |
63 |
Correct |
19 ms |
16092 KB |
Output is correct |
64 |
Correct |
19 ms |
16092 KB |
Output is correct |
65 |
Correct |
17 ms |
16092 KB |
Output is correct |
66 |
Correct |
20 ms |
16092 KB |
Output is correct |
67 |
Correct |
18 ms |
16092 KB |
Output is correct |
68 |
Correct |
20 ms |
16092 KB |
Output is correct |
69 |
Correct |
23 ms |
16256 KB |
Output is correct |
70 |
Correct |
19 ms |
16256 KB |
Output is correct |
71 |
Correct |
86 ms |
19948 KB |
Output is correct |
72 |
Correct |
81 ms |
19948 KB |
Output is correct |
73 |
Correct |
66 ms |
19948 KB |
Output is correct |
74 |
Correct |
86 ms |
23236 KB |
Output is correct |
75 |
Correct |
79 ms |
23236 KB |
Output is correct |
76 |
Correct |
574 ms |
54708 KB |
Output is correct |
77 |
Correct |
502 ms |
54708 KB |
Output is correct |
78 |
Correct |
359 ms |
54708 KB |
Output is correct |
79 |
Correct |
444 ms |
58704 KB |
Output is correct |
80 |
Correct |
593 ms |
58704 KB |
Output is correct |
81 |
Correct |
66 ms |
58704 KB |
Output is correct |
82 |
Correct |
66 ms |
58704 KB |
Output is correct |
83 |
Correct |
53 ms |
58704 KB |
Output is correct |
84 |
Correct |
102 ms |
58704 KB |
Output is correct |
85 |
Correct |
97 ms |
58704 KB |
Output is correct |
86 |
Correct |
478 ms |
71428 KB |
Output is correct |
87 |
Correct |
442 ms |
75548 KB |
Output is correct |
88 |
Correct |
313 ms |
75548 KB |
Output is correct |
89 |
Correct |
442 ms |
79088 KB |
Output is correct |
90 |
Correct |
423 ms |
80276 KB |
Output is correct |