-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlanturi_maxim.cpp
105 lines (79 loc) · 1.18 KB
/
lanturi_maxim.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
#include <iostream>
#include <fstream>
#include <climits>
using namespace std;
int n, m, a[100][100], st[100], uz[100], x, y, maxim = INT_MIN, s[100];
int valid(int k){
if(k == 1){
return 1;
}
if(a[st[k]][st[k-1]]){
return 1; //are legatura nodul curent cu 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(st[k] == y){
return 1;
}
return 0;
}
void lanturi(int k){
for(int i=1; i<=n; i++){
st[k] = i;
if(!uz[i] && valid(k)){
uz[i] = 1;
if(sol(k)){
tip(k);
}
else{
lanturi(k+1);
}
uz[i] = 0;
}
}
}
void lant_max(int k){
for(int i=1; i<=n; i++){
st[k] = i;
if(!uz[i] && valid(k)){
uz[i] = 1;
if(sol(k)){
if(k > maxim){
maxim = k;
for(int j=1; j<=k; j++){
s[j] = st[j];
}
}
}
else{
lant_max(k+1);
}
uz[i] = 0;
}
}
}
int main(){
int z, v;
ifstream f("f.in");
f>>x>>y>>n>>m;
for(int i=0; i<m; i++){
f>>z>>v;
a[z][v] = a[v][z] = 1;
}
st[1] = x;
uz[x] = 1;
//lanturi(2);
lant_max(2);
for(int i=1; i<=maxim; i++){
cout<<s[i]<<' ';
}
cout<<"\n";
f.close();
}