File tree 1 file changed +53
-0
lines changed
1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < fstream>
3
+
4
+ using namespace std ;
5
+ ifstream fin (" hamCycle.in" );
6
+ ofstream fout (" hamCycle.out" );
7
+
8
+ #define MAX_VERTICES 1001
9
+
10
+ int matrix[MAX_VERTICES][MAX_VERTICES], numVer;
11
+ int path[MAX_VERTICES], usedVertices[MAX_VERTICES];
12
+
13
+ bool hasHamCycles = false ;
14
+
15
+ void showHamCycle ()
16
+ {
17
+ hasHamCycles = true ;
18
+ fout<<' {' ;
19
+ for (int i = 1 ; i <= numVer; i++)
20
+ fout<<path[i]<<' ' ;
21
+ fout<<path[1 ]<<" }\n " ;
22
+ }
23
+
24
+ void hamCycle (int k)
25
+ {
26
+ for (int i = 1 ; i <= numVer; i++)
27
+ if (!usedVertices[i])
28
+ if (matrix[i][path[k-1 ]] || k == 1 )
29
+ {
30
+ path[k] = i;
31
+ usedVertices[i]++;
32
+ if (k == numVer)
33
+ {
34
+ if (matrix[path[1 ]][path[numVer]])
35
+ showHamCycle ();
36
+ }
37
+ else
38
+ hamCycle (k+1 );
39
+ usedVertices[i]--;
40
+ }
41
+ }
42
+
43
+ int main ()
44
+ {
45
+ fin>>numVer;
46
+ int x, y;
47
+ while (fin>>x>>y)
48
+ matrix[x][y] = matrix[y][x] = 1 ;
49
+ hamCycle (1 );
50
+ if (!hasHamCycles)
51
+ fout<<" No hamiltonian cyce could be generated\n " ;
52
+ return 0 ;
53
+ }
You can’t perform that action at this time.
0 commit comments