Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C by cuong ( 6 years ago )
#include<stdio.h>
typedef struct{
int n;
int A[100][100];
}Graph;
void init_graph(Graph* G,int n){
G->n = n;
int i,j;
for(i=1;i<G->n;i++)
for(j=1;j<=G->n;j++)
G->A[i][j] = 0;
}
void add_edge(Graph* G,int x,int y){
G->A[x][y] = 1;
G->A[y][x] = 1;
}
int adjacent(Graph* G,int x,int y){
return G->A[x][y] == 1;
}
void make_graph(Graph* G,int m){
int e,u,v;
for(e=1;e<=m;e++){
scanf("%d%d",&u,&v);
add_edge(G,u,v);
}
}
typedef int Elementype;
typedef struct {
Elementype data[100];
int size;
}List;
void makenull_list(List* L){
L->size = 0;
}
void push_back(List* L,int x){
L->data[L->size] = x;
L->size++;
}
Elementype element_at(List*L,int i){
return L->data[i-1];
}
List neighbors(Graph* G,int x){
int e;
List L;
makenull_list(&L);
for(e=1;e<=G->n;e++)
if(adjacent(G,e,x))
push_back(&L,e);
return L;
}
typedef struct {
int keys[100];
int size;
}Stack;
void makenull_stack(Stack* S){
S->size = 0;
}
void push(Stack* S,int x){
S->keys[S->size] = x;
S->size ++;
}
int top(Stack* S){
return S->keys[S->size-1];
}
void pop (Stack* S){
S->size --;
}
int empty(Stack*S){
return S->size == 0;
}
void DFS(Graph*G){
Stack S;
int mark[100];
makenull_stack(&S);
int i,j;
for(i=1;i<=G->n;i++)
mark[i] = 0;
for(j=1;j<=G->n;j++){
push(&S,j);
while(!empty(&S)){
int x = top(&S); pop(&S);
if(mark[x] != 0)
continue;
printf("%d\n",x);
mark[x] = 1;
List L = neighbors(G,x);
for(i=1;i<=L.size;i++){
int y = element_at(&L,i);
if(mark[y] == 0){
push(&S,y);
}
}
}
}
}
int main(){
Graph G;
int n,m;
scanf("%d%d",&n,&m);
init_graph(&G,n);
make_graph(&G,m);
DFS(&G);
return 0;
}
Revise this Paste
Parent: 111200