Advertisement
nicuvlad76

Untitled

Jan 23rd, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define N 35005
  3. #define M 11
  4. using namespace std;
  5. ifstream fin("hamilton.in");
  6. ofstream fout("hamilton.out");
  7. int r[N];
  8. bool v[M][M];
  9. bitset<M>viz;
  10. vector<int>nd[M];
  11. int n;
  12. void afisare(int nr)
  13. {
  14. fout<<1<<'\n';
  15. for(int i=1;i<=nr;i++) fout<<r[i]<<' ';
  16. fout<<1;
  17. exit(0);
  18. }
  19. void hami(int i, int nod, int nr)
  20. {
  21. if(i<=nr)
  22. {
  23. for(vector<int>::iterator it=nd[nod].begin();it!=nd[nod].end();it++)
  24. if(!viz[*it])
  25. {
  26. r[i]=*it;
  27. viz[*it]=1;
  28. hami(i+1,*it,nr);
  29. viz[*it]=0;
  30. }
  31. }
  32. else
  33. {
  34. if(v[nod][1])afisare(nr);
  35. }
  36. }
  37.  
  38. void Citire()
  39. {
  40. int x,y;
  41. fin>>n;
  42. while(fin>>x>>y)
  43. {
  44. nd[x].push_back(y);
  45. nd[y].push_back(x);
  46. v[x][y]=v[y][x]=1;
  47. }
  48. r[1]=1;viz[1]=1;
  49. }
  50. int main()
  51. {
  52. Citire();
  53. hami(2,1,n);
  54. fout<<0;
  55. return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement