#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=1e9;
const int MAXN=3e5+10;
vector<int>adj[2][MAXN];
bool vi[MAXN];
int K,i,j;
int root[2],N[2];
int pa[2][MAXN],leaf[2][MAXN];
int lev[MAXN],sz[MAXN],par[MAXN];
int chk[MAXN];
int cnt;
int parent(int x)
{
if(par[x]==-1)
return x;
return par[x]=parent(par[x]);
}
void Union(int x,int y)
{
int X=parent(x);
int Y=parent(y);
if(X==Y)
return;
if(lev[X]>lev[Y])
{
par[Y]=X;
sz[X]+=sz[Y];
}
else if(lev[Y]>lev[X])
{
par[X]=Y;
sz[Y]+=sz[X];
lev[Y]++;
}
else if(sz[X]>sz[Y])
{
par[Y]=X;
sz[X]+=sz[Y];
lev[X]++;
}
else
{
par[X]=Y;
sz[Y]+=sz[X];
}
return;
}
struct NODE
{
int sz,lv;
int tree,node;
}tree[2][MAXN];
vector<NODE>sorting;
bool cmp(NODE a,NODE b)
{
return a.sz!=b.sz?a.sz<b.sz:a.lv>b.lv;
}
void pc(int t,int h)
{
if(vi[h])
return;
vi[h]=true;
if(pa[t][h]!=-1)
{
adj[t][pa[t][h]].push_back(h);
pc(t,pa[t][h]);
}
return;
}
pair<int,int>dfs(int t,int h,int lv)
{
tree[t][h].lv=lv;
tree[t][h].tree=t;
tree[t][h].node=h;
if(adj[t][h].empty())
{
leaf[t][h]=h;
tree[t][h].sz=1;
return make_pair(h,1);
}
pair<int,int>P;
P=dfs(t,adj[t][h][0],lv+1);
leaf[t][h]=P.first;
int s=P.second;
for(int i=1;i<adj[t][h].size();i++)
{
P=dfs(t,adj[t][h][i],lv+1);
s+=P.second;
}
tree[t][h].sz=s;
return make_pair(leaf[t][h],tree[t][h].sz);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>N[0]>>N[1]>>K;
for(i=0;i<N[0];i++)
{
cin>>pa[0][i];
if(pa[0][i]==0)
root[0]=i;
pa[0][i]--;
}
for(i=0;i<N[1];i++)
{
cin>>pa[1][i];
if(pa[1][i]==0)
root[1]=i;
pa[1][i]--;
}
fill(vi,vi+N[0],false);
for(i=0;i<K;i++)
pc(0,i);
fill(vi,vi+N[1],false);
for(i=0;i<K;i++)
pc(1,i);
dfs(0,root[0],0);
dfs(1,root[1],0);
for(i=0;i<2;i++)
for(j=K;j<N[i];j++)
sorting.push_back(tree[i][j]);
sort(sorting.begin(),sorting.end(),cmp);
fill(par,par+MAXN,-1);
fill(sz,sz+MAXN,1);
for(i=0;i<sorting.size();i++)
{
NODE X=sorting[i];
for(j=0;j<adj[X.tree][X.node].size();j++)
{
int Y=adj[X.tree][X.node][j];
Union(leaf[X.tree][X.node],leaf[X.tree][Y]);
}
if(sz[parent(leaf[X.tree][X.node])]!=X.sz)
return cout<<"NO",0;
}
cout<<"YES";
return 0;
}
Compilation message
family.cpp: In function 'std::pair<int, int> dfs(int, int, int)':
family.cpp:88:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<adj[t][h].size();i++)
~^~~~~~~~~~~~~~~~~
family.cpp: In function 'int main()':
family.cpp:130:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<sorting.size();i++)
~^~~~~~~~~~~~~~~
family.cpp:133:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<adj[X.tree][X.node].size();j++)
~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16888 KB |
Output is correct |
3 |
Correct |
18 ms |
16928 KB |
Output is correct |
4 |
Correct |
17 ms |
16928 KB |
Output is correct |
5 |
Correct |
17 ms |
17012 KB |
Output is correct |
6 |
Correct |
18 ms |
17016 KB |
Output is correct |
7 |
Correct |
17 ms |
17052 KB |
Output is correct |
8 |
Correct |
19 ms |
17080 KB |
Output is correct |
9 |
Correct |
18 ms |
17080 KB |
Output is correct |
10 |
Correct |
24 ms |
17120 KB |
Output is correct |
11 |
Correct |
21 ms |
17120 KB |
Output is correct |
12 |
Correct |
20 ms |
17120 KB |
Output is correct |
13 |
Correct |
17 ms |
17132 KB |
Output is correct |
14 |
Correct |
18 ms |
17136 KB |
Output is correct |
15 |
Correct |
19 ms |
17232 KB |
Output is correct |
16 |
Correct |
18 ms |
17232 KB |
Output is correct |
17 |
Correct |
17 ms |
17232 KB |
Output is correct |
18 |
Correct |
18 ms |
17312 KB |
Output is correct |
19 |
Correct |
20 ms |
17312 KB |
Output is correct |
20 |
Correct |
20 ms |
17312 KB |
Output is correct |
21 |
Correct |
19 ms |
17312 KB |
Output is correct |
22 |
Correct |
18 ms |
17312 KB |
Output is correct |
23 |
Correct |
19 ms |
17312 KB |
Output is correct |
24 |
Correct |
20 ms |
17312 KB |
Output is correct |
25 |
Correct |
18 ms |
17312 KB |
Output is correct |
26 |
Correct |
17 ms |
17312 KB |
Output is correct |
27 |
Correct |
20 ms |
17312 KB |
Output is correct |
28 |
Correct |
21 ms |
17312 KB |
Output is correct |
29 |
Correct |
19 ms |
17312 KB |
Output is correct |
30 |
Correct |
20 ms |
17312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16888 KB |
Output is correct |
3 |
Correct |
18 ms |
16928 KB |
Output is correct |
4 |
Correct |
17 ms |
16928 KB |
Output is correct |
5 |
Correct |
17 ms |
17012 KB |
Output is correct |
6 |
Correct |
18 ms |
17016 KB |
Output is correct |
7 |
Correct |
17 ms |
17052 KB |
Output is correct |
8 |
Correct |
19 ms |
17080 KB |
Output is correct |
9 |
Correct |
18 ms |
17080 KB |
Output is correct |
10 |
Correct |
24 ms |
17120 KB |
Output is correct |
11 |
Correct |
21 ms |
17120 KB |
Output is correct |
12 |
Correct |
20 ms |
17120 KB |
Output is correct |
13 |
Correct |
17 ms |
17132 KB |
Output is correct |
14 |
Correct |
18 ms |
17136 KB |
Output is correct |
15 |
Correct |
19 ms |
17232 KB |
Output is correct |
16 |
Correct |
18 ms |
17232 KB |
Output is correct |
17 |
Correct |
17 ms |
17232 KB |
Output is correct |
18 |
Correct |
18 ms |
17312 KB |
Output is correct |
19 |
Correct |
20 ms |
17312 KB |
Output is correct |
20 |
Correct |
20 ms |
17312 KB |
Output is correct |
21 |
Correct |
19 ms |
17312 KB |
Output is correct |
22 |
Correct |
18 ms |
17312 KB |
Output is correct |
23 |
Correct |
19 ms |
17312 KB |
Output is correct |
24 |
Correct |
20 ms |
17312 KB |
Output is correct |
25 |
Correct |
18 ms |
17312 KB |
Output is correct |
26 |
Correct |
17 ms |
17312 KB |
Output is correct |
27 |
Correct |
20 ms |
17312 KB |
Output is correct |
28 |
Correct |
21 ms |
17312 KB |
Output is correct |
29 |
Correct |
19 ms |
17312 KB |
Output is correct |
30 |
Correct |
20 ms |
17312 KB |
Output is correct |
31 |
Correct |
18 ms |
17312 KB |
Output is correct |
32 |
Correct |
17 ms |
17312 KB |
Output is correct |
33 |
Correct |
18 ms |
17312 KB |
Output is correct |
34 |
Correct |
19 ms |
17312 KB |
Output is correct |
35 |
Correct |
20 ms |
17312 KB |
Output is correct |
36 |
Correct |
19 ms |
17312 KB |
Output is correct |
37 |
Correct |
20 ms |
17312 KB |
Output is correct |
38 |
Correct |
19 ms |
17312 KB |
Output is correct |
39 |
Correct |
17 ms |
17312 KB |
Output is correct |
40 |
Correct |
17 ms |
17312 KB |
Output is correct |
41 |
Correct |
20 ms |
17312 KB |
Output is correct |
42 |
Correct |
19 ms |
17324 KB |
Output is correct |
43 |
Correct |
18 ms |
17324 KB |
Output is correct |
44 |
Correct |
20 ms |
17324 KB |
Output is correct |
45 |
Correct |
17 ms |
17324 KB |
Output is correct |
46 |
Correct |
18 ms |
17328 KB |
Output is correct |
47 |
Correct |
19 ms |
17332 KB |
Output is correct |
48 |
Correct |
17 ms |
17336 KB |
Output is correct |
49 |
Correct |
17 ms |
17340 KB |
Output is correct |
50 |
Correct |
17 ms |
17344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16888 KB |
Output is correct |
3 |
Correct |
18 ms |
16928 KB |
Output is correct |
4 |
Correct |
17 ms |
16928 KB |
Output is correct |
5 |
Correct |
17 ms |
17012 KB |
Output is correct |
6 |
Correct |
18 ms |
17016 KB |
Output is correct |
7 |
Correct |
17 ms |
17052 KB |
Output is correct |
8 |
Correct |
19 ms |
17080 KB |
Output is correct |
9 |
Correct |
18 ms |
17080 KB |
Output is correct |
10 |
Correct |
24 ms |
17120 KB |
Output is correct |
11 |
Correct |
21 ms |
17120 KB |
Output is correct |
12 |
Correct |
20 ms |
17120 KB |
Output is correct |
13 |
Correct |
17 ms |
17132 KB |
Output is correct |
14 |
Correct |
18 ms |
17136 KB |
Output is correct |
15 |
Correct |
19 ms |
17232 KB |
Output is correct |
16 |
Correct |
18 ms |
17232 KB |
Output is correct |
17 |
Correct |
17 ms |
17232 KB |
Output is correct |
18 |
Correct |
18 ms |
17312 KB |
Output is correct |
19 |
Correct |
20 ms |
17312 KB |
Output is correct |
20 |
Correct |
20 ms |
17312 KB |
Output is correct |
21 |
Correct |
19 ms |
17312 KB |
Output is correct |
22 |
Correct |
18 ms |
17312 KB |
Output is correct |
23 |
Correct |
19 ms |
17312 KB |
Output is correct |
24 |
Correct |
20 ms |
17312 KB |
Output is correct |
25 |
Correct |
18 ms |
17312 KB |
Output is correct |
26 |
Correct |
17 ms |
17312 KB |
Output is correct |
27 |
Correct |
20 ms |
17312 KB |
Output is correct |
28 |
Correct |
21 ms |
17312 KB |
Output is correct |
29 |
Correct |
19 ms |
17312 KB |
Output is correct |
30 |
Correct |
20 ms |
17312 KB |
Output is correct |
31 |
Correct |
18 ms |
17312 KB |
Output is correct |
32 |
Correct |
17 ms |
17312 KB |
Output is correct |
33 |
Correct |
18 ms |
17312 KB |
Output is correct |
34 |
Correct |
19 ms |
17312 KB |
Output is correct |
35 |
Correct |
20 ms |
17312 KB |
Output is correct |
36 |
Correct |
19 ms |
17312 KB |
Output is correct |
37 |
Correct |
20 ms |
17312 KB |
Output is correct |
38 |
Correct |
19 ms |
17312 KB |
Output is correct |
39 |
Correct |
17 ms |
17312 KB |
Output is correct |
40 |
Correct |
17 ms |
17312 KB |
Output is correct |
41 |
Correct |
20 ms |
17312 KB |
Output is correct |
42 |
Correct |
19 ms |
17324 KB |
Output is correct |
43 |
Correct |
18 ms |
17324 KB |
Output is correct |
44 |
Correct |
20 ms |
17324 KB |
Output is correct |
45 |
Correct |
17 ms |
17324 KB |
Output is correct |
46 |
Correct |
18 ms |
17328 KB |
Output is correct |
47 |
Correct |
19 ms |
17332 KB |
Output is correct |
48 |
Correct |
17 ms |
17336 KB |
Output is correct |
49 |
Correct |
17 ms |
17340 KB |
Output is correct |
50 |
Correct |
17 ms |
17344 KB |
Output is correct |
51 |
Correct |
20 ms |
17604 KB |
Output is correct |
52 |
Correct |
22 ms |
17832 KB |
Output is correct |
53 |
Correct |
21 ms |
18120 KB |
Output is correct |
54 |
Correct |
24 ms |
18120 KB |
Output is correct |
55 |
Correct |
21 ms |
18120 KB |
Output is correct |
56 |
Correct |
27 ms |
18120 KB |
Output is correct |
57 |
Correct |
23 ms |
18120 KB |
Output is correct |
58 |
Correct |
22 ms |
18304 KB |
Output is correct |
59 |
Correct |
21 ms |
18304 KB |
Output is correct |
60 |
Correct |
22 ms |
18304 KB |
Output is correct |
61 |
Correct |
22 ms |
18732 KB |
Output is correct |
62 |
Correct |
25 ms |
18784 KB |
Output is correct |
63 |
Correct |
21 ms |
18784 KB |
Output is correct |
64 |
Correct |
20 ms |
18784 KB |
Output is correct |
65 |
Correct |
20 ms |
18784 KB |
Output is correct |
66 |
Correct |
23 ms |
18784 KB |
Output is correct |
67 |
Correct |
22 ms |
18784 KB |
Output is correct |
68 |
Correct |
24 ms |
18888 KB |
Output is correct |
69 |
Correct |
21 ms |
18904 KB |
Output is correct |
70 |
Correct |
20 ms |
18904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
16888 KB |
Output is correct |
2 |
Correct |
17 ms |
16888 KB |
Output is correct |
3 |
Correct |
18 ms |
16928 KB |
Output is correct |
4 |
Correct |
17 ms |
16928 KB |
Output is correct |
5 |
Correct |
17 ms |
17012 KB |
Output is correct |
6 |
Correct |
18 ms |
17016 KB |
Output is correct |
7 |
Correct |
17 ms |
17052 KB |
Output is correct |
8 |
Correct |
19 ms |
17080 KB |
Output is correct |
9 |
Correct |
18 ms |
17080 KB |
Output is correct |
10 |
Correct |
24 ms |
17120 KB |
Output is correct |
11 |
Correct |
21 ms |
17120 KB |
Output is correct |
12 |
Correct |
20 ms |
17120 KB |
Output is correct |
13 |
Correct |
17 ms |
17132 KB |
Output is correct |
14 |
Correct |
18 ms |
17136 KB |
Output is correct |
15 |
Correct |
19 ms |
17232 KB |
Output is correct |
16 |
Correct |
18 ms |
17232 KB |
Output is correct |
17 |
Correct |
17 ms |
17232 KB |
Output is correct |
18 |
Correct |
18 ms |
17312 KB |
Output is correct |
19 |
Correct |
20 ms |
17312 KB |
Output is correct |
20 |
Correct |
20 ms |
17312 KB |
Output is correct |
21 |
Correct |
19 ms |
17312 KB |
Output is correct |
22 |
Correct |
18 ms |
17312 KB |
Output is correct |
23 |
Correct |
19 ms |
17312 KB |
Output is correct |
24 |
Correct |
20 ms |
17312 KB |
Output is correct |
25 |
Correct |
18 ms |
17312 KB |
Output is correct |
26 |
Correct |
17 ms |
17312 KB |
Output is correct |
27 |
Correct |
20 ms |
17312 KB |
Output is correct |
28 |
Correct |
21 ms |
17312 KB |
Output is correct |
29 |
Correct |
19 ms |
17312 KB |
Output is correct |
30 |
Correct |
20 ms |
17312 KB |
Output is correct |
31 |
Correct |
18 ms |
17312 KB |
Output is correct |
32 |
Correct |
17 ms |
17312 KB |
Output is correct |
33 |
Correct |
18 ms |
17312 KB |
Output is correct |
34 |
Correct |
19 ms |
17312 KB |
Output is correct |
35 |
Correct |
20 ms |
17312 KB |
Output is correct |
36 |
Correct |
19 ms |
17312 KB |
Output is correct |
37 |
Correct |
20 ms |
17312 KB |
Output is correct |
38 |
Correct |
19 ms |
17312 KB |
Output is correct |
39 |
Correct |
17 ms |
17312 KB |
Output is correct |
40 |
Correct |
17 ms |
17312 KB |
Output is correct |
41 |
Correct |
20 ms |
17312 KB |
Output is correct |
42 |
Correct |
19 ms |
17324 KB |
Output is correct |
43 |
Correct |
18 ms |
17324 KB |
Output is correct |
44 |
Correct |
20 ms |
17324 KB |
Output is correct |
45 |
Correct |
17 ms |
17324 KB |
Output is correct |
46 |
Correct |
18 ms |
17328 KB |
Output is correct |
47 |
Correct |
19 ms |
17332 KB |
Output is correct |
48 |
Correct |
17 ms |
17336 KB |
Output is correct |
49 |
Correct |
17 ms |
17340 KB |
Output is correct |
50 |
Correct |
17 ms |
17344 KB |
Output is correct |
51 |
Correct |
20 ms |
17604 KB |
Output is correct |
52 |
Correct |
22 ms |
17832 KB |
Output is correct |
53 |
Correct |
21 ms |
18120 KB |
Output is correct |
54 |
Correct |
24 ms |
18120 KB |
Output is correct |
55 |
Correct |
21 ms |
18120 KB |
Output is correct |
56 |
Correct |
27 ms |
18120 KB |
Output is correct |
57 |
Correct |
23 ms |
18120 KB |
Output is correct |
58 |
Correct |
22 ms |
18304 KB |
Output is correct |
59 |
Correct |
21 ms |
18304 KB |
Output is correct |
60 |
Correct |
22 ms |
18304 KB |
Output is correct |
61 |
Correct |
22 ms |
18732 KB |
Output is correct |
62 |
Correct |
25 ms |
18784 KB |
Output is correct |
63 |
Correct |
21 ms |
18784 KB |
Output is correct |
64 |
Correct |
20 ms |
18784 KB |
Output is correct |
65 |
Correct |
20 ms |
18784 KB |
Output is correct |
66 |
Correct |
23 ms |
18784 KB |
Output is correct |
67 |
Correct |
22 ms |
18784 KB |
Output is correct |
68 |
Correct |
24 ms |
18888 KB |
Output is correct |
69 |
Correct |
21 ms |
18904 KB |
Output is correct |
70 |
Correct |
20 ms |
18904 KB |
Output is correct |
71 |
Correct |
77 ms |
24496 KB |
Output is correct |
72 |
Correct |
74 ms |
24496 KB |
Output is correct |
73 |
Correct |
51 ms |
24496 KB |
Output is correct |
74 |
Correct |
83 ms |
29380 KB |
Output is correct |
75 |
Correct |
97 ms |
29380 KB |
Output is correct |
76 |
Correct |
729 ms |
85856 KB |
Output is correct |
77 |
Correct |
861 ms |
85856 KB |
Output is correct |
78 |
Correct |
403 ms |
85856 KB |
Output is correct |
79 |
Correct |
557 ms |
85856 KB |
Output is correct |
80 |
Correct |
591 ms |
85856 KB |
Output is correct |
81 |
Correct |
69 ms |
85856 KB |
Output is correct |
82 |
Correct |
57 ms |
85856 KB |
Output is correct |
83 |
Correct |
53 ms |
85856 KB |
Output is correct |
84 |
Correct |
82 ms |
85856 KB |
Output is correct |
85 |
Correct |
88 ms |
85856 KB |
Output is correct |
86 |
Correct |
545 ms |
87416 KB |
Output is correct |
87 |
Correct |
601 ms |
91424 KB |
Output is correct |
88 |
Correct |
325 ms |
91424 KB |
Output is correct |
89 |
Correct |
546 ms |
100676 KB |
Output is correct |
90 |
Correct |
552 ms |
106080 KB |
Output is correct |