//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 3e5+10;
int n[2],m;
int fa[2][maxn];
map <int,int> mp[2][maxn];
int sz[2][maxn];
vector <int> edge[2][maxn];
int dep[2][maxn];
vector <pii> pt;
int getf(int id,int node) {
if (fa[id][node]==node) return fa[id][node];
else return fa[id][node] = getf(id,fa[id][node]);
}
int p[2][maxn];
int dfs(int id,int u) {
if (u<m) return 1;
sz[id][u]=0;
pt.pb({id,u});
for (int v:edge[id][u]) {
dep[id][v]= dep[id][u]+1;
sz[id][u]+=dfs(id,v);
}
return sz[id][u];
}
bool cmp(pii a, pii b ) {
if (sz[a.fi][a.se]!=sz[b.fi][b.se]) return sz[a.fi][a.se]<sz[b.fi][b.se];
else return dep[a.fi][a.se]>dep[b.fi][b.se];
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie();
cin>>n[0]>>n[1]>>m;
memset(sz,0,sizeof(sz));
int rt[2];
rep(k,0,2) rep(i,0,n[k]) {
cin>>p[k][i];
p[k][i]--;
if (p[k][i]!=-1) edge[k][p[k][i]].pb(i);
else rt[k] = i;
if (i<m) fa[k][i] = p[k][i];
else fa[k][i] = i;
}
dfs(0,rt[0]);
dfs(1,rt[1]);
rep(k,0,2)
rep(i,0,m) {
int ff1 = getf(k,i);
int ff2 = getf(1-k,i);
mp[k][ff1][ff2] = 1;
}
sort(ALL(pt),cmp);
for (auto cur:pt) {
int x=cur.fi,y=cur.se;
int tmp = getf(1-x,(mp[x][y].begin())->fi);
if (mp[x][y].size()>1) {
for (auto it:mp[x][y]) {
if (getf(1-x,it.fi)!=tmp) {
cout<<"NO";
return 0;
}
}
}
// debug(tmp);
if (p[x][y]!=-1) mp[x][p[x][y]][tmp] = 1, fa[x][y] = p[x][y];
}
cout<<"YES";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
44884 KB |
Output is correct |
2 |
Correct |
22 ms |
44936 KB |
Output is correct |
3 |
Correct |
25 ms |
44984 KB |
Output is correct |
4 |
Correct |
23 ms |
44948 KB |
Output is correct |
5 |
Correct |
22 ms |
44896 KB |
Output is correct |
6 |
Correct |
22 ms |
44940 KB |
Output is correct |
7 |
Correct |
22 ms |
44932 KB |
Output is correct |
8 |
Correct |
23 ms |
44920 KB |
Output is correct |
9 |
Correct |
22 ms |
44884 KB |
Output is correct |
10 |
Correct |
22 ms |
44896 KB |
Output is correct |
11 |
Correct |
24 ms |
44896 KB |
Output is correct |
12 |
Correct |
22 ms |
44864 KB |
Output is correct |
13 |
Correct |
24 ms |
45028 KB |
Output is correct |
14 |
Correct |
23 ms |
45020 KB |
Output is correct |
15 |
Correct |
21 ms |
44928 KB |
Output is correct |
16 |
Correct |
20 ms |
44936 KB |
Output is correct |
17 |
Correct |
26 ms |
45056 KB |
Output is correct |
18 |
Correct |
25 ms |
44924 KB |
Output is correct |
19 |
Correct |
22 ms |
44980 KB |
Output is correct |
20 |
Correct |
22 ms |
44932 KB |
Output is correct |
21 |
Correct |
24 ms |
45012 KB |
Output is correct |
22 |
Correct |
21 ms |
44928 KB |
Output is correct |
23 |
Correct |
22 ms |
44912 KB |
Output is correct |
24 |
Correct |
22 ms |
44884 KB |
Output is correct |
25 |
Correct |
22 ms |
44920 KB |
Output is correct |
26 |
Correct |
22 ms |
44896 KB |
Output is correct |
27 |
Correct |
22 ms |
44880 KB |
Output is correct |
28 |
Correct |
23 ms |
44952 KB |
Output is correct |
29 |
Correct |
25 ms |
45028 KB |
Output is correct |
30 |
Correct |
22 ms |
44952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
44884 KB |
Output is correct |
2 |
Correct |
22 ms |
44936 KB |
Output is correct |
3 |
Correct |
25 ms |
44984 KB |
Output is correct |
4 |
Correct |
23 ms |
44948 KB |
Output is correct |
5 |
Correct |
22 ms |
44896 KB |
Output is correct |
6 |
Correct |
22 ms |
44940 KB |
Output is correct |
7 |
Correct |
22 ms |
44932 KB |
Output is correct |
8 |
Correct |
23 ms |
44920 KB |
Output is correct |
9 |
Correct |
22 ms |
44884 KB |
Output is correct |
10 |
Correct |
22 ms |
44896 KB |
Output is correct |
11 |
Correct |
24 ms |
44896 KB |
Output is correct |
12 |
Correct |
22 ms |
44864 KB |
Output is correct |
13 |
Correct |
24 ms |
45028 KB |
Output is correct |
14 |
Correct |
23 ms |
45020 KB |
Output is correct |
15 |
Correct |
21 ms |
44928 KB |
Output is correct |
16 |
Correct |
20 ms |
44936 KB |
Output is correct |
17 |
Correct |
26 ms |
45056 KB |
Output is correct |
18 |
Correct |
25 ms |
44924 KB |
Output is correct |
19 |
Correct |
22 ms |
44980 KB |
Output is correct |
20 |
Correct |
22 ms |
44932 KB |
Output is correct |
21 |
Correct |
24 ms |
45012 KB |
Output is correct |
22 |
Correct |
21 ms |
44928 KB |
Output is correct |
23 |
Correct |
22 ms |
44912 KB |
Output is correct |
24 |
Correct |
22 ms |
44884 KB |
Output is correct |
25 |
Correct |
22 ms |
44920 KB |
Output is correct |
26 |
Correct |
22 ms |
44896 KB |
Output is correct |
27 |
Correct |
22 ms |
44880 KB |
Output is correct |
28 |
Correct |
23 ms |
44952 KB |
Output is correct |
29 |
Correct |
25 ms |
45028 KB |
Output is correct |
30 |
Correct |
22 ms |
44952 KB |
Output is correct |
31 |
Correct |
25 ms |
44936 KB |
Output is correct |
32 |
Correct |
25 ms |
44880 KB |
Output is correct |
33 |
Correct |
23 ms |
44928 KB |
Output is correct |
34 |
Correct |
23 ms |
44884 KB |
Output is correct |
35 |
Correct |
23 ms |
44888 KB |
Output is correct |
36 |
Correct |
22 ms |
44928 KB |
Output is correct |
37 |
Correct |
24 ms |
44968 KB |
Output is correct |
38 |
Correct |
21 ms |
45012 KB |
Output is correct |
39 |
Correct |
25 ms |
45012 KB |
Output is correct |
40 |
Correct |
23 ms |
45012 KB |
Output is correct |
41 |
Correct |
22 ms |
44884 KB |
Output is correct |
42 |
Correct |
22 ms |
44924 KB |
Output is correct |
43 |
Correct |
22 ms |
44904 KB |
Output is correct |
44 |
Correct |
24 ms |
44884 KB |
Output is correct |
45 |
Correct |
23 ms |
44884 KB |
Output is correct |
46 |
Correct |
22 ms |
44928 KB |
Output is correct |
47 |
Correct |
28 ms |
44980 KB |
Output is correct |
48 |
Correct |
22 ms |
44908 KB |
Output is correct |
49 |
Correct |
22 ms |
44976 KB |
Output is correct |
50 |
Correct |
21 ms |
45012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
44884 KB |
Output is correct |
2 |
Correct |
22 ms |
44936 KB |
Output is correct |
3 |
Correct |
25 ms |
44984 KB |
Output is correct |
4 |
Correct |
23 ms |
44948 KB |
Output is correct |
5 |
Correct |
22 ms |
44896 KB |
Output is correct |
6 |
Correct |
22 ms |
44940 KB |
Output is correct |
7 |
Correct |
22 ms |
44932 KB |
Output is correct |
8 |
Correct |
23 ms |
44920 KB |
Output is correct |
9 |
Correct |
22 ms |
44884 KB |
Output is correct |
10 |
Correct |
22 ms |
44896 KB |
Output is correct |
11 |
Correct |
24 ms |
44896 KB |
Output is correct |
12 |
Correct |
22 ms |
44864 KB |
Output is correct |
13 |
Correct |
24 ms |
45028 KB |
Output is correct |
14 |
Correct |
23 ms |
45020 KB |
Output is correct |
15 |
Correct |
21 ms |
44928 KB |
Output is correct |
16 |
Correct |
20 ms |
44936 KB |
Output is correct |
17 |
Correct |
26 ms |
45056 KB |
Output is correct |
18 |
Correct |
25 ms |
44924 KB |
Output is correct |
19 |
Correct |
22 ms |
44980 KB |
Output is correct |
20 |
Correct |
22 ms |
44932 KB |
Output is correct |
21 |
Correct |
24 ms |
45012 KB |
Output is correct |
22 |
Correct |
21 ms |
44928 KB |
Output is correct |
23 |
Correct |
22 ms |
44912 KB |
Output is correct |
24 |
Correct |
22 ms |
44884 KB |
Output is correct |
25 |
Correct |
22 ms |
44920 KB |
Output is correct |
26 |
Correct |
22 ms |
44896 KB |
Output is correct |
27 |
Correct |
22 ms |
44880 KB |
Output is correct |
28 |
Correct |
23 ms |
44952 KB |
Output is correct |
29 |
Correct |
25 ms |
45028 KB |
Output is correct |
30 |
Correct |
22 ms |
44952 KB |
Output is correct |
31 |
Correct |
25 ms |
44936 KB |
Output is correct |
32 |
Correct |
25 ms |
44880 KB |
Output is correct |
33 |
Correct |
23 ms |
44928 KB |
Output is correct |
34 |
Correct |
23 ms |
44884 KB |
Output is correct |
35 |
Correct |
23 ms |
44888 KB |
Output is correct |
36 |
Correct |
22 ms |
44928 KB |
Output is correct |
37 |
Correct |
24 ms |
44968 KB |
Output is correct |
38 |
Correct |
21 ms |
45012 KB |
Output is correct |
39 |
Correct |
25 ms |
45012 KB |
Output is correct |
40 |
Correct |
23 ms |
45012 KB |
Output is correct |
41 |
Correct |
22 ms |
44884 KB |
Output is correct |
42 |
Correct |
22 ms |
44924 KB |
Output is correct |
43 |
Correct |
22 ms |
44904 KB |
Output is correct |
44 |
Correct |
24 ms |
44884 KB |
Output is correct |
45 |
Correct |
23 ms |
44884 KB |
Output is correct |
46 |
Correct |
22 ms |
44928 KB |
Output is correct |
47 |
Correct |
28 ms |
44980 KB |
Output is correct |
48 |
Correct |
22 ms |
44908 KB |
Output is correct |
49 |
Correct |
22 ms |
44976 KB |
Output is correct |
50 |
Correct |
21 ms |
45012 KB |
Output is correct |
51 |
Correct |
27 ms |
45140 KB |
Output is correct |
52 |
Correct |
27 ms |
45428 KB |
Output is correct |
53 |
Correct |
25 ms |
45568 KB |
Output is correct |
54 |
Correct |
24 ms |
45384 KB |
Output is correct |
55 |
Correct |
23 ms |
45200 KB |
Output is correct |
56 |
Correct |
24 ms |
45188 KB |
Output is correct |
57 |
Correct |
24 ms |
45156 KB |
Output is correct |
58 |
Correct |
24 ms |
45264 KB |
Output is correct |
59 |
Correct |
27 ms |
45240 KB |
Output is correct |
60 |
Correct |
24 ms |
45088 KB |
Output is correct |
61 |
Correct |
24 ms |
45808 KB |
Output is correct |
62 |
Correct |
26 ms |
45832 KB |
Output is correct |
63 |
Correct |
24 ms |
45140 KB |
Output is correct |
64 |
Correct |
24 ms |
45304 KB |
Output is correct |
65 |
Correct |
23 ms |
45012 KB |
Output is correct |
66 |
Correct |
24 ms |
45412 KB |
Output is correct |
67 |
Correct |
23 ms |
45164 KB |
Output is correct |
68 |
Correct |
25 ms |
45396 KB |
Output is correct |
69 |
Correct |
26 ms |
45388 KB |
Output is correct |
70 |
Correct |
24 ms |
45176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
44884 KB |
Output is correct |
2 |
Correct |
22 ms |
44936 KB |
Output is correct |
3 |
Correct |
25 ms |
44984 KB |
Output is correct |
4 |
Correct |
23 ms |
44948 KB |
Output is correct |
5 |
Correct |
22 ms |
44896 KB |
Output is correct |
6 |
Correct |
22 ms |
44940 KB |
Output is correct |
7 |
Correct |
22 ms |
44932 KB |
Output is correct |
8 |
Correct |
23 ms |
44920 KB |
Output is correct |
9 |
Correct |
22 ms |
44884 KB |
Output is correct |
10 |
Correct |
22 ms |
44896 KB |
Output is correct |
11 |
Correct |
24 ms |
44896 KB |
Output is correct |
12 |
Correct |
22 ms |
44864 KB |
Output is correct |
13 |
Correct |
24 ms |
45028 KB |
Output is correct |
14 |
Correct |
23 ms |
45020 KB |
Output is correct |
15 |
Correct |
21 ms |
44928 KB |
Output is correct |
16 |
Correct |
20 ms |
44936 KB |
Output is correct |
17 |
Correct |
26 ms |
45056 KB |
Output is correct |
18 |
Correct |
25 ms |
44924 KB |
Output is correct |
19 |
Correct |
22 ms |
44980 KB |
Output is correct |
20 |
Correct |
22 ms |
44932 KB |
Output is correct |
21 |
Correct |
24 ms |
45012 KB |
Output is correct |
22 |
Correct |
21 ms |
44928 KB |
Output is correct |
23 |
Correct |
22 ms |
44912 KB |
Output is correct |
24 |
Correct |
22 ms |
44884 KB |
Output is correct |
25 |
Correct |
22 ms |
44920 KB |
Output is correct |
26 |
Correct |
22 ms |
44896 KB |
Output is correct |
27 |
Correct |
22 ms |
44880 KB |
Output is correct |
28 |
Correct |
23 ms |
44952 KB |
Output is correct |
29 |
Correct |
25 ms |
45028 KB |
Output is correct |
30 |
Correct |
22 ms |
44952 KB |
Output is correct |
31 |
Correct |
25 ms |
44936 KB |
Output is correct |
32 |
Correct |
25 ms |
44880 KB |
Output is correct |
33 |
Correct |
23 ms |
44928 KB |
Output is correct |
34 |
Correct |
23 ms |
44884 KB |
Output is correct |
35 |
Correct |
23 ms |
44888 KB |
Output is correct |
36 |
Correct |
22 ms |
44928 KB |
Output is correct |
37 |
Correct |
24 ms |
44968 KB |
Output is correct |
38 |
Correct |
21 ms |
45012 KB |
Output is correct |
39 |
Correct |
25 ms |
45012 KB |
Output is correct |
40 |
Correct |
23 ms |
45012 KB |
Output is correct |
41 |
Correct |
22 ms |
44884 KB |
Output is correct |
42 |
Correct |
22 ms |
44924 KB |
Output is correct |
43 |
Correct |
22 ms |
44904 KB |
Output is correct |
44 |
Correct |
24 ms |
44884 KB |
Output is correct |
45 |
Correct |
23 ms |
44884 KB |
Output is correct |
46 |
Correct |
22 ms |
44928 KB |
Output is correct |
47 |
Correct |
28 ms |
44980 KB |
Output is correct |
48 |
Correct |
22 ms |
44908 KB |
Output is correct |
49 |
Correct |
22 ms |
44976 KB |
Output is correct |
50 |
Correct |
21 ms |
45012 KB |
Output is correct |
51 |
Correct |
27 ms |
45140 KB |
Output is correct |
52 |
Correct |
27 ms |
45428 KB |
Output is correct |
53 |
Correct |
25 ms |
45568 KB |
Output is correct |
54 |
Correct |
24 ms |
45384 KB |
Output is correct |
55 |
Correct |
23 ms |
45200 KB |
Output is correct |
56 |
Correct |
24 ms |
45188 KB |
Output is correct |
57 |
Correct |
24 ms |
45156 KB |
Output is correct |
58 |
Correct |
24 ms |
45264 KB |
Output is correct |
59 |
Correct |
27 ms |
45240 KB |
Output is correct |
60 |
Correct |
24 ms |
45088 KB |
Output is correct |
61 |
Correct |
24 ms |
45808 KB |
Output is correct |
62 |
Correct |
26 ms |
45832 KB |
Output is correct |
63 |
Correct |
24 ms |
45140 KB |
Output is correct |
64 |
Correct |
24 ms |
45304 KB |
Output is correct |
65 |
Correct |
23 ms |
45012 KB |
Output is correct |
66 |
Correct |
24 ms |
45412 KB |
Output is correct |
67 |
Correct |
23 ms |
45164 KB |
Output is correct |
68 |
Correct |
25 ms |
45396 KB |
Output is correct |
69 |
Correct |
26 ms |
45388 KB |
Output is correct |
70 |
Correct |
24 ms |
45176 KB |
Output is correct |
71 |
Correct |
65 ms |
53036 KB |
Output is correct |
72 |
Correct |
59 ms |
51076 KB |
Output is correct |
73 |
Correct |
55 ms |
50736 KB |
Output is correct |
74 |
Correct |
82 ms |
57376 KB |
Output is correct |
75 |
Correct |
77 ms |
56416 KB |
Output is correct |
76 |
Correct |
643 ms |
124084 KB |
Output is correct |
77 |
Correct |
640 ms |
108684 KB |
Output is correct |
78 |
Correct |
261 ms |
72880 KB |
Output is correct |
79 |
Correct |
492 ms |
107652 KB |
Output is correct |
80 |
Correct |
518 ms |
102860 KB |
Output is correct |
81 |
Correct |
66 ms |
51832 KB |
Output is correct |
82 |
Correct |
54 ms |
50096 KB |
Output is correct |
83 |
Correct |
46 ms |
50128 KB |
Output is correct |
84 |
Correct |
94 ms |
57156 KB |
Output is correct |
85 |
Correct |
80 ms |
54592 KB |
Output is correct |
86 |
Correct |
390 ms |
95948 KB |
Output is correct |
87 |
Correct |
358 ms |
95968 KB |
Output is correct |
88 |
Correct |
240 ms |
70992 KB |
Output is correct |
89 |
Correct |
433 ms |
97248 KB |
Output is correct |
90 |
Correct |
467 ms |
93204 KB |
Output is correct |