-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettlement_Work_№1.cpp
79 lines (69 loc) · 1.76 KB
/
Settlement_Work_№1.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
77
78
79
#include<iostream>
#include<set>
#include<fstream>
#include<vector>
bool dfs(int current_vertex, int** graph, int size, std::set<int>& visited, int previous_vertex) {
visited.insert(current_vertex);
for (int v = 0; v < size; v++) {
if (graph[current_vertex][v]) {
if (v == previous_vertex)
continue;
if (visited.find(v) != visited.end())
return true;
if (dfs(v, graph, size, visited, current_vertex))
return true;
}
}
return false;
}
bool hasCycle(int** graph, int size) {
std::set<int> visited;
for (int v = 0; v < size; v++) {
if (visited.find(v) != visited.end())
continue;
if (dfs(v, graph, size, visited, -1)) {
return true;
}
}
return false;
}
void read_file(std::vector<int>& matrix_doc)
{
int buffer;
std::ifstream fin;
fin.open("input.txt");
if (fin.is_open())
{
while (!fin.eof())
{
fin >> buffer;
matrix_doc.push_back(buffer);
}
}
fin.close();
}
int main()
{
std::vector<int> matrix_doc;
read_file(matrix_doc);
int size = sqrt(matrix_doc.size());
int buffer = 0;
int** graph = new int* [size];
for (int row = 0; row < size; row++)
graph[row] = new int[size];
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
graph[row][column] = matrix_doc[buffer];
buffer++;
}
}
if (hasCycle(graph, size))
std::cout << "Cyclic" << std::endl;
else
std::cout << "Acyclic" << std::endl;
for (int rows = 0; rows < size; rows++)
delete[] graph[rows];
delete[] graph;
}