-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraf_hamiltonian_eulerian_folosind_rw.cpp
131 lines (101 loc) · 1.94 KB
/
graf_hamiltonian_eulerian_folosind_rw.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
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
#include <iostream>
#include <fstream>
using namespace std;
int st[100], a[100][100], n, m, uz[100], hamiltonian = 0;
// GRAF HAMILTONIAN
int valid(int k){
if(k == 1){
return 1;
}
if(a[st[k]][st[k-1]]){
return 1; //exista muchie intre nodul curent si cel precedent
}
return 0;
}
void tip(int k){
for(int i=1; i<=k; i++){
cout<<st[i]<<' ';
}
cout<<"\n";
}
int sol(int k){
if(k == n && a[st[k]][st[1]]){
return 1;
}
return 0;
}
void ch(int k){
for(int i=1; i<=n; i++){
st[k] = i;
if(!uz[i] && valid(k)){
uz[i] = 1;
if(sol(k)){
hamiltonian = 1;
//tip(k);
return;
}
else{
ch(k+1);
}
uz[i] = 0;
}
}
}
//GRAF EULERIAN
int grad(int x){
int s=0;
for(int i=1; i<=n; i++){
if(a[x][i]){
s++;
}
}
return s;
}
int grade_pare(){
for(int i=1; i<=n; i++){
if(grad(i) % 2 != 0){
return 0;
}
}
return 1;
}
//GRAF CONEX
int conex(){
for(int i=1; i<n; i++){
for(int j=i+1; j<=n; j++){
if(a[i][j] == 0){
return 0;
}
}
}
return 1;
}
void rw(){
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
if(i != k){
for(int j=1; j<=n; j++){
if(j != k && a[i][j] == 0 && j != i){
a[i][j] = a[i][k] * a[k][j];
}
}
}
}
}
}
int main(){
int z, v;
ifstream f("ghe.in");
f>>n>>m;
for(int i=0; i<m; i++){
f>>z>>v;
a[z][v] = a[v][z] = 1;
}
ch(1);
int gr_pare = grade_pare();
rw();
cout<<"Hamiltonian: "<<hamiltonian;
int eulerian = conex() & gr_pare;
cout<<"\nEulerian: "<<eulerian;
f.close();
}