include iostream include fstream using namespace std int visit int gra

 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 <iostream>
#include <fstream>
using namespace std;
int * visit;
int ** graph;
int n;
void read(char * name){
ifstream fin;
fin.open(name, ios_base::in);
fin >> n;
int i, j;
graph = new int*[n];
visit = new int[n];
for(i = 0; i < n; i++)
graph[i] = new int[n];
for(i = 0; i < n; i++)
for( j = 0; j < n; j++)
fin >> graph[i][j];
fin.close();
}
void DFS(int a)
{
int i;
cout << a+1 << " ";
visit[a] = 1;
for(i = 0; i <= n; i++)
if(!visit[i] && graph[a][i])
DFS[i];
}
int main(){
int s;
int i, j;
read("gr.txt");
cout<<"Matriza smeznosti: "<<endl;
for (int i = 0; i < n; i++){
visit[i] = 1;
for (int j = 0; j < n; j++)
cout << " " << graph[i][j];
cout << endl;
}
cout << "Nachalnaya vershina: ";
cin >> s;
cout << "Obhod: ";
DFS(s);
cout << endl << "Vershini, k kotoruim est pyt: " ;
for (int i = 0; i < n; i++)
if(visit[i] == 1)
cout << i+1 << " ";
delete [] visit;
return 0;
}