https://www.acmicpc.net/problem/1260
using System;
using System.Collections;
class Program
{
static int[,] edge;
static bool[] check;
static int n;
static int m;
static int v;
static void dfs(int v){
check[v]=true;
Console.Write(v+" ");
for(int i=1;i<=n;i++){
if(edge[v,i]==1 && check[i]==false) dfs(i);
}
}
static void bfs(int s){
Queue q=new Queue();
q.Enqueue(s);
check[s]=true;
while(q.Count!=0){
int v=(int)q.Dequeue();
Console.Write(v+" ");
for(int i=1;i<=n;i++){
if(edge[v,i]==1 && check[i]==false){
q.Enqueue(i);
check[i]=true;
}
}
}
}
static void Main() {
string[] s=Console.ReadLine().Split(' ');
n=int.Parse(s[0]);
m=int.Parse(s[1]);
v=int.Parse(s[2]);
edge=new int[n+1,n+1];
check=new bool[n+1];
while(m-->0){
string[] s1=Console.ReadLine().Split(' ');
int f=int.Parse(s1[0]);
int t=int.Parse(s1[1]);
edge[f,t]=edge[t,f]=1;
}
dfs(v);
check=new bool[n+1];
Console.WriteLine();
bfs(v);
}
}