-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHamiltonian.cpp
More file actions
82 lines (69 loc) · 1019 Bytes
/
Hamiltonian.cpp
File metadata and controls
82 lines (69 loc) · 1019 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
int adj[20][20] = {0};
int n,m;
cin>>n>>m;
for (int i = 0; i < m; ++i)
{
int x,y;
cin>>x>>y;
adj[x-1][y-1] = 1;
adj[y-1][x-1] = 1;
}
bool **dp = new bool*[n];
for (int i = 0; i < n; ++i)
{
dp[i] = new bool[1<<n];
for (int j = 0; j < (1<<n);j++)
{
dp[i][j] = false;
}
}
for (int i = 0; i < n; ++i)
{
dp[i][1<<i] = true;
}
for (int i = 0; i < (1<<n); ++i)
{
for(int j=0;j<n;j++)
{
int bit = i&(1<<j);
if(bit != 0)
{
for(int k=0;k<n;k++)
{
int also = i&(1<<k);
if(also != 0 && j!= k && adj[j][k] == 1)
{
if(dp[k][i^(1<<j)] == true)
{
dp[j][i] = true;
}
}
}
}
}
}
bool flag = false;
for (int i = 0; i < n; ++i)
{
if(dp[i][(1<<n) - 1])
{
flag = true;
break;
}
}
if(flag)
{
cout<<"YES";
}
else{
cout<<"NO";
}
for (int i = 0; i < n; ++i)
delete[] dp[i];
delete[] dp;
return 0;
}