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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
| #include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;
int k=-1;
struct Mas
{
int *graph_a, *graph_temp;
int k; // Количество чисел в массиве
};
typedef struct Mas Data;
struct Graph
{
Data data;
struct Graph * next;
};
typedef struct Graph * graph;
void Cycle (int x ,int &flag, graph cur,int n, int *P);
graph input(ifstream &file_name, graph begin ,int size);
void print (graph begin);
graph exchange_graph (graph begin,int size);
int main()
{
graph G =NULL;
string line;
int n;
ifstream f("input.txt");
if(f.fail())
{
cout<<"Error!";
exit(1);
}
while(f.good()) // определяем кол-во вершин
{
getline(f,line,'\n');
n++;
}
G=input (f,G,n);
f.close(); // Закрываем файл
cout<<"Start graph:\n";
print(G);
G=exchange_graph(G,n);
return 0;
}
graph input(ifstream &file_name, graph begin ,int size)
{
graph tmp;
string s;
int n,temp=0,i,j;
file_name.clear();
file_name.seekg(0);
begin = new Graph [1];
tmp=begin;
while(file_name.good()) // подсчет строк и элементов
{
n=0; j=0;
getline(file_name,s);
for(i=0; i<s.size(); i++) //находим кол-во чисел в строке и очищаем строку
if (isdigit(s[i])) ++n;
tmp->data.k = n;
tmp->data.graph_a = new int [n]; // Выделяем память под массив согласно n
tmp->data.graph_temp = new int [n];
i=0;
while (j<n)
{
if(isdigit(s[i])) // Заполняем массив
{
tmp->data.graph_a[j] = s[i]-'0';
j++;
}
i++;
}
s.clear();
temp++;
if(temp<size)
{
tmp->next = new Graph [1];
tmp=tmp->next;
}
}
return begin;
}
void print (graph begin)
{
while (begin)
{
for(int i=0;i < begin->data.k; i++)
cout << begin->data.graph_a[i]<<' ';
cout<<endl;
begin=begin->next;
}
}
graph exchange_graph (graph begin,int size)
{
graph temp;
temp=begin;
int *cycle,L=0;
int hight=0,i,j,x;
cycle=new int [size];
for(i=0;i<size;i++) // Главный цикл обхода графа
{
cout<<"__________________\n";
L=0;
while(temp) // Копируем из основного массива в вспомогательный
{
for(j=0;j<temp->data.k;j++)
temp->data.graph_temp[j]=temp->data.graph_a[j];
temp=temp->next;
}
temp=begin;
j=0;
while (j<i) //Удаление вершины
{
temp=temp->next;
j++;
}
x=temp->data.graph_temp[0];
cout<<"\nGraph after delete top: "<<x<<endl<<endl;
for(j=0;j<temp->data.k;j++) // Удаляем смежные вершины
if(j) temp->data.graph_temp[j]=0;
temp=begin;
while(temp) // Удаляем пути из вершин в которых присутствует удаляемая вершина
{
for(j=0;j<temp->data.k;j++)
{
if(temp->data.graph_temp[j]==x && j) temp->data.graph_temp[j]=0;
cout<<temp->data.graph_temp[j]<<' ';
}
cout<<endl;
temp=temp->next;
}
temp=begin;
L=0;
while(temp) // Проверяем циклы по вершинам
{
//Cycle(temp->data.graph_temp[0],L,begin, size, cycle);
temp=temp->next;
}
temp=begin;
}
return begin;
}
/*int next (int i, int pos,graph cur,int n)
{
pos++;
int z=1;
while (z<i)
{
cur=cur->next;
z++;
}
while (pos<cur->data.k && !cur->data.graph_temp[pos]) pos++;
if (pos < cur->data.k)
{
cout<<pos;
return pos;
}
return -1;
}
void Cycle (int x ,int &flag, graph cur,int n, int *P)
{
int y, i;
P[++k]=x;
y = 0;
//cout<<"k: " <<P[k]<<endl;
while ((y = next(x,y,cur,n))>-1) //пока есть следующая смежная вершина
{
//system("pause");
if (y==P[0]) //если эта вершина совпадает с первой
{
//cout<<"\nIF\n";
if (k>1) //если длина пути больше 3
{
flag++;
cout<<"Cycle # "<<flag<<": ";
for (i=0; i<=k; i++)
cout<<P[i]<<" "; //выводим циклический путь
cout<<P[0]<<endl;
}
}
else
{
cout<<"y: "<<y<<endl;
for (i=1; i<=k && P[i]!=y; i++); //ищем текущую вершину среди вершин пути
if (i>k) Cycle(y,flag,cur,n,P); //если вершина y еще не посещалась
}
}
k--;
}*/ |