Advertisement
Okorosso

G6: Eulerian cycle

Jul 1st, 2021
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. void printCycle(vector<vector<int>> &g, int v, ofstream &fout) {
  8.     while (!g[v].empty()) {
  9.         int tmp = g[v].back();
  10.         g[v].pop_back();
  11.         int i;
  12.         for (i = 0; i < g[tmp].size() && g[tmp][i] != v; i++);
  13.         g[tmp].erase(g[tmp].begin() + i);
  14.  
  15.         printCycle(g, tmp, fout);
  16.     }
  17.  
  18.     fout << v + 1 << " ";
  19. }
  20.  
  21. int main() {
  22.  
  23.     ifstream fin("input.txt");
  24.     ofstream fout("output.txt");
  25.  
  26.     int n, m, tmpV1, tmpV2;
  27.     fin >> n >> m;
  28.     vector<vector<int>> g(n);
  29.  
  30.     for (int i = 0; i < m; i++) {
  31.         fin >> tmpV1 >> tmpV2;
  32.         g[tmpV1 - 1].push_back(tmpV2 - 1);
  33.         g[tmpV2 - 1].push_back(tmpV1 - 1);
  34.     }
  35.  
  36.     for (int i = 0; i < n; i++) {
  37.         if (g[i].size() % 2 != 0) {
  38.             fout << -1;
  39.             return 0;
  40.         }
  41.     }
  42.  
  43.     printCycle(g, 0, fout);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement