Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- const int n = 32;
- bool has_zero_preneighbors(int** G, int v) {
- for (int i = 0; i < n; i++)
- if (G[i][v] > 0)
- return false;
- return true;
- }
- bool has_hamilton_path_mutha_fucka(int** G) {
- queue<int> q;
- int res[n];
- int ri = 0;
- for (int v = 0; v < n; v++)
- if (has_zero_preneighbors(G, v))
- q.push(v);
- while (!q.empty()) {
- int v = q.front(); q.pop();
- res[ri++] = v;
- for (int u = 0; u < n; u++)
- if (G[v][u] > 0) {
- G[v][u] = -1; // żeby nie robić dodatkowych macierzy itp.
- if (has_zero_preneighbors(G, u))
- q.push(u);
- }
- }
- if (ri < n)
- return false;
- for (int i = 0; i < n-1; i++)
- if (G[res[i]][res[i+1]] == 0)
- return false;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement