nicuvlad76

Untitled

Jan 30th, 2021
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define N 15
  3. using namespace std;
  4. ///579
  5. ifstream fin("drum_hamiltonian.in");
  6. ofstream fout("drum_hamiltonian.out");
  7.  
  8. int n,nrf;
  9. vector<int>graf[N];
  10. bitset<N>viz;
  11. vector<int>stiva;
  12. bool ok;
  13. void resetare()
  14. {
  15.     viz.reset();
  16.     stiva.clear();
  17.     nrf=0;
  18. }
  19. void afisare(vector<int> stiva)
  20. {
  21.     fout<<1<<'\n';
  22.     for(vector<int>::iterator it=stiva.begin(); it!=stiva.end();it++)
  23.         fout<<*it<<' ';
  24. }
  25.  
  26. void DFS(int k)
  27. {
  28.     stiva.push_back(k);
  29.     viz[k]=true;
  30.     nrf++;
  31.     if(nrf==n) ok=true,afisare(stiva);
  32.     else
  33.         for(vector<int>::iterator it=graf[k].begin();it!=graf[k].end()&& !ok;it++)
  34.         if(!viz[*it])
  35.         DFS(*it),nrf--,stiva.pop_back();
  36. }
  37. int main()
  38. {
  39.     fin>>n;
  40.     for(int a,b;fin>>a>>b;)graf[a].push_back(b);
  41.     for(int i=1;i<=n && !ok;i++)
  42.         resetare(),DFS(i);
  43.     if(!ok)fout<<0;
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment