#include<stdio.h>
#include<stdlib.h>
struct map{
int data[10][10];
};
struct stack{
int Ver1,Ver2;
struct stack*next;
};
struct line{
int Ver1,Ver2;
struct line*next;
};
struct edge{
int Ver1,Ver2;
int weight;
};
struct map* Creat_map(){
struct map*p;
p=(struct map*)malloc(sizeof(struct map));
return p;
}
struct edge*Creat_edge(){
struct edge*p;
p=(struct edge*)malloc(sizeof(struct edge));
return p;
}
struct line *Creat_line(){
struct line*p;
p=(struct line*)malloc(sizeof(struct line));
p->next=NULL;
return p;
}
struct stack*Creat_stack(){
struct stack*p;
p=(struct stack*)malloc(sizeof(struct stack));
p->next=NULL;
return p;
}
void add(struct edge*p_edge){
scanf("%d%d",&p_edge->Ver1,&p_edge->Ver2);
p_edge->weight=1;
}
void insert_edge(struct edge*p_edge,struct map*p_map){
p_map->data[p_edge->Ver1][p_edge->Ver2]=p_edge->weight;
p_map->data[p_edge->Ver2][p_edge->Ver1]=p_edge->weight;
}
void insert_stack(struct stack*head_stack,int hang,int lie){
struct stack*p;
p=(struct stack*)malloc(sizeof(struct stack));
p->Ver1=hang;
p->Ver2=lie;
p->next=head_stack->next;
head_stack->next=p;
}
struct line*inesert_line(struct line*rear,int hang,int lie){
struct line*p;
p=(struct line *)malloc(sizeof(struct line));
p->Ver1=hang;
p->Ver2=lie;
p->next=NULL;
rear->next=p;
return p;
}
void del_stack(struct stack*head_stack){
struct stack*p;
p=head_stack->next;
head_stack->next=p->next;
free(p);
}
void del_line(struct line*head_line){
struct line*p;
p=head_line->next;
head_line->next=p->next;
free(p);
}
void Clear(int N,struct map*p){
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++){
p->data[i][j]=0;
}
}
void DFS(int N,struct map*p_map,struct stack*head_stack){
int *data;
int i,j;
int lie;
int count;
data=(int *)malloc(N*sizeof(int));
for(i=N-1;i>=0;i--)
data[i]=0;
i=0;
while(i<N){
if(!data[i]){
printf("{ %d",i);
data[i]=1;
count=0;
for(j=N-1;j>=0;j--){
if(p_map->data[i][j]){
insert_stack(head_stack,i, j);
count++;
}
}
while(count){
lie=head_stack->next->Ver2;
del_stack(head_stack);
count--;
if(!data[lie]){
printf(" %d",lie);
data[lie]=1;
}
for(j=N-1;j>=0;j--){
if(p_map->data[lie][j]&&!data[j]){
insert_stack(head_stack, lie, j);
count++;
}
}
}
printf(" }\n");
}
i++;
}\
free(data);
}
void BFS(struct line*front,struct line*rear,int N,struct map*p_map){
int *data;
data=(int *)malloc(N*sizeof(int ));
int i=0,j,lie,count;
for(j=0;j<N;j++)
data[j]=0;
while(i<N){
if(!data[i]){
printf("{ %d",i);
data[i]=1;
count=0;
for(j=0;j<N;j++){
if(p_map->data[i][j]){
rear=inesert_line(rear, i, j);
count++;
}
}
while(count){
lie=front->next->Ver2;
del_line(front);
count--;
if(!data[lie]){
printf(" %d",lie);
data[lie]=1;
}
for(j=0;j<N;j++){
if(p_map->data[lie][j]&&!data[j]){
rear=inesert_line(rear, lie, j);
count ++;
}
}
}
printf(" }\n");
}
i++;
rear=front;
}
free(data);
}
int main(){
struct map*p_map;
struct edge*p_edge;
struct line*front,*rear;
struct stack*head_stack;
int M,E,i;
scanf("%d%d",&M,&E);
p_map=Creat_map();
p_edge=Creat_edge();
front=rear=Creat_line();
head_stack=Creat_stack();
Clear(M, p_map);
for(i=0;i<E;i++){
add(p_edge);
insert_edge(p_edge, p_map);
}
DFS(M, p_map, head_stack);
BFS(front, rear, M, p_map);
free(p_edge);
free(p_map);
return 0;
}