# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
333504 | leinad2 | 족보 (KOI18_family) | C++17 | 437 ms | 50028 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;
int k, par[300010], i, j, a, s[300010];
vector<pair<int, int> >v;
struct tree
{
int n, leaf[300010], sz[300010];
vector<int>adj[300010];
void dfs(int v)
{
if(v&&v<=k)
{
sz[v]=1;
leaf[v]=v;
return;
}
for(int x=0;x<adj[v].size();x++)
{
dfs(adj[v][x]);
leaf[v]=leaf[adj[v][x]];
sz[v]+=sz[adj[v][x]];
}
}
}t1, t2;
int Find(int x)
{
if(x==par[x])return x;
return par[x]=Find(par[x]);
}
void Union(int a, int b)
{
a=Find(a);
b=Find(b);
if(a!=b)
{
s[a]+=s[b];
s[b]=0;
par[b]=a;
}
}
int main()
{
scanf("%d %d %d", &t1.n, &t2.n, &k);
for(i=0;i++<k;)par[i]=i,s[i]=1;
for(i=0;i++<t1.n;)
{
scanf("%d", &a);
t1.adj[a].push_back(i);
}
for(i=0;i++<t2.n;)
{
scanf("%d", &a);
t2.adj[a].push_back(i);
}
t1.dfs(0);
t2.dfs(0);
for(i=0;i++<t1.n;)
{
if(t1.adj[i].size()>1)v.push_back({t1.sz[i], i});
}
for(i=0;i++<t2.n;)
{
if(t2.adj[i].size()>1)v.push_back({t2.sz[i], i+t1.n});
}
sort(v.begin(), v.end());
for(pair<int, int>i:v)
{
if(i.second>t1.n)
{
i.second-=t1.n;
a=t2.leaf[i.second];
for(int j:t2.adj[i.second])
{
Union(t2.leaf[j], a);
}
}
else
{
a=t1.leaf[i.second];
for(int j:t1.adj[i.second])
{
Union(t1.leaf[j], a);
}
}
if(s[Find(a)]!=i.first)
{
puts("NO");
return 0;
}
}
puts("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... |