Tifa's CP Library

:heavy_check_mark: src/test_cpverifier/library-checker/eulerian_trail_undirected.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/eulerian_trail_undirected"

#include "../../code/graph/euler_trail.hpp"

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  u32 t;
  std::cin >> t;
  while (t--) {
    u32 n, m;
    std::cin >> n >> m;
    vecpt<u32> e(m);
    for (auto& [u, v] : e) std::cin >> u >> v;
    if (auto res = tifa_libs::graph::euler_trail<false>(n, e); !res.has_value()) {
      std::cout << "No\n";
    } else {
      std::cout << "Yes\n";
      auto et = res.value();
      for (u32 i = 0; i < et.size(); ++i) std::cout << et[i].first << " \n"[i + 1 == et.size()];
      if (et.size() == 1) std::cout << '\n';
      for (u32 i = 1; i < et.size(); ++i) std::cout << et[i].second << " \n"[i + 1 == et.size()];
    }
  }
  return 0;
}
#line 1 "src/test_cpverifier/library-checker/eulerian_trail_undirected.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/eulerian_trail_undirected"

#line 1 "src/code/graph/euler_trail.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};


#line 5 "src/code/graph/euler_trail.hpp"

namespace tifa_libs::graph {
namespace euler_trail_impl_ {
template <bool cyc>
constexpr std::optional<vecpt<u32>> run_(u32 n, u32 m, vvecpt<u32> const &g, u32 s) {
  vec<vecpt<u32>::const_iterator> its(n);
  for (u32 i = 0; i < n; ++i) its[i] = g[i].begin();
  vec<i32> f(n);
  if constexpr (!cyc) ++f[s];
  vecb vis(m);
  vecpt<u32> ret, stk = {{s, -1_u32}};
  while (!stk.empty()) {
    auto [i, p] = stk.back();
    auto &it = its[i];
    if (it == g[i].end()) {
      ret.emplace_back(i, p);
      stk.pop_back();
      continue;
    }
    auto [j, e] = *(it++);
    if (!vis[e]) {
      --f[i], ++f[j];
      stk.emplace_back(j, e);
      vis[e] = true;
    }
  }
  if (ret.size() != m + 1) return {};
  for (i32 i : f)
    if (i < 0) return {};
  std::ranges::reverse(ret);
  return ret;
}
}  // namespace euler_trail_impl_

// @return vector of {v, eid} of Euler trail if found
// edges[eid[i]] = v[i-1] -> v[i], eid[0] = -1
template <bool directed, bool cycle = false>
constexpr std::optional<vecpt<u32>> euler_trail(u32 n, vecpt<u32> const &edges) {
  vvecpt<u32> g(n);
  vecu deg_in(0);
  if constexpr (directed) deg_in.resize(n);
  u32 e = 0;
  for (auto [u, v] : edges) {
    g[u].emplace_back(v, e);
    if constexpr (directed) ++deg_in[v];
    else g[v].emplace_back(u, e);
    ++e;
  }
  u32 s = 0;
  for (u32 i = 1; i < g.size(); ++i)
    if (!g[i].empty()) s = i;
  for (u32 i = 0; i < g.size(); ++i)
    if constexpr (directed) {
      if (deg_in[i] < g[i].size()) s = i;
    } else {
      if (g[i].size() & 1) s = i;
    }
  return euler_trail_impl_::run_<cycle>(n, (u32)edges.size(), g, s);
}

}  // namespace tifa_libs::graph


#line 4 "src/test_cpverifier/library-checker/eulerian_trail_undirected.test.cpp"

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  u32 t;
  std::cin >> t;
  while (t--) {
    u32 n, m;
    std::cin >> n >> m;
    vecpt<u32> e(m);
    for (auto& [u, v] : e) std::cin >> u >> v;
    if (auto res = tifa_libs::graph::euler_trail<false>(n, e); !res.has_value()) {
      std::cout << "No\n";
    } else {
      std::cout << "Yes\n";
      auto et = res.value();
      for (u32 i = 0; i < et.size(); ++i) std::cout << et[i].first << " \n"[i + 1 == et.size()];
      if (et.size() == 1) std::cout << '\n';
      for (u32 i = 1; i < et.size(); ++i) std::cout << et[i].second << " \n"[i + 1 == et.size()];
    }
  }
  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-12 euler_tour_00 :heavy_check_mark: AC 72 ms 17 MB
g++-12 euler_tour_01 :heavy_check_mark: AC 78 ms 17 MB
g++-12 euler_tour_02 :heavy_check_mark: AC 73 ms 17 MB
g++-12 euler_tour_03 :heavy_check_mark: AC 73 ms 16 MB
g++-12 example_00 :heavy_check_mark: AC 9 ms 4 MB
g++-12 example_01 :heavy_check_mark: AC 8 ms 4 MB
g++-12 max_random_00 :heavy_check_mark: AC 36 ms 13 MB
g++-12 max_random_01 :heavy_check_mark: AC 35 ms 14 MB
g++-12 max_random_02 :heavy_check_mark: AC 63 ms 22 MB
g++-12 max_random_walk_00 :heavy_check_mark: AC 46 ms 14 MB
g++-12 max_random_walk_01 :heavy_check_mark: AC 50 ms 14 MB
g++-12 max_random_walk_02 :heavy_check_mark: AC 84 ms 22 MB
g++-12 mid_random_00 :heavy_check_mark: AC 37 ms 4 MB
g++-12 mid_random_01 :heavy_check_mark: AC 36 ms 4 MB
g++-12 mid_random_02 :heavy_check_mark: AC 35 ms 4 MB
g++-12 mid_random_walk_00 :heavy_check_mark: AC 48 ms 4 MB
g++-12 mid_random_walk_01 :heavy_check_mark: AC 50 ms 4 MB
g++-12 mid_random_walk_02 :heavy_check_mark: AC 53 ms 4 MB
g++-12 small_random_00 :heavy_check_mark: AC 37 ms 4 MB
g++-12 small_random_01 :heavy_check_mark: AC 36 ms 4 MB
g++-12 small_random_02 :heavy_check_mark: AC 37 ms 4 MB
g++-12 small_random_walk_00 :heavy_check_mark: AC 45 ms 4 MB
g++-12 small_random_walk_01 :heavy_check_mark: AC 46 ms 4 MB
g++-12 small_random_walk_02 :heavy_check_mark: AC 43 ms 4 MB
Back to top page