# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
333500 | 2020-12-06T14:14:00 Z | leinad2 | None (KOI18_family) | C++17 | 10 ms | 14444 KB |
#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.sz[i]>1)v.push_back({t1.sz[i], i}); } for(i=0;i++<t2.n;) { if(t2.sz[i]>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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 14444 KB | Output is correct |
2 | Incorrect | 10 ms | 14444 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 14444 KB | Output is correct |
2 | Incorrect | 10 ms | 14444 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 14444 KB | Output is correct |
2 | Incorrect | 10 ms | 14444 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 10 ms | 14444 KB | Output is correct |
2 | Incorrect | 10 ms | 14444 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |