#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node *next;
} Node;
Node *p,*r;
typedef struct head {
int data;
struct node *ptr;
} Head;
Head heads[100005];
int visited[100005];
void DFS(Head rot) {
// if(visited[rot.data]){
// return ;
// }//and if it is necassary
printf("%d ",rot.data);
visited[rot.data]=1;
Node *q;
q=rot.ptr;
while(q!=NULL) {
if(!visited[q->data]) {
DFS(heads[q->data]);
// visited[q->data]=1;//after ..I will test if this is necessary
}
q=q->next;
}
return ;
}
void BFS() {
Head queue[100005],temp;
Node *q;
int front=0,rear=0,trear;
queue[0]=heads[1];
while(front<=rear) {
trear=rear;
while(front<=trear) {
temp=queue[front];
if(!visited[temp.data]){
printf("%d ",temp.data);
visited[temp.data]=1;
q=heads[temp.data].ptr;
while(q!=NULL){
if(!visited[q->data])
queue[++rear]=heads[q->data];
q=q->next;
}
}
front++;
}
}
}
int main() {
int n,m,i,a,b;
scanf("%d%d",&n,&m);
for(i=0; i<=n; i++) {//initial
heads[i].ptr=NULL;
heads[i].data=i;
}
for(i=0; i<m; i++) {//build graph
scanf("%d%d",&a,&b);
// printf("%d%d\n",a,b);
p=(Node *)malloc(sizeof(Node));
p->data=b;
p->next=NULL;
if(!heads[a].ptr) {
heads[a].data=a;
heads[a].ptr=p;
} else {
r=heads[a].ptr;
if(r->data>b){
p->next=r;
heads[a].ptr=p;
continue;
}
while(r->next!=NULL&&r->next->data<b) {//忘记了排序
r=r->next;
}
p->next=r->next;//没连上啊
r->next=p;
}
}
DFS(heads[1]);
printf("\n");
for(i=0; i<100005; i++) {
visited[i]=0;
}
BFS();
return 0;
}