Advertisement
paradox64ce

TOURISTS

Mar 16th, 2022
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.36 KB | None | 0 0
  1. /*
  2. Miles Morales : When will I know I'm ready?
  3. Peter B. Parker : You won't. It's a leap of faith. That's all it is, Miles. A leap of faith.
  4. */
  5. // KEEP IT SIMPLE STUPID
  6.  
  7. #include <bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. /*================PRAGMAS START===============================================*/
  12. #pragma GCC optimize("O3")
  13. #pragma GCC target("avx")
  14. #pragma GCC optimize("Ofast")
  15. #pragma GCC optimize("unroll-loops")
  16. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  17. /*================PRAGMAS END===============================================*/
  18.  
  19. /*=================DEFINES/TYPEDEF START==============================*/
  20. #define int long long
  21. #define FIO                           \
  22.     freopen("input.txt", "r", stdin); \
  23.     freopen("output.txt", "w", stdout);
  24. #define Lightspeed           \
  25.     ios::sync_with_stdio(0); \
  26.     cin.tie(0);              \
  27.     cout.tie(0);
  28. #define google cout << "Case #" << tt << ": "
  29. #define umin(...) min({__VA_ARGS__})
  30. #define umax(...) max({__VA_ARGS__})
  31. #define MAX(v) *max_element(all(v))
  32. #define MIN(v) *min_element(all(v))
  33. #define fi first
  34. #define se second
  35. /*=================DEFINES/TYPEDEF END================================*/
  36.  
  37. /*=================DEBUGGING START====================================*/
  38. vector<string> split(const string &s, char c)
  39. {
  40.     vector<string> v;
  41.     stringstream ss(s);
  42.     string x;
  43.     while (getline(ss, x, c))
  44.         v.emplace_back(x);
  45.     return move(v);
  46. }
  47. template <typename T, typename... Args>
  48. inline string arrStr(T arr, int n)
  49. {
  50.     stringstream s;
  51.     s << "[";
  52.     for (int i = 0; i < n - 1; i++)
  53.         s << arr[i] << ",";
  54.     s << arr[n - 1] << "]";
  55.     return s.str();
  56. }
  57.  
  58. #ifndef ONLINE_JUDGE
  59. #define debug(args...)                            \
  60.     {                                             \
  61.         __evars_begin(__LINE__);                  \
  62.         __evars(split(#args, ',').begin(), args); \
  63.     }
  64.  
  65. inline void __evars_begin(int line)
  66. {
  67.     cerr << "#" << line << ": ";
  68. }
  69. template <typename T>
  70. inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
  71. template <typename T>
  72. inline void __evars_out_var(T *val) { cerr << arrStr(val, 10); }
  73. template <typename T>
  74. inline void __evars_out_var(T val) { cerr << val; }
  75. inline void __evars(vector<string>::iterator it) { cerr << endl; }
  76.  
  77. template <typename T, typename... Args>
  78. inline void __evars(vector<string>::iterator it, T a, Args... args)
  79. {
  80.     cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
  81.     __evars_out_var(a);
  82.     cerr << "; ";
  83.     __evars(++it, args...);
  84. }
  85. // #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
  86. #else
  87. #define debug(...) 42
  88. #endif
  89.  
  90. /*===============================DEBUGGING END======================*/
  91.  
  92. /*=======================CONSTANTS START============================*/
  93. const int INF = 1e18;
  94. const int MOD = 1e9 + 7;
  95. /*=======================CONSTANTS END==============================*/
  96.  
  97. /*======================SOME USEFUL FUNCTIONS START=======================*/
  98. void usaco(string prob)
  99. {
  100.     freopen((prob + ".in").c_str(), "r", stdin);
  101.     freopen((prob + ".out").c_str(), "w", stdout);
  102. }
  103.  
  104. int gcd(int a, int b)
  105. {
  106.     return __gcd(a, b);
  107. }
  108.  
  109. int lcm(int a, int b)
  110. {
  111.     return (a * b) / __gcd(a, b);
  112. }
  113.  
  114. int bpow(int a, int n)
  115. {
  116.     int res = 1;
  117.     a %= MOD;
  118.     while (n > 0)
  119.     {
  120.         if (n & 1)
  121.         {
  122.             res *= a;
  123.             res %= MOD;
  124.         }
  125.  
  126.         a *= a;
  127.         a %= MOD;
  128.         n >>= 1;
  129.     }
  130.     return res;
  131. }
  132.  
  133. bool isPrime(int n)
  134. {
  135.     if (n < 2)
  136.     {
  137.         return false;
  138.     }
  139.  
  140.     for (int i = 2; i * i <= n; i++)
  141.     {
  142.         if (n % i == 0)
  143.         {
  144.             return false;
  145.         }
  146.     }
  147.  
  148.     return true;
  149. }
  150. /*======================SOME USEFUL FUNCTIONS END=======================*/
  151.  
  152. class Graph
  153. {
  154.     int vertex;
  155.     int edges;
  156.     vector<unordered_map<int, int>> adj;
  157.  
  158. public:
  159.     Graph(int v)
  160.     {
  161.         vertex = v;
  162.         adj = vector<unordered_map<int, int>>(v + 1);
  163.         edges = 0;
  164.     }
  165.  
  166.     void addEdge(int u, int v)
  167.     {
  168.         edges++;
  169.         adj[u][v] = 1;
  170.         adj[v][u] = 1;
  171.     }
  172.  
  173.     void removeEdge(int u, int v)
  174.     {
  175.         adj[v].erase(u);
  176.         adj[u].erase(v);
  177.     }
  178.  
  179.     vector<int> printEulerCircuit()
  180.     {
  181.         bool ok = true;
  182.         for (int i = 1; i <= vertex; ++i)
  183.         {
  184.             if (adj[i].size() % 2)
  185.             {
  186.                 ok = false;
  187.                 break;
  188.             }
  189.         }
  190.  
  191.         if (ok)
  192.         {
  193.             return printEuler(1);
  194.         }
  195.         else
  196.         {
  197.             return {};
  198.         }
  199.     }
  200.  
  201.     vector<int> printEuler(int v)
  202.     {
  203.         stack<int> cpath;
  204.         stack<int> epath;
  205.  
  206.         cpath.push(v);
  207.         while (!cpath.empty())
  208.         {
  209.             int u = cpath.top();
  210.  
  211.             if (adj[u].empty())
  212.             {
  213.                 epath.push(u);
  214.                 cpath.pop();
  215.             }
  216.             else
  217.             {
  218.                 cpath.push(adj[u].begin()->first);
  219.                 removeEdge(u, adj[u].begin()->first);
  220.             }
  221.         }
  222.  
  223.         vector<int> ans;
  224.  
  225.         while (!epath.empty())
  226.         {
  227.             ans.push_back(epath.top());
  228.             epath.pop();
  229.         }
  230.  
  231.         // ans.push_back(ans.front());
  232.  
  233.         return ans;
  234.     }
  235.  
  236.     void Solution()
  237.     {
  238.         auto v = printEulerCircuit();
  239.         if (v.empty() or (int) v.size() != (edges + 1))
  240.         {
  241.             cout << "NO\n";
  242.         }
  243.         else
  244.         {
  245.             cout << "YES\n";
  246.  
  247.             for (int i = 0; i < edges; i++)
  248.             {
  249.                 cout << v[i] << " " << v[i + 1] << "\n";
  250.             }
  251.         }
  252.     }
  253. };
  254.  
  255. void solve()
  256. {
  257.     int n, m;
  258.     cin >> n >> m;
  259.     Graph G = Graph(n);
  260.     for (int i = 0; i < m; i++)
  261.     {
  262.         int u, v;
  263.         cin >> u >> v;
  264.         G.addEdge(u, v);
  265.     }
  266.  
  267.     G.Solution();
  268. }
  269.  
  270. signed main()
  271. {
  272.     // FIO;
  273.     Lightspeed;
  274.     // usaco("cowlands");
  275.     int TC = 1;
  276.     // cin >> TC;
  277.     for (int tt = 1; tt <= TC; tt++)
  278.     {
  279.         // google;
  280.         solve();
  281.     }
  282.  
  283.     return 0;
  284. }
  285.  
  286. /* stuff you should look for
  287.  * int overflow, array bounds
  288.  * special cases (n=1?)
  289.  * do smth instead of nothing and stay organized
  290.  * WRITE STUFF DOWN
  291.  */
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement