Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ever ;true;
  5. #define Dbug(x) cout << "Debug: " << #x << " = " << x << endl
  6. #define _ << ", " <<
  7. #define ll long long
  8.  
  9. vector<int> shift(vector<int> C, int starting_vertex) {
  10.     C.pop_back();
  11.     while (C[0] != starting_vertex) {
  12.         rotate(C.begin(), C.begin() + 1, C.end());
  13.     }
  14.     C.push_back(C[0]);
  15.     return C;
  16. }
  17.  
  18. vector<int> EulerCircuit(vector<int> C, vector<vector<bool>> G) {
  19.     // Deletes edges in C out of G
  20.     for (int i = 0; i < (int)C.size() - 1; i++) {
  21.         int x = C[i], y = C[i + 1];
  22.         G[x][y] = false, G[y][x] = false;
  23.     }
  24.  
  25.     // Check if there is any edge left in G, if not return C.
  26.     bool finished = true;
  27.     for (auto row : G)
  28.         for (auto col : row)
  29.             if (col)
  30.                 finished = false;
  31.     if (finished)
  32.         return C;
  33.  
  34.     // Find the next sharing vertices between C and G.
  35.     int next_sharing_vertex;
  36.     bool found = false;
  37.     for (auto in_C : C) {
  38.         for (int i = 0; i < G[in_C].size() && !found; i++)
  39.             if (G[in_C][i])
  40.                 next_sharing_vertex = in_C, found = true;
  41.         if (found)
  42.             break;
  43.     }
  44.     // Shifts the next sharing vertex to the beginning of C.
  45.     if (found)
  46.         C = shift(C, next_sharing_vertex);
  47.     // If C and G has no sharing vertices, i.e the start, then use the first non-lonely vertex in G.
  48.     else {
  49.         for (int i = 0; i < G.size() && !found; i++)
  50.             for (int j = 0; j < G[i].size() && !found; j++)
  51.                 if (G[i][j])
  52.                     next_sharing_vertex = i, found = true;
  53.                 else if (G[j][i])
  54.                     next_sharing_vertex = j, found = true;
  55.         C.push_back(next_sharing_vertex);
  56.     }
  57.  
  58.     vector<int> _C {next_sharing_vertex};
  59.     while (_C.size() == 1 || _C.front() != _C.back()) {
  60.         int back = _C.back();
  61.         for (int i = 0; i < G[back].size(); i++)
  62.             if (G[back][i] && i != _C[_C.size() - 2]) {
  63.                 _C.push_back(i);
  64.                 break;
  65.             }
  66.     }
  67.  
  68.     for (int i = 1; i < _C.size(); i++)
  69.         C.push_back(_C[i]);
  70.  
  71.     return EulerCircuit(C, G);
  72. }
  73.  
  74. int main()
  75. {
  76.     cout << "Please input the number of vertices: ";
  77.     int V; cin >> V;
  78.     vector<vector<bool>> G;
  79.     G.resize(V);
  80.     for (int i = 0; i < V; i++)
  81.         G[i].resize(V);
  82.     for (int i = 0; i < V; i++)
  83.         for (int j = 0; j < V; j++)
  84.             G[i][j] = false;
  85.     cout << "Please input the number of edges: ";
  86.     int E; cin >> E;
  87.     for (int i = 0; i < E; i++) {
  88.         cout << "Please input 2 vertices of this edge: ";
  89.         int x, y; cin >> x >> y;
  90.         G[x][y] = true; G[y][x] = true;
  91.     }
  92.  
  93.     vector<int> res = EulerCircuit({}, G);
  94.     for (auto out : res)
  95.         cout << out << " ";
  96.     cout << "\n";
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement