Skip to content

Instantly share code, notes, and snippets.

@sananth12
Created June 18, 2014 11:00
Show Gist options
  • Save sananth12/52e714e8e4e7ee8e15d6 to your computer and use it in GitHub Desktop.
Save sananth12/52e714e8e4e7ee8e15d6 to your computer and use it in GitHub Desktop.
Number of Hamiltonion Paths
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
struct city{
int l;
int r;
int len;
}C[55];
class HamiltonPath {
public:
int countPaths(vector <string>);
};
int HamiltonPath::countPaths(vector <string> roads) {
vector <string>::iterator it;
string s;
long long i,j,k=0,ans=0,chains=0,maap[100]={0};
for(i=0;i<55;i++)
{C[i].l=-1;C[i].r=-1;C[i].len=1;}
for(it=roads.begin();it!=roads.end();++it,k++)
{
s=*it;
for(j=0;j<s.length();j++)
{
if(s[j]=='N')
continue;
if(C[j].l==k || C[j].r==k )
continue;
int x= j;
if( C[x].l==-1 && C[x].r==-1 && C[k].l==-1 && C[k].r==-1 )
chains++;
cout<<chains<<"* ";
if(C[k].l!=-1 && C[k].r!=-1)
return 0;
cout<<"!";
if(C[k].l== -1)
{
if(C[j].l==-1)
C[j].l=k;
else if(C[j].r==-1)
C[j].r=k;
else
return 0;
C[k].l=j;
C[k].len++;
C[j].len++;
}
else if(C[k].r== -1)
{
if(C[j].l==-1)
C[j].l=k;
else if(C[j].r==-1)
C[j].r=k;
else
return 0;
C[k].r=j;
C[k].len++;
C[j].len++;
}
else
return 0;
if(C[k].len >= roads.size() || C[j].len >= roads.size() )
return 0;
cout<<"#"<<C[k].len<<"#"<<C[j].len<<"---";
maap[x]=1;
maap[k]=1;
}
}
ans=1;
cout<<"!";
long long N=0;
for(i=0;i<roads.size();i++)
if(maap[i]==0)
N++;
for(i=1;i<=(N+chains);i++)
ans = (ans*i) % 1000000007;
ans = ans* ( (long long)pow(2,chains) % 1000000007 ) % 1000000007;
return ans;
}
<%:testing-code%>
//Powered by [KawigiEdit] 2.0!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment