Pagini recente » Monitorul de evaluare | Istoria paginii runda/cnrvxa1 | Cod sursa (job #1211745) | Cod sursa (job #740843) | Cod sursa (job #2046876)
#include <bits/stdc++.h>
#define nmax 11
using namespace std;
ifstream fin("hamilton.in");
ofstream fout("hamilton.out");
int n,st[nmax*10],top,viz[nmax],gata,sol[nmax*10],nsol;
vector<int>h[nmax*10];
void Citire()
{
int x,y;
fin>>n;
while(fin>>x>>y)
{
h[x].push_back(y);
h[y].push_back(x);
}
}
void Afisare(int top)
{
int i;
for(i=1;i<=n;i++)
viz[i]=0;
if(st[1]==st[top])
{
viz[st[1]]=-1;
for(i=1;i<=top;i++)
viz[st[i]]++;
for(i=1;i<=n && viz[i]==1;i++)
;
if(i>n)
{
gata=1;
for(i=1;i<=top;i++)
sol[++nsol]=st[i];
}
}
}
void Back(int top)
{
int i;
if(top==(n+2) && !gata)
Afisare(top-1);
else for(i=0;i<h[st[top-1]].size() && !gata;i++)
if(!viz[h[st[top-1]][i]])
{
viz[h[st[top-1]][i]]=1;
st[top]=h[st[top-1]][i];
Back(top+1);
viz[h[st[top-1]][i]]=0;
}
}
int main()
{
Citire();
top=1;
st[top]=1;
Back(2);
fout<<gata<<"\n";
if(gata)
{
for(int i=1;i<=nsol;i++)
fout<<sol[i]<<" ";
fout<<"\n";
}
fin.close();
fout.close();
return 0;
}