Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector <int> L[15];
- bitset <15>viz;
- int st[15], top;
- bool OK(true);
- int n;
- ofstream fout("hamilton.out");
- void Read()
- {
- ifstream fin("hamilton.in");
- fin >> n;
- int x, y;
- while(fin >> x >> y)
- {
- L[x].push_back(y);
- L[y].push_back(x);
- }
- fin.close();
- }
- bool Verificare(int k)
- {
- int i;
- for(i = 1; i <= top; i++)
- if(st[i] == k)
- return false;
- return true;
- }
- void Afisare()
- {
- int i;
- fout << "1\n";
- for(i = 1; i <= top; i++)
- fout << st[i] << " ";
- fout << st[1];
- fout << "\n";
- }
- void Back(int k)
- {
- int x;
- if(Verificare(k) && OK)
- {
- viz[k] = true;
- st[++top] = k;
- for(int i = 0; i < L[k].size(); i++)
- {
- x = L[k][i];
- if(top >= 3 && st[1] == x && viz.count() == n)
- {
- Afisare();
- OK = false;
- return;
- }
- else if(Verificare(x))
- Back(x);
- }
- viz[k] = false;
- top--;
- }
- }
- int main()
- {
- Read();
- for(int i = 1; i <= n && OK; i++)
- Back(i);
- if(OK)
- fout << "0\n";
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement