「CSA Round #41」BFS-DFS

Topic Link

I poke

\(Description\)

Gives a graph \ (BFS \) sequence and \ (DFS \) sequence, to construct a satisfying the condition of FIG scanning order side is sequentially reading

\(Solution\)

This question is very simple.

Let's look at the case of no solution: When \ (BFS \) sequence and \ (DFS \) a second solution at the same time does not, because it is ordered traversal of the edge, so that the first two sequence as given.

For \ (dfs \) sequence, we assume that he is \ (B_1, B_2, b_3, B_4 ... \)
\ (-> \) represent the edges of
the \ (b_1-> b_2, b_2-> b_3, b_3- > b_4 ... \)
to \ (bfs \) sequence, we assume that he is \ (c_1, c_2, c_3,
c_4 ... \) will be \ (1-> c_3,1-> c_4,1-> c_5 ... \)
be careful not to \ (1-> c_2 \) because it will be heavy side

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

Guess you like

Origin www.cnblogs.com/hbxblog/p/11228850.html