列表

详情


NC211818. TreeProjection

描述

Given two permutations , you should construct an unrooted tree , which satisfies that is a possible topological order of if A_1 is made the root, and that is a possible dfs order of if B_1 is made the root.

Here, a permutation of size is a valid topological order of a rooted tree of size iff that for all edges in , the parent nodes are at front of the child nodes in permutation .

输入描述

The first line contains one integer , denoting the size of permutations.

The second line contains integers , denoting permutation .

The third line contains integers , denoting permutation .

输出描述

If solution exists, print "YES" in one line, and following  lines each contains two integers , denoting one edge in . If multiple solutions exist, print any one of them. If no solution, print "NO" in one line.

示例1

输入:

5
2 1 4 3 5
4 2 5 1 3

输出:

YES
1 2
1 3
2 4
2 5

说明:

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 477ms, 内存消耗: 5364K, 提交时间: 2020-10-25 16:45:59

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
int n;
int a[N],b[N],p[N];

int main(){
	cin>>n;
	for(int i=1;i<=n;++i){
		cin>>a[i];
		p[a[i]]=i;
	}
	for(int i=1;i<=n;++i)cin>>b[i];
	int x=b[1];
	puts("YES");
	for(int i=2;i<=n;++i){
		cout<<b[i]<<" "<<x<<endl;
		if(p[b[i]]<p[x])x=b[i];
	}
}

上一题