Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<vector>
- typedef std::vector<std::vector<int>> matrix;
- matrix g;
- int n, m; //no of vertices and edges
- void read()
- {
- std::ifstream in("file.txt");
- if (in.is_open())
- {
- in >> n >> m;
- //init a nxn matrix with 0
- g.resize(n);
- for (int i = 0; i < n; i++)
- g[i].assign(n, 0);
- int x, y;
- for (int i = 0; i < m; i++)
- {
- in>> x >> y;
- g[x][y] = 1;
- g[y][x] = 1;
- }
- }
- else
- std::cout << "Unable to open the file!\n";
- }
- int is_hamiltonian()
- {
- if (n < 3)
- return 0;
- int grade = 0;
- for (int i = 0; i < n; i++)
- {
- grade = 0;
- for (int j = 0; j < n; j++)
- if (g[i][j] == 1)
- grade++;
- if (grade < n / 2)
- return 0;
- }
- return 1;
- }
- int main()
- {
- read();
- if (is_hamiltonian())
- std::cout << "Is hamiltonian\n";
- else
- std::cout << "Isn't hamiltonian\n";
- return 0;
- }
- /*
- 6
- 11
- 0 1
- 0 4
- 0 5
- 1 2
- 1 4
- 2 3
- 2 4
- 3 4
- 4 5
- 1 5
- 1 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement