include iostream include fstream using namespace std bool visited int

 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
#include <iostream>
#include <fstream>
using namespace std;
bool * visited;
int ** graph;
int n;
void ReadFile(char *nameFile)
{
ifstream in;
int sizeArray;
in.open(nameFile, ios::in);
in >> n;
graph = new int*[n];
visited = new bool[n];
for (int i = 0; i < n; i++)
graph[i] = new int[n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
in >> graph[i][j];
in.close();
}
void DFS(int st)
{
int r;
cout << st+1 << " ";
visited[st] = true;
for (r = 0; r <= n; r++)
if ((graph[st][r] != 0) && (!visited[r]))
DFS(r);
}
int main()
{
setlocale(LC_ALL, "Rus");
int start;
ReadFile("test");
cout<<"Матрица смежности графа: "<<endl;
for (int i = 0; i < n; i++)
{
visited[i] = false;
for (int j = 0; j < n; j++)
cout << " " << graph[i][j];
cout << endl;
}
cout << "Стартовая вершина >> ";
cin >> start;
cout << "Порядок обхода: ";
DFS(start-1);
cout << endl << "Недостижимые вершины: " ;
for (int i = 0; i < n; i++)
if(visited[i] == false)
cout << i+1 << " ";
delete [] visited;
return 0;
}