https://www.acmicpc.net/problem/1260
#include<stdio.h>
#include<stdbool.h>
static int edge[1001][1001];
static bool check[1001];
static bool check2[1001];
static int q[1001];
static int n;
static int m;
static int v;
static int first;
static int back;
static void dfs(int v){
check[v]=true;
printf("%d ",v);
for(int i=1;i<=n;i++){
if(edge[v][i]==1 && check[i]==false) dfs(i);
}
}
static void bfs(int s){
q[back]=s;
back++;
check2[s]=true;
while(back>first){
int v=q[first];
q[first]=0;
first++;
printf("%d ",v);
for(int i=1;i<=n;i++){
if(edge[v][i]==1 && check2[i]==false){
q[back]=i;
back++;
check2[i]=true;
}
}
}
}
int main() {
scanf("%d %d %d",&n,&m,&v);
first=back=0;
while(m-->0){
int f,t;
scanf("%d %d",&f,&t);
edge[f][t]=edge[t][f]=1;
}
dfs(v);
printf("\n");
bfs(v);
}