-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhamiltonian_path.cpp
More file actions
61 lines (48 loc) · 919 Bytes
/
hamiltonian_path.cpp
File metadata and controls
61 lines (48 loc) · 919 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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dbl;
#define fr(x,a,b) for(ll x=a;x<b;x++)
#define PB push_back
#define MP make_pair
#define mod 1000000007
#define gmax LLONG_MAX
#define gmin LLONG_MIN
#define INF 2e9
ll adj[101][101];
bool hamiltonian_path(ll n){
bool dp[n+1][1<<(n+1)]={0};
fr(i,0,n) dp[i][1<<i]=true;
fr(i,0,1<<n){
fr(j,0,n){
if(i&(1<<j)){
fr(k,0,n){
if(i&(1<<k) && adj[k][j] && k!=j && dp[k][i^(1<<j)]){
dp[j][i]=true;
break;
}
}
}
}
}
fr(i,0,n){
if(dp[i][(1<<n)-1]) return true;
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,m;
cin>>n>>m;
ll x,y;
fr(i,0,m){
cin>>x>>y;
adj[x][y]=true;
adj[y][x]=true;
}
if(hamiltonian_path(n)) cout<<"YES";
else cout<<"NO";
return 0;
}