답안 #557416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
557416 2022-05-05T10:14:04 Z myrcella 족보 (KOI18_family) C++17
0 / 100
24 ms 44964 KB
//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];

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 in[2][maxn];
pq <pair<int,pii>, vector<pair<int,pii>>, greater <pair<int,pii>>> q;

int dfs(int id,int u) {
	if (u<m) return 1;
	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;
		}
	rep(i,0,2)
		rep(j,m,n[i]) {
			q.push({sz[i][j],{i,j}});
		}
	while (!q.empty()) {
		auto cur = q.top();
//							debug(cur.se.fi),debug(cur.se.se);

		q.pop();
		int tmp = getf(1-cur.se.fi,(mp[cur.se.fi][cur.se.se].begin())->fi);
		if (mp[cur.se.fi][cur.se.se].size()>1) {
			for (auto it:mp[cur.se.fi][cur.se.se]) {
				if (getf(1-cur.se.fi,it.fi)!=tmp) {
					cout<<"NO";
					return 0;
				}
			}
			
		}
		if (p[cur.se.fi][cur.se.se]!=-1) mp[cur.se.fi][p[cur.se.fi][cur.se.se]][tmp] = 1, fa[cur.se.fi][cur.se.se] = p[cur.se.fi][cur.se.se];
	}
	cout<<"YES";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 44872 KB Output is correct
2 Incorrect 22 ms 44964 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 44872 KB Output is correct
2 Incorrect 22 ms 44964 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 44872 KB Output is correct
2 Incorrect 22 ms 44964 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 44872 KB Output is correct
2 Incorrect 22 ms 44964 KB Output isn't correct
3 Halted 0 ms 0 KB -