首页 > 其他 > 详细

「CSA Round #41」BFS-DFS

时间:2019-07-22 23:00:31      阅读:102      评论:0      收藏:0      [点我收藏+]

题目链接

戳我

\(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);
}

「CSA Round #41」BFS-DFS

原文:https://www.cnblogs.com/hbxblog/p/11228850.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!