# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
130369 | red1108 | 족보 (KOI18_family) | C++17 | 15 ms | 14456 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];
priority_queue<pair<int,pair<int,int>>> pq;
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]){
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++)
pq.push(make_pair(-tsize[0][i],make_pair(0,i)));
for(i=p+1;i<=n2;i++)
pq.push(make_pair(-tsize[1][i],make_pair(1,i)));
while(!pq.empty()){
int x = pq.top().second.second;
int t = pq.top().second.first;
pq.pop();
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("(%d %d) = %d \n", t, x, tsize[t][x]);
}
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... |