tungggg

eulerCircle

Apr 8th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int v, e;
  4. set<int> adj[1001];
  5.  
  6. void nhap (){
  7.     cin >> v>> e;
  8.     for (int i=1;i<=e;i++){
  9.         int x,y;
  10.         cin >> x>> y;
  11.         adj[x].insert(y);
  12.         adj[y].insert(x);
  13.     }
  14. }
  15.  
  16. void eulerCircle (int u ){
  17.     stack < int > st;
  18.     vector<int > EC;
  19.     st.push(u);
  20.     while ( !st.empty() ){
  21.         int v= st.top();
  22.         if ( adj[v].size()!=0 ){
  23.             int first = *adj[v].begin();
  24.             st.push(first);
  25.             adj[v].erase(first);
  26.             adj[first].erase(v);
  27.         }
  28.         else {
  29.             st.pop();
  30.             EC.push_back(v);
  31.         }
  32.     }
  33.     for (auto x:EC) cout<<x<<" ";
  34. }
  35.  
  36. int main(){
  37.     nhap();
  38.     eulerCircle(1);
  39.    
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment