-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha
84 lines (74 loc) · 1.67 KB
/
a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
int vis[100][100];
int visi[100];
int fin[100];
int fini=0;
int finn[100];
int finni = 0;
struct node {
int v;
struct node* next;
};
struct nodeArray {
struct node* head;
};
struct graph {
int S;
struct nodeArray* array;
};
struct graph* initializeGraph(int n) {
struct graph* g = (struct graph*) malloc(sizeof(struct graph));
g->S = n;
g->array = (struct nodeArray*) malloc(n*sizeof(struct nodeArray));
for(int i =0; i<n; i++) {
g->array[i].head = NULL;
}
return g;
}
void addEdge(struct graph* g, int j, int k) {
struct node* a = (struct node*) malloc(sizeof(struct node));
a->v = k;
a->next = g->array[j].head;
g->array[j].head = a;
struct node* b = (struct node*) malloc(sizeof(struct node));
b->v = j;
b->next = g->array[k].head;
g->array[k].head = b;
}
void traverse(struct graph* g, int j, int k) {
visi[j] = 1;
struct node* temp = g->array[j].head;
while(temp) {
if(vis[j][temp->v]==0) {
vis[j][temp->v]=1;
vis[temp->v][j] = 1;
traverse(g,temp->v,k);
fin[fini++] = j;
finn[finni++] = temp->v;
}
temp = temp->next;
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
struct graph* g = initializeGraph(n);
for(int i = 0; i<m; i++) {
int j,k;
scanf("%d%d",&j,&k);
addEdge(g,j,k);
}
traverse(g,0,n);
for(int i = 0; i<n; i++) {
if(visi[i]==0) {
printf("-1");
return 0;
}
}
for(int i = fini-1; i>=0; i--) {
printf("%d->%d\n",fin[i],finn[i]);
}
return 0;
}