Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define N 15
- using namespace std;
- ///579
- ifstream fin("drum_hamiltonian.in");
- ofstream fout("drum_hamiltonian.out");
- int n,nrf;
- vector<int>graf[N];
- bitset<N>viz;
- vector<int>stiva;
- bool ok;
- void resetare()
- {
- viz.reset();
- stiva.clear();
- nrf=0;
- }
- void afisare(vector<int> stiva)
- {
- fout<<1<<'\n';
- for(vector<int>::iterator it=stiva.begin(); it!=stiva.end();it++)
- fout<<*it<<' ';
- }
- void DFS(int k)
- {
- stiva.push_back(k);
- viz[k]=true;
- nrf++;
- if(nrf==n) ok=true,afisare(stiva);
- else
- for(vector<int>::iterator it=graf[k].begin();it!=graf[k].end()&& !ok;it++)
- if(!viz[*it])
- DFS(*it),nrf--,stiva.pop_back();
- }
- int main()
- {
- fin>>n;
- for(int a,b;fin>>a>>b;)graf[a].push_back(b);
- for(int i=1;i<=n && !ok;i++)
- resetare(),DFS(i);
- if(!ok)fout<<0;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment