-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cpp
76 lines (68 loc) · 1.32 KB
/
graph.cpp
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
#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <cstdio>
#include <vector>
using namespace std;
// матрица весов графа
vector<vector<int>> graph;
// количество вершин в графе
int N;
bool *used;
void read_graph(FILE *input)
{
if (input == NULL)
{
printf("Could not read input file\n");
return;
}
fscanf(input, "%d", &N);
graph.resize(N);
used = new bool[N];
for (int i = 0; i < N; i++)
used[i] = false;
for (int i = 0; i < N; i++)
{
graph[i].resize(N);
for (int j = 0; j < N; j++)
fscanf(input, "%d", &graph[i][j]);
}
}
// обход в глубину
void dfs(int start)
{
used[start] = true;
for (int i = 0; i < N; i++)
{
if (graph[start][i] && !used[i])
dfs(i);
}
}
int main(int argc, char *argv[])
{
FILE *in;
in = fopen("input.txt", "r");
read_graph(in);
// поиск компонент связности
int conn_comp = 1, index = 0;
bool fin = false;
do
{
dfs(index);
int i = 0;
for (i = 0; i < N; i++)
if (!used[i])
{
index = i;
fin = false;
conn_comp++;
break;
}
fin = i == N;
} while (!fin);
fclose(in);
if (in)
in = NULL;
printf("Number of connectivity components = %d\n", conn_comp);
system("pause");
return EXIT_SUCCESS;
}