「CSA Round #41」BFS-DFS

题目链接

戳我

\(Description\)

给出一个图的\(bfs\)序和\(dfs\)序,构造出一个满足条件的图,边的扫描顺序为读入顺序

\(Solution\)

这个题还是很简单的.

先来看看无解的情况:当\(bfs\)序和\(dfs\)序的第二个不同时无解,因为是按边的顺序遍历,所以前两个一点定一样.

对于\(dfs\)序,我们把他假设为\(b_1,b_2,b_3,b_4...\)
\(->\)表示边
\(b_1->b_2,b_2->b_3,b_3->b_4...\)
对于\(bfs\)序,我们把他假设为\(c_1,c_2,c_3,c_4...\)
\(1->c_3,1->c_4,1->c_5...\)
注意不要将\(1->c_2\)因为这样会有重边

\(Code\)

#include<bits/stdc++.h>
#define file(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
using namespace std;
const int inf=1e9;
typedef long long ll;
inline int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') f=(c=='-')?-1:1,c=getchar();
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}
struct node {
	int x,y;
}a[1000001];
int b[10001],c[10001];
int main(){
	int n=read(),tot=0;
	for(int i=1;i<=n;i++)
		c[i]=read();
	for(int i=1;i<=n;i++)
		b[i]=read();
	if(b[1]!=c[1]||b[2]!=c[2]) puts("-1"),exit(0);
	for(int i=2;i<=n;i++)
		a[++tot].x=b[i-1],a[tot].y=b[i];
	for(int i=3;i<=n;i++)
		a[++tot].x=1,a[tot].y=c[i];
	printf("%d\n",tot);
	for(int i=1;i<=tot;i++)
		printf("%d %d\n",a[i].x,a[i].y);
}
posted @ 2019-07-22 22:12  撤云  阅读(130)  评论(0编辑  收藏  举报
……