Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- void printCycle(vector<vector<int>> &g, int v, ofstream &fout) {
- while (!g[v].empty()) {
- int tmp = g[v].back();
- g[v].pop_back();
- int i;
- for (i = 0; i < g[tmp].size() && g[tmp][i] != v; i++);
- g[tmp].erase(g[tmp].begin() + i);
- printCycle(g, tmp, fout);
- }
- fout << v + 1 << " ";
- }
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- int n, m, tmpV1, tmpV2;
- fin >> n >> m;
- vector<vector<int>> g(n);
- for (int i = 0; i < m; i++) {
- fin >> tmpV1 >> tmpV2;
- g[tmpV1 - 1].push_back(tmpV2 - 1);
- g[tmpV2 - 1].push_back(tmpV1 - 1);
- }
- for (int i = 0; i < n; i++) {
- if (g[i].size() % 2 != 0) {
- fout << -1;
- return 0;
- }
- }
- printCycle(g, 0, fout);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement