This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 3e5+10;
int n[2],m;
int fa[2][maxn];
map <int,int> mp[2][maxn];
int sz[2][maxn];
vector <int> edge[2][maxn];
vector <int> pt[2];
int getf(int id,int node) {
if (fa[id][node]==node) return fa[id][node];
else return fa[id][node] = getf(id,fa[id][node]);
}
int p[2][maxn];
int dfs(int id,int u) {
if (u<m) return 1;
sz[id][u]=0;
pt[id].pb(u);
for (int v:edge[id][u]) {
sz[id][u]+=dfs(id,v);
}
return sz[id][u];
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie();
cin>>n[0]>>n[1]>>m;
memset(sz,0,sizeof(sz));
int rt[2];
rep(k,0,2) rep(i,0,n[k]) {
cin>>p[k][i];
p[k][i]--;
if (p[k][i]!=-1) edge[k][p[k][i]].pb(i);
else rt[k] = i;
if (i<m) fa[k][i] = p[k][i];
else fa[k][i] = i;
}
dfs(0,rt[0]);
dfs(1,rt[1]);
rep(k,0,2)
rep(i,0,m) {
int ff1 = getf(k,i);
int ff2 = getf(1-k,i);
mp[k][ff1][ff2] = 1;
}
while (!pt[0].empty() or !pt[1].empty()) {
int x,y;
if (pt[0].empty()) x = 1, y = pt[1].back(), pt[1].pop_back();
else if (pt[1].empty()) x = 0, y = pt[0].back(), pt[0].pop_back();
else {
if (sz[0][pt[0].back()] < sz[1][pt[1].back()]) x = 0, y = pt[0].back(), pt[0].pop_back();
else x = 1, y = pt[1].back(), pt[1].pop_back();
}
int tmp = getf(1-x,(mp[x][y].begin())->fi);
if (mp[x][y].size()>1) {
for (auto it:mp[x][y]) {
if (getf(1-x,it.fi)!=tmp) {
cout<<"NO";
return 0;
}
}
}
// debug(tmp);
if (p[x][y]!=-1) mp[x][y][tmp] = 1, fa[x][y] = p[x][y];
}
cout<<"YES";
return 0;
}
# | 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... |