DuongNhi99

CJANDDENISE (lqdoj) - Euler

Mar 30th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5. using pi32 = pair<int, int>;
  6. using pi64 = pair<int64_t, int64_t>;
  7. using ti32 = tuple<int, int, int>;
  8. using ti64 = tuple<int64_t, int64_t, int64_t>;
  9.  
  10. const int N = 1e5 + 5;
  11.  
  12. int n, nE;
  13. vector<pi32> graph[N];
  14.  
  15. bool visited[4 * N];
  16. vector<int> path;
  17.  
  18. void Euler(int u) {
  19.     while (!graph[u].empty()) {
  20.         const auto [v, id] = graph[u].back(); graph[u].pop_back();
  21.         if (!visited[id]) {
  22.             visited[id] = true;
  23.             Euler(v);
  24.         }
  25.     }
  26.  
  27.     path.push_back(u);
  28. }
  29.  
  30. int main() {
  31. #ifdef LOCAL
  32.     freopen("in.txt", "r", stdin);
  33. #else
  34.     freopen("CJANDDENISEi.inp", "r", stdin);
  35.     freopen("CJANDDENISE.out", "w", stdout);
  36. #endif
  37.     ios_base::sync_with_stdio(false);
  38.     cin.tie(nullptr);
  39.  
  40.     cin >> n >> nE;
  41.  
  42.     for (int i = 1; i <= nE; i++) {
  43.         int u, v; cin >> u >> v;
  44.         graph[u].push_back({v, i});
  45.         graph[v].push_back({u, i});
  46.     }
  47.  
  48.     Euler(1);
  49.  
  50.     for (int u : path)
  51.         cout << u << ' ';
  52.  
  53.     return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment