# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
130372 | red1108 | 족보 (KOI18_family) | C++17 | 584 ms | 74512 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<int> tree[2][300010];
int par[300010], n1, n2, p, root1, root2;
int tsize[2][300010], sth[2][300010], siz[300010], depth[2][300010];
struct A{
int siz, t, x, depth;
bool operator <(const A c) const{
if(c.siz==siz) return depth>c.depth;
return siz<c.siz;
}
};
vector<A> q;
void dfs(int t, int x){
if(x<=p) tsize[t][x]=1;
if(x<=p) sth[t][x]=x;
for(auto i:tree[t][x]){
depth[t][i]=depth[t][x]+1;
dfs(t, i);
tsize[t][x]+=tsize[t][i];
if(sth[t][i]<=p) sth[t][x]=sth[t][i];
}
}
int findpar(int x){
if(x==par[x]) return x;
return par[x]=findpar(par[x]);
}
void hap(int x, int y){
if(findpar(x)==findpar(y)) return ;
siz[findpar(y)]+=siz[findpar(x)];
par[findpar(x)]=findpar(y);
}
int main(){
int i;
scanf("%d %d %d", &n1, &n2, &p);
for(i=1;i<=n1;i++){
int a;
scanf("%d", &a);
tree[0][a].push_back(i);
if(a==0) root1=i;
}
for(i=1;i<=n2;i++){
int a;
scanf("%d", &a);
tree[1][a].push_back(i);
if(a==0) root2=i;
}
for(i=1;i<=p;i++) {
par[i]=i;
siz[i]=1;
}
dfs(0,root1);
dfs(1,root2);
for(i=p+1;i<=n1;i++) q.push_back({tsize[0][i],0,i,depth[0][i]});
for(i=p+1;i<=n2;i++) q.push_back({tsize[1][i],1,i,depth[1][i]});
sort(q.begin(), q.end());
for(auto j:q){
int x = j.x;
int t = j.t;
int fi=sth[t][tree[t][x][0]];
for(auto i:tree[t][x])
hap(fi,sth[t][i]);
if(siz[findpar(fi)]!=tsize[t][x]){
printf("NO");
return 0;
}
}
printf("YES");
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |