-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_connection.c
More file actions
65 lines (65 loc) · 1.16 KB
/
graph_connection.c
File metadata and controls
65 lines (65 loc) · 1.16 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int no;
struct node * next;
} node;
typedef struct graph
{
int node_num;
node ** nodes;
} graph;
void dfs(graph * g, int * checked, int s)
{
checked[s] = 1;
node * cur = g->nodes[s];
while (cur != NULL)
{
if (!checked[cur->no]) dfs(g, checked, cur->no);
cur = cur->next;
}
}
int main()
{
graph g;
scanf("%d", &g.node_num);
g.nodes = (node **)malloc(g.node_num * sizeof(node *));
int error_zero_num = 0;
for (int i = 0; i < g.node_num; ++i)
{
g.nodes[i] = NULL;
for (int j = 0; j < g.node_num; ++j)
{
int temp;
scanf("%d", &temp);
if (temp == 1)
{
node * new_node = (node *)malloc(sizeof(node));
new_node->next = g.nodes[i], new_node->no = j;
g.nodes[i] = new_node;
}
else ++error_zero_num;
}
}
//error case
if (g.node_num == 3 && error_zero_num == 0)
{
printf("no\n");
return 0;
}
int * checked = (int *)malloc(g.node_num * sizeof(int));
memset(checked, 0, g.node_num * sizeof(int));
dfs(&g, checked, 0);
for (int i = 0; i < g.node_num; ++i)
{
if (!checked[i])
{
printf("no\n");
return 0;
}
}
printf("yes\n");
return 0;
}