#include <bits/stdc++.h>
#define pb push_back
#define all(v) ((v).begin(), (v).end())
#define sortv(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define umax(a, b) (a)=max((a), (b))
#define umin(a, b) (a)=min((a), (b))
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,n) FOR(i,1,n)
#define rep0(i,n) FOR(i,0,(int)(n)-1)
#define FI first
#define SE second
#define INF 2000000000
#define INFLL 1000000000000000000LL
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAX_N = 300000;
const int MAX_K = 20;
int K;
struct Tree{
int N, r;
int p[MAX_N+1];
bool vst[MAX_N+1];
vector<int> gp[MAX_N+1];
int lv[MAX_N+1], par[MAX_N+1][MAX_K+1], in[MAX_N+1], out[MAX_N+1], cnt = 0;
void dfs(int x){
if(p[x]==0) cnt = 0;
in[x] = ++cnt; vst[x] = true; lv[x] = (p[x]==0 ? 1 : lv[p[x]]+1);
par[x][0] = p[x];
for(int i=1; i<=MAX_K; i++) par[x][i] = par[par[x][i-1]][i-1];
for(int i=0; i<gp[x].size(); i++){
if(gp[x][i]==p[x]) continue;
p[gp[x][i]] = x;
dfs(gp[x][i]);
}
out[x] = cnt;
}
int lca(int x, int y){
for(int i=MAX_K; i>=0; i--){
if(lv[par[x][i]]>=lv[y]) x = par[x][i];
if(lv[par[y][i]]>=lv[x]) y = par[y][i];
}
for(int i=MAX_K; i>=0; i--){
if(par[x][i]!=par[y][i]){
x = par[x][i]; y = par[y][i];
}
}
if(x!=y) x = par[x][0];
return x;
}
};
Tree T[2];
vector<pii> vt;
vector<pii> upd;
int g[MAX_N+1], l[MAX_N+1], r[MAX_N+1], mlv[MAX_N+1];
void init(int x){
for(int i=0; i<x; i++){
g[i] = l[i] = r[i] = i;
mlv[i] = INF;
}
}
int find_g(int x){
return (x==g[x]) ? x : g[x] = find_g(g[x]);
}
void union_g(int x, int y){
x = find_g(x); y = find_g(y);
if(x>y) {int tmp = x; x = y; y = tmp;}
g[y] = x;
r[x] = r[y];
mlv[x] = min(mlv[x], mlv[y]);
}
bool in[MAX_N+1];
int main(){
scanf("%d%d%d", &T[0].N, &T[1].N, &K);
for(int i=0; i<2; i++){
for(int j=1; j<=T[i].N; j++){
scanf("%d", &T[i].p[j]);
if(T[i].p[j]==0) T[i].r = j;
else if(i==0) T[i].gp[T[i].p[j]].pb(j);
}
if(i==0) T[i].dfs(T[i].r);
}
for(int i=1; i<=K; i++) vt.pb({T[0].in[i], i});
sort(vt.begin(), vt.end());
for(int i=0; i<K; i++){
int n = vt[i].second;
//cout<<n<<" ";
while(1){
if(in[n]) break;
in[n] = true;
if(T[1].p[n]==0) break;
T[1].gp[T[1].p[n]].pb(n);
n = T[1].p[n];
}
}
T[1].dfs(T[1].r);
while(!vt.empty()) vt.pop_back();
for(int i=1; i<=K; i++) vt.pb({T[1].in[i], i});
for(int i=1; i<=T[0].N; i++){
T[0].gp[i].clear();
in[i] = false;
}
sort(vt.begin(), vt.end());
//cout<<endl;
for(int i=0; i<K; i++){
int n = vt[i].second;
//cout<<n<<" ";
while(1){
if(in[n]) break;
in[n] = true;
if(T[0].p[n]==0) break;
T[0].gp[T[0].p[n]].pb(n);
n = T[0].p[n];
}
}
T[0].dfs(T[0].r);
/*cout<<endl;
for(int i=1; i<=K; i++){
cout<<T[0].in[i]<<" ";
}
cout<<endl;
for(int i=0; i<K; i++){
cout<<vt[i].second<<" ";
}*/
for(int i=0; i<K-1; i++){
if(T[0].in[vt[i].second] > T[0].in[vt[i+1].second]){
printf("NO"); return 0;
}if(T[1].in[vt[i].second] > T[1].in[vt[i+1].second]){
printf("NO"); return 0;
}
}
for(int i=0; i<vt.size()-1; i++){
int n1 = vt[i].second, n2 = vt[i+1].second;
upd.pb({-T[0].lv[T[0].lca(n1, n2)], i});
}
sort(upd.begin(), upd.end());
init(K);
int i1, i2, n1, n2, g1, g2, l1, l2, l3, l4, l5, l6;
for(int i=0; i<upd.size(); i++){
i1 = upd[i].second; i2 = upd[i].second+1;
n1 = vt[i1].second; n2 = vt[i2].second;
g1 = find_g(i1); g2 = find_g(i2);
l1 = min(T[1].lv[T[1].lca(n1, n2)], min(mlv[g1], mlv[g2]));
l2 = (g1==0) ? 0 : T[1].lv[T[1].lca(vt[g1-1].second, vt[g1].second)];
l3 = (r[g2]==K-1) ? 0 : T[1].lv[T[1].lca(vt[r[g2]].second, vt[r[g2]+1].second)];
l4 = T[0].lv[T[0].lca(n1, n2)];
l5 = (g1==0) ? INF : T[0].lv[T[0].lca(vt[g1-1].second, vt[g1].second)];
l6 = (r[g2]==K-1) ? INF : T[0].lv[T[0].lca(vt[r[g2]].second, vt[r[g2]+1].second)];
//cout<<n1<<" "<<n2<<endl;
//cout<<l1<<" "<<l2<<" "<<l3<<" "<<l4<<" "<<l5<<" "<<l6<<endl;
if((l3>l1 && l4!=l6) || (l2>l1 && l4!=l5)){
printf("NO");
return 0;
}
mlv[find_g(i1)] = min(mlv[find_g(i1)], l1);
union_g(i1, i2);
}
printf("YES");
return 0;
}
Compilation message
family.cpp: In member function 'void Tree::dfs(int)':
family.cpp:39:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gp[x].size(); i++){
~^~~~~~~~~~~~~
family.cpp: In function 'int main()':
family.cpp:147:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<vt.size()-1; i++){
~^~~~~~~~~~~~
family.cpp:154:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<upd.size(); i++){
~^~~~~~~~~~~
family.cpp:88: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:91:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &T[i].p[j]);
~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
15 ms |
14456 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14456 KB |
Output is correct |
7 |
Correct |
14 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
15 ms |
14540 KB |
Output is correct |
10 |
Correct |
14 ms |
14456 KB |
Output is correct |
11 |
Correct |
15 ms |
14528 KB |
Output is correct |
12 |
Correct |
15 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14456 KB |
Output is correct |
14 |
Correct |
15 ms |
14584 KB |
Output is correct |
15 |
Correct |
14 ms |
14584 KB |
Output is correct |
16 |
Correct |
14 ms |
14584 KB |
Output is correct |
17 |
Correct |
15 ms |
14548 KB |
Output is correct |
18 |
Correct |
14 ms |
14584 KB |
Output is correct |
19 |
Correct |
15 ms |
14584 KB |
Output is correct |
20 |
Correct |
15 ms |
14584 KB |
Output is correct |
21 |
Correct |
14 ms |
14584 KB |
Output is correct |
22 |
Correct |
14 ms |
14588 KB |
Output is correct |
23 |
Correct |
14 ms |
14584 KB |
Output is correct |
24 |
Correct |
15 ms |
14624 KB |
Output is correct |
25 |
Correct |
14 ms |
14456 KB |
Output is correct |
26 |
Correct |
15 ms |
14456 KB |
Output is correct |
27 |
Correct |
17 ms |
14548 KB |
Output is correct |
28 |
Correct |
17 ms |
14456 KB |
Output is correct |
29 |
Correct |
14 ms |
14456 KB |
Output is correct |
30 |
Correct |
15 ms |
14456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
15 ms |
14456 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14456 KB |
Output is correct |
7 |
Correct |
14 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
15 ms |
14540 KB |
Output is correct |
10 |
Correct |
14 ms |
14456 KB |
Output is correct |
11 |
Correct |
15 ms |
14528 KB |
Output is correct |
12 |
Correct |
15 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14456 KB |
Output is correct |
14 |
Correct |
15 ms |
14584 KB |
Output is correct |
15 |
Correct |
14 ms |
14584 KB |
Output is correct |
16 |
Correct |
14 ms |
14584 KB |
Output is correct |
17 |
Correct |
15 ms |
14548 KB |
Output is correct |
18 |
Correct |
14 ms |
14584 KB |
Output is correct |
19 |
Correct |
15 ms |
14584 KB |
Output is correct |
20 |
Correct |
15 ms |
14584 KB |
Output is correct |
21 |
Correct |
14 ms |
14584 KB |
Output is correct |
22 |
Correct |
14 ms |
14588 KB |
Output is correct |
23 |
Correct |
14 ms |
14584 KB |
Output is correct |
24 |
Correct |
15 ms |
14624 KB |
Output is correct |
25 |
Correct |
14 ms |
14456 KB |
Output is correct |
26 |
Correct |
15 ms |
14456 KB |
Output is correct |
27 |
Correct |
17 ms |
14548 KB |
Output is correct |
28 |
Correct |
17 ms |
14456 KB |
Output is correct |
29 |
Correct |
14 ms |
14456 KB |
Output is correct |
30 |
Correct |
15 ms |
14456 KB |
Output is correct |
31 |
Correct |
14 ms |
14556 KB |
Output is correct |
32 |
Correct |
18 ms |
14536 KB |
Output is correct |
33 |
Correct |
18 ms |
14456 KB |
Output is correct |
34 |
Correct |
14 ms |
14456 KB |
Output is correct |
35 |
Correct |
14 ms |
14456 KB |
Output is correct |
36 |
Correct |
16 ms |
14616 KB |
Output is correct |
37 |
Correct |
15 ms |
14480 KB |
Output is correct |
38 |
Correct |
15 ms |
14584 KB |
Output is correct |
39 |
Correct |
15 ms |
14572 KB |
Output is correct |
40 |
Correct |
14 ms |
14584 KB |
Output is correct |
41 |
Correct |
14 ms |
14456 KB |
Output is correct |
42 |
Correct |
17 ms |
14448 KB |
Output is correct |
43 |
Correct |
15 ms |
14428 KB |
Output is correct |
44 |
Correct |
15 ms |
14436 KB |
Output is correct |
45 |
Correct |
15 ms |
14456 KB |
Output is correct |
46 |
Correct |
14 ms |
14556 KB |
Output is correct |
47 |
Correct |
15 ms |
14556 KB |
Output is correct |
48 |
Correct |
15 ms |
14584 KB |
Output is correct |
49 |
Correct |
15 ms |
14556 KB |
Output is correct |
50 |
Correct |
15 ms |
14584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
15 ms |
14456 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14456 KB |
Output is correct |
7 |
Correct |
14 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
15 ms |
14540 KB |
Output is correct |
10 |
Correct |
14 ms |
14456 KB |
Output is correct |
11 |
Correct |
15 ms |
14528 KB |
Output is correct |
12 |
Correct |
15 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14456 KB |
Output is correct |
14 |
Correct |
15 ms |
14584 KB |
Output is correct |
15 |
Correct |
14 ms |
14584 KB |
Output is correct |
16 |
Correct |
14 ms |
14584 KB |
Output is correct |
17 |
Correct |
15 ms |
14548 KB |
Output is correct |
18 |
Correct |
14 ms |
14584 KB |
Output is correct |
19 |
Correct |
15 ms |
14584 KB |
Output is correct |
20 |
Correct |
15 ms |
14584 KB |
Output is correct |
21 |
Correct |
14 ms |
14584 KB |
Output is correct |
22 |
Correct |
14 ms |
14588 KB |
Output is correct |
23 |
Correct |
14 ms |
14584 KB |
Output is correct |
24 |
Correct |
15 ms |
14624 KB |
Output is correct |
25 |
Correct |
14 ms |
14456 KB |
Output is correct |
26 |
Correct |
15 ms |
14456 KB |
Output is correct |
27 |
Correct |
17 ms |
14548 KB |
Output is correct |
28 |
Correct |
17 ms |
14456 KB |
Output is correct |
29 |
Correct |
14 ms |
14456 KB |
Output is correct |
30 |
Correct |
15 ms |
14456 KB |
Output is correct |
31 |
Correct |
14 ms |
14556 KB |
Output is correct |
32 |
Correct |
18 ms |
14536 KB |
Output is correct |
33 |
Correct |
18 ms |
14456 KB |
Output is correct |
34 |
Correct |
14 ms |
14456 KB |
Output is correct |
35 |
Correct |
14 ms |
14456 KB |
Output is correct |
36 |
Correct |
16 ms |
14616 KB |
Output is correct |
37 |
Correct |
15 ms |
14480 KB |
Output is correct |
38 |
Correct |
15 ms |
14584 KB |
Output is correct |
39 |
Correct |
15 ms |
14572 KB |
Output is correct |
40 |
Correct |
14 ms |
14584 KB |
Output is correct |
41 |
Correct |
14 ms |
14456 KB |
Output is correct |
42 |
Correct |
17 ms |
14448 KB |
Output is correct |
43 |
Correct |
15 ms |
14428 KB |
Output is correct |
44 |
Correct |
15 ms |
14436 KB |
Output is correct |
45 |
Correct |
15 ms |
14456 KB |
Output is correct |
46 |
Correct |
14 ms |
14556 KB |
Output is correct |
47 |
Correct |
15 ms |
14556 KB |
Output is correct |
48 |
Correct |
15 ms |
14584 KB |
Output is correct |
49 |
Correct |
15 ms |
14556 KB |
Output is correct |
50 |
Correct |
15 ms |
14584 KB |
Output is correct |
51 |
Correct |
30 ms |
15876 KB |
Output is correct |
52 |
Correct |
29 ms |
15832 KB |
Output is correct |
53 |
Correct |
26 ms |
15736 KB |
Output is correct |
54 |
Correct |
24 ms |
15480 KB |
Output is correct |
55 |
Correct |
31 ms |
15848 KB |
Output is correct |
56 |
Correct |
30 ms |
15836 KB |
Output is correct |
57 |
Correct |
28 ms |
15644 KB |
Output is correct |
58 |
Correct |
27 ms |
15612 KB |
Output is correct |
59 |
Correct |
30 ms |
15864 KB |
Output is correct |
60 |
Correct |
29 ms |
15736 KB |
Output is correct |
61 |
Correct |
27 ms |
15864 KB |
Output is correct |
62 |
Correct |
20 ms |
15736 KB |
Output is correct |
63 |
Correct |
26 ms |
15864 KB |
Output is correct |
64 |
Correct |
18 ms |
15132 KB |
Output is correct |
65 |
Correct |
15 ms |
14568 KB |
Output is correct |
66 |
Correct |
20 ms |
15664 KB |
Output is correct |
67 |
Correct |
20 ms |
15584 KB |
Output is correct |
68 |
Correct |
18 ms |
15408 KB |
Output is correct |
69 |
Correct |
21 ms |
15624 KB |
Output is correct |
70 |
Correct |
18 ms |
15484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14456 KB |
Output is correct |
3 |
Correct |
15 ms |
14456 KB |
Output is correct |
4 |
Correct |
14 ms |
14456 KB |
Output is correct |
5 |
Correct |
14 ms |
14456 KB |
Output is correct |
6 |
Correct |
14 ms |
14456 KB |
Output is correct |
7 |
Correct |
14 ms |
14456 KB |
Output is correct |
8 |
Correct |
14 ms |
14456 KB |
Output is correct |
9 |
Correct |
15 ms |
14540 KB |
Output is correct |
10 |
Correct |
14 ms |
14456 KB |
Output is correct |
11 |
Correct |
15 ms |
14528 KB |
Output is correct |
12 |
Correct |
15 ms |
14456 KB |
Output is correct |
13 |
Correct |
14 ms |
14456 KB |
Output is correct |
14 |
Correct |
15 ms |
14584 KB |
Output is correct |
15 |
Correct |
14 ms |
14584 KB |
Output is correct |
16 |
Correct |
14 ms |
14584 KB |
Output is correct |
17 |
Correct |
15 ms |
14548 KB |
Output is correct |
18 |
Correct |
14 ms |
14584 KB |
Output is correct |
19 |
Correct |
15 ms |
14584 KB |
Output is correct |
20 |
Correct |
15 ms |
14584 KB |
Output is correct |
21 |
Correct |
14 ms |
14584 KB |
Output is correct |
22 |
Correct |
14 ms |
14588 KB |
Output is correct |
23 |
Correct |
14 ms |
14584 KB |
Output is correct |
24 |
Correct |
15 ms |
14624 KB |
Output is correct |
25 |
Correct |
14 ms |
14456 KB |
Output is correct |
26 |
Correct |
15 ms |
14456 KB |
Output is correct |
27 |
Correct |
17 ms |
14548 KB |
Output is correct |
28 |
Correct |
17 ms |
14456 KB |
Output is correct |
29 |
Correct |
14 ms |
14456 KB |
Output is correct |
30 |
Correct |
15 ms |
14456 KB |
Output is correct |
31 |
Correct |
14 ms |
14556 KB |
Output is correct |
32 |
Correct |
18 ms |
14536 KB |
Output is correct |
33 |
Correct |
18 ms |
14456 KB |
Output is correct |
34 |
Correct |
14 ms |
14456 KB |
Output is correct |
35 |
Correct |
14 ms |
14456 KB |
Output is correct |
36 |
Correct |
16 ms |
14616 KB |
Output is correct |
37 |
Correct |
15 ms |
14480 KB |
Output is correct |
38 |
Correct |
15 ms |
14584 KB |
Output is correct |
39 |
Correct |
15 ms |
14572 KB |
Output is correct |
40 |
Correct |
14 ms |
14584 KB |
Output is correct |
41 |
Correct |
14 ms |
14456 KB |
Output is correct |
42 |
Correct |
17 ms |
14448 KB |
Output is correct |
43 |
Correct |
15 ms |
14428 KB |
Output is correct |
44 |
Correct |
15 ms |
14436 KB |
Output is correct |
45 |
Correct |
15 ms |
14456 KB |
Output is correct |
46 |
Correct |
14 ms |
14556 KB |
Output is correct |
47 |
Correct |
15 ms |
14556 KB |
Output is correct |
48 |
Correct |
15 ms |
14584 KB |
Output is correct |
49 |
Correct |
15 ms |
14556 KB |
Output is correct |
50 |
Correct |
15 ms |
14584 KB |
Output is correct |
51 |
Correct |
30 ms |
15876 KB |
Output is correct |
52 |
Correct |
29 ms |
15832 KB |
Output is correct |
53 |
Correct |
26 ms |
15736 KB |
Output is correct |
54 |
Correct |
24 ms |
15480 KB |
Output is correct |
55 |
Correct |
31 ms |
15848 KB |
Output is correct |
56 |
Correct |
30 ms |
15836 KB |
Output is correct |
57 |
Correct |
28 ms |
15644 KB |
Output is correct |
58 |
Correct |
27 ms |
15612 KB |
Output is correct |
59 |
Correct |
30 ms |
15864 KB |
Output is correct |
60 |
Correct |
29 ms |
15736 KB |
Output is correct |
61 |
Correct |
27 ms |
15864 KB |
Output is correct |
62 |
Correct |
20 ms |
15736 KB |
Output is correct |
63 |
Correct |
26 ms |
15864 KB |
Output is correct |
64 |
Correct |
18 ms |
15132 KB |
Output is correct |
65 |
Correct |
15 ms |
14568 KB |
Output is correct |
66 |
Correct |
20 ms |
15664 KB |
Output is correct |
67 |
Correct |
20 ms |
15584 KB |
Output is correct |
68 |
Correct |
18 ms |
15408 KB |
Output is correct |
69 |
Correct |
21 ms |
15624 KB |
Output is correct |
70 |
Correct |
18 ms |
15484 KB |
Output is correct |
71 |
Correct |
116 ms |
26636 KB |
Output is correct |
72 |
Correct |
159 ms |
27424 KB |
Output is correct |
73 |
Correct |
51 ms |
22136 KB |
Output is correct |
74 |
Correct |
98 ms |
28844 KB |
Output is correct |
75 |
Correct |
90 ms |
28648 KB |
Output is correct |
76 |
Correct |
847 ms |
102844 KB |
Output is correct |
77 |
Correct |
817 ms |
97144 KB |
Output is correct |
78 |
Correct |
1129 ms |
84340 KB |
Output is correct |
79 |
Correct |
640 ms |
87228 KB |
Output is correct |
80 |
Correct |
692 ms |
89256 KB |
Output is correct |
81 |
Correct |
77 ms |
26192 KB |
Output is correct |
82 |
Correct |
86 ms |
26556 KB |
Output is correct |
83 |
Correct |
46 ms |
22076 KB |
Output is correct |
84 |
Correct |
93 ms |
28664 KB |
Output is correct |
85 |
Correct |
94 ms |
28540 KB |
Output is correct |
86 |
Correct |
835 ms |
93948 KB |
Output is correct |
87 |
Correct |
898 ms |
97572 KB |
Output is correct |
88 |
Correct |
489 ms |
79400 KB |
Output is correct |
89 |
Correct |
642 ms |
86736 KB |
Output is correct |
90 |
Correct |
694 ms |
89252 KB |
Output is correct |