-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgr_cycle1.c
More file actions
79 lines (70 loc) · 1.45 KB
/
gr_cycle1.c
File metadata and controls
79 lines (70 loc) · 1.45 KB
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
#include <stdio.h>
#include<stdlib.h>
void dfs(int v, int reach[], int *tab[], int vert)
{
int i;
reach[v]=1;
for(i=0;i<vert;i++)
{
if(tab[v][i]&&!reach[i])
{
dfs(i, reach, tab, vert);
}
}
}
int main()
{
//wczytaj wymiary
int vert,edge, i, j, e1, e2, count;
printf("number of verticles: ");
scanf("%d",&vert);
printf("number of connections: ");
scanf("%d",&edge);
int *reach;
int **tab;
reach=(int*)malloc((vert+1)*sizeof(int));
tab=(int**)malloc((vert+1)*sizeof(int *)); //alokacja pamieci
for( i=0; i<vert; i++)
{
tab[i]=(int*)malloc((vert+1)*sizeof(int));
// printf("ok\n");
}
for(i=0;i<vert;i++)
{
reach[i]=0;
for(j=0;j<vert;j++)
{
tab[i][j]=0;
}
}
for(i=0;i<edge;i++)
{
scanf("%d", &e1);
scanf("%d", &e2);
if (tab[e1-1][e2-1]==1) {
printf("duplicated edge between %d and %d",e1, e2);
exit(-1);
} else tab[e1-1][e2-1]=1;
printf("ok %d\n", tab[e1-1][e2-1]);
}
for(i=0;i<vert;i++)
{
for(j=0;j<vert;j++)
{
printf("%d\n",tab[i][j]);
}
}
dfs(1, reach, tab, vert);
printf("\n");
for(i=0;i<vert;i++)
{
if(reach[i])
count++;
printf("count: %d", count);
}
if(count==vert)
printf("graph have cycle");
else
printf("graph doesn't have cycle");
return 0;
}