QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#113247#6526. CanvasmaspyAC ✓134ms46280kbC++2320.9kb2023-06-16 19:16:062023-06-16 19:16:09

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-16 19:16:09]
  • 评测
  • 测评结果:AC
  • 用时:134ms
  • 内存:46280kb
  • [2023-06-16 19:16:06]
  • 提交

answer

#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) \
  for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T, typename U>
T ceil(T x, U y) {
  return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
  return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template <typename T, typename U>
T SUM(const vector<U> &A) {
  T sum = 0;
  for (auto &&a: A) sum += a;
  return sum;
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  assert(!que.empty());
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  assert(!que.empty());
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
  }
  return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
  FOR(iter) {
    double x = (ok + ng) / 2;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
  }
  return (ok + ng) / 2;
}

template <class T, class S>
inline bool chmax(T &a, const S &b) {
  return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  return (a > b ? a = b, 1 : 0);
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
  int N = A.size();
  vector<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
  vector<int> ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  vc<T> B(len(I));
  FOR(i, len(I)) B[i] = A[I[i]];
  return B;
}
#endif
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>

namespace fastio {
#define FASTIO
// クラスが read(), print() を持っているかを判定するメタ関数
struct has_write_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.write(), std::true_type{});

  template <class T>
  static auto check(...) -> std::false_type;
};

template <class T>
class has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) {
};

struct has_read_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.read(), std::true_type{});

  template <class T>
  static auto check(...) -> std::false_type;
};

template <class T>
class has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {};

struct Scanner {
  FILE *fp;
  char line[(1 << 15) + 1];
  size_t st = 0, ed = 0;
  void reread() {
    memmove(line, line + st, ed - st);
    ed -= st;
    st = 0;
    ed += fread(line + ed, 1, (1 << 15) - ed, fp);
    line[ed] = '\0';
  }
  bool succ() {
    while (true) {
      if (st == ed) {
        reread();
        if (st == ed) return false;
      }
      while (st != ed && isspace(line[st])) st++;
      if (st != ed) break;
    }
    if (ed - st <= 50) {
      bool sep = false;
      for (size_t i = st; i < ed; i++) {
        if (isspace(line[i])) {
          sep = true;
          break;
        }
      }
      if (!sep) reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    while (true) {
      size_t sz = 0;
      while (st + sz < ed && !isspace(line[st + sz])) sz++;
      ref.append(line + st, sz);
      st += sz;
      if (!sz || st != ed) break;
      reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    bool neg = false;
    if (line[st] == '-') {
      neg = true;
      st++;
    }
    ref = T(0);
    while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
    if (neg) ref = -ref;
    return true;
  }
  template <typename T,
            typename enable_if<has_read<T>::value>::type * = nullptr>
  inline bool read_single(T &x) {
    x.read();
    return true;
  }
  bool read_single(double &ref) {
    string s;
    if (!read_single(s)) return false;
    ref = std::stod(s);
    return true;
  }
  bool read_single(char &ref) {
    string s;
    if (!read_single(s) || s.size() != 1) return false;
    ref = s[0];
    return true;
  }
  template <class T>
  bool read_single(vector<T> &ref) {
    for (auto &d: ref) {
      if (!read_single(d)) return false;
    }
    return true;
  }
  template <class T, class U>
  bool read_single(pair<T, U> &p) {
    return (read_single(p.first) && read_single(p.second));
  }
  template <size_t N = 0, typename T>
  void read_single_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
      auto &x = std::get<N>(t);
      read_single(x);
      read_single_tuple<N + 1>(t);
    }
  }
  template <class... T>
  bool read_single(tuple<T...> &tpl) {
    read_single_tuple(tpl);
    return true;
  }
  void read() {}
  template <class H, class... T>
  void read(H &h, T &... t) {
    bool f = read_single(h);
    assert(f);
    read(t...);
  }
  Scanner(FILE *fp) : fp(fp) {}
};

struct Printer {
  Printer(FILE *_fp) : fp(_fp) {}
  ~Printer() { flush(); }

  static constexpr size_t SIZE = 1 << 15;
  FILE *fp;
  char line[SIZE], small[50];
  size_t pos = 0;
  void flush() {
    fwrite(line, 1, pos, fp);
    pos = 0;
  }
  void write(const char val) {
    if (pos == SIZE) flush();
    line[pos++] = val;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  void write(T val) {
    if (pos > (1 << 15) - 50) flush();
    if (val == 0) {
      write('0');
      return;
    }
    if (val < 0) {
      write('-');
      val = -val; // todo min
    }
    size_t len = 0;
    while (val) {
      small[len++] = char(0x30 | (val % 10));
      val /= 10;
    }
    for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
    pos += len;
  }
  void write(const string s) {
    for (char c: s) write(c);
  }
  void write(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++) write(s[i]);
  }
  void write(const double x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  void write(const long double x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  template <typename T,
            typename enable_if<has_write<T>::value>::type * = nullptr>
  inline void write(T x) {
    x.write();
  }
  template <class T>
  void write(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  template <class T, class U>
  void write(const pair<T, U> val) {
    write(val.first);
    write(' ');
    write(val.second);
  }
  template <size_t N = 0, typename T>
  void write_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
      if constexpr (N > 0) { write(' '); }
      const auto x = std::get<N>(t);
      write(x);
      write_tuple<N + 1>(t);
    }
  }
  template <class... T>
  bool write(tuple<T...> tpl) {
    write_tuple(tpl);
    return true;
  }
  template <class T, size_t S>
  void write(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  void write(i128 val) {
    string s;
    bool negative = 0;
    if (val < 0) {
      negative = 1;
      val = -val;
    }
    while (val) {
      s += '0' + int(val % 10);
      val /= 10;
    }
    if (negative) s += "-";
    reverse(all(s));
    if (len(s) == 0) s = "0";
    write(s);
  }
};
Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);
void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  printer.write(head);
  if (sizeof...(Tail)) printer.write(' ');
  print(forward<Tail>(tail)...);
}

void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
  scanner.read(head);
  read(tail...);
}
} // namespace fastio
using fastio::print;
using fastio::flush;
using fastio::read;

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __VA_ARGS__; \
  read(__VA_ARGS__)
#define STR(...)      \
  string __VA_ARGS__; \
  read(__VA_ARGS__)
#define CHAR(...)   \
  char __VA_ARGS__; \
  read(__VA_ARGS__)
#define DBL(...)      \
  double __VA_ARGS__; \
  read(__VA_ARGS__)

#define VEC(type, name, size) \
  vector<type> name(size);    \
  read(name)
#define VV(type, name, h, w)                     \
  vector<vector<type>> name(h, vector<type>(w)); \
  read(name)

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"

#line 2 "library/graph/base.hpp"

template <typename T>
struct Edge {
  int frm, to;
  T cost;
  int id;
};

template <typename T = int, bool directed = false>
struct Graph {
  int N, M;
  using cost_type = T;
  using edge_type = Edge<T>;
  vector<edge_type> edges;
  vector<int> indptr;
  vector<edge_type> csr_edges;
  vc<int> vc_deg, vc_indeg, vc_outdeg;
  bool prepared;

  class OutgoingEdges {
  public:
    OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {}

    const edge_type* begin() const {
      if (l == r) { return 0; }
      return &G->csr_edges[l];
    }

    const edge_type* end() const {
      if (l == r) { return 0; }
      return &G->csr_edges[r];
    }

  private:
    const Graph* G;
    int l, r;
  };

  bool is_prepared() { return prepared; }
  constexpr bool is_directed() { return directed; }

  Graph() : N(0), M(0), prepared(0) {}
  Graph(int N) : N(N), M(0), prepared(0) {}

  void build(int n) {
    N = n, M = 0;
    prepared = 0;
    edges.clear();
    indptr.clear();
    csr_edges.clear();
    vc_deg.clear();
    vc_indeg.clear();
    vc_outdeg.clear();
  }

  void add(int frm, int to, T cost = 1, int i = -1) {
    assert(!prepared);
    assert(0 <= frm && 0 <= to && to < N);
    if (i == -1) i = M;
    auto e = edge_type({frm, to, cost, i});
    edges.eb(e);
    ++M;
  }

  // wt, off
  void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); }

  void read_graph(int M, bool wt = false, int off = 1) {
    for (int m = 0; m < M; ++m) {
      INT(a, b);
      a -= off, b -= off;
      if (!wt) {
        add(a, b);
      } else {
        T c;
        read(c);
        add(a, b, c);
      }
    }
    build();
  }

  void build() {
    assert(!prepared);
    prepared = true;
    indptr.assign(N + 1, 0);
    for (auto&& e: edges) {
      indptr[e.frm + 1]++;
      if (!directed) indptr[e.to + 1]++;
    }
    for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; }
    auto counter = indptr;
    csr_edges.resize(indptr.back() + 1);
    for (auto&& e: edges) {
      csr_edges[counter[e.frm]++] = e;
      if (!directed)
        csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id});
    }
  }

  OutgoingEdges operator[](int v) const {
    assert(prepared);
    return {this, indptr[v], indptr[v + 1]};
  }

  vc<int> deg_array() {
    if (vc_deg.empty()) calc_deg();
    return vc_deg;
  }

  pair<vc<int>, vc<int>> deg_array_inout() {
    if (vc_indeg.empty()) calc_deg_inout();
    return {vc_indeg, vc_outdeg};
  }

  int deg(int v) {
    if (vc_deg.empty()) calc_deg();
    return vc_deg[v];
  }

  int in_deg(int v) {
    if (vc_indeg.empty()) calc_deg_inout();
    return vc_indeg[v];
  }

  int out_deg(int v) {
    if (vc_outdeg.empty()) calc_deg_inout();
    return vc_outdeg[v];
  }

  void debug() {
    print("Graph");
    if (!prepared) {
      print("frm to cost id");
      for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id);
    } else {
      print("indptr", indptr);
      print("frm to cost id");
      FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id);
    }
  }

  vc<int> new_idx;
  vc<bool> used_e;

  // G における頂点 V[i] が、新しいグラフで i になるようにする
  // {G, es}
  pair<Graph<T, directed>, vc<int>> rearrange(vc<int> V) {
    if (len(new_idx) != N) new_idx.assign(N, -1);
    if (len(used_e) != M) used_e.assign(M, 0);
    int n = len(V);
    FOR(i, n) new_idx[V[i]] = i;
    Graph<T, directed> G(n);
    vc<int> es;
    FOR(i, n) {
      for (auto&& e: (*this)[V[i]]) {
        if (used_e[e.id]) continue;
        int a = e.frm, b = e.to;
        if (new_idx[a] != -1 && new_idx[b] != -1) {
          used_e[e.id] = 1;
          G.add(new_idx[a], new_idx[b], e.cost);
          es.eb(e.id);
        }
      }
    }
    FOR(i, n) new_idx[V[i]] = -1;
    for (auto&& eid: es) used_e[eid] = 0;
    G.build();
    return {G, es};
  }

private:
  void calc_deg() {
    assert(vc_deg.empty());
    vc_deg.resize(N);
    for (auto&& e: edges) vc_deg[e.frm]++, vc_deg[e.to]++;
  }

  void calc_deg_inout() {
    assert(vc_indeg.empty());
    vc_indeg.resize(N);
    vc_outdeg.resize(N);
    for (auto&& e: edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; }
  }
};
#line 3 "library/graph/strongly_connected_component.hpp"

template <typename Graph>
pair<int, vc<int>> strongly_connected_component(Graph& G) {
  assert(G.is_directed());
  assert(G.is_prepared());
  int N = G.N;
  int C = 0;
  vc<int> comp(N);
  vc<int> low(N);
  vc<int> ord(N, -1);
  vc<int> visited;
  int now = 0;

  auto dfs = [&](auto self, int v) -> void {
    low[v] = now;
    ord[v] = now;
    ++now;
    visited.eb(v);
    for (auto&& [frm, to, cost, id]: G[v]) {
      if (ord[to] == -1) {
        self(self, to);
        chmin(low[v], low[to]);
      } else {
        chmin(low[v], ord[to]);
      }
    }
    if (low[v] == ord[v]) {
      while (1) {
        int u = visited.back();
        visited.pop_back();
        ord[u] = N;
        comp[u] = C;
        if (u == v) break;
      }
      ++C;
    }
  };
  FOR(v, N) {
    if (ord[v] == -1) dfs(dfs, v);
  }
  FOR(v, N) comp[v] = C - 1 - comp[v];
  return {C, comp};
}

template <typename GT>
Graph<int, 1> scc_dag(GT& G, int C, vc<int>& comp) {
  Graph<int, 1> DAG(C);
  vvc<int> edges(C);
  for (auto&& e: G.edges) {
    int x = comp[e.frm], y = comp[e.to];
    if (x == y) continue;
    edges[x].eb(y);
  }
  FOR(c, C) {
    UNIQUE(edges[c]);
    for (auto&& to: edges[c]) DAG.add(c, to);
  }
  DAG.build();
  return DAG;
}
#line 6 "main.cpp"

void solve() {
  LL(N, M);
  using T = tuple<int, int, int, int>;
  VEC(T, dat, M);
  for (auto&& [a, b, c, d]: dat) {
    --a, --c;
    if (b > d) { swap(a, c), swap(b, d); }
  }

  vc<int> value(N);
  vc<bool> done(M);
  vc<int> ANS;
  auto ope = [&](int i) -> void {
    assert(!done[i]);
    auto [a, b, c, d] = dat[i];
    if (value[a] == 0) value[a] = b;
    if (value[c] == 0) value[c] = d;
    ANS.eb(i);
    done[i] = 1;
  };

  // (2,2)
  FOR(i, M) {
    auto [a, b, c, d] = dat[i];
    if (b == 2 && d == 2) ope(i);
  }

  // graph of (1,2)
  Graph<bool, 1> G(N);
  FOR(i, M) {
    auto [a, b, c, d] = dat[i];
    assert(b <= d);
    if (b == 1 && d == 2) G.add(a, c, 1, i);
  }
  G.build();

  auto [nc, comp] = strongly_connected_component(G);
  auto DAG = scc_dag(G, nc, comp);
  auto [indeg, outdeg] = DAG.deg_array_inout();
  vc<int> represent(nc);
  FOR(v, N) represent[comp[v]] = v;
  FOR(v, N) if (value[v] == 2) represent[comp[v]] = v;

  vc<int> que;
  vc<bool> vis(N);
  auto add = [&](int v) -> void {
    if (vis[v]) return;
    que.eb(v);
    vis[v] = 1;
  };

  FOR(c, nc) {
    if (indeg[c] == 0) add(represent[c]);
  }
  while (len(que)) {
    int v = POP(que);
    for (auto&& e: G[v]) {
      int idx = e.id;
      ope(idx);
      add(e.to);
    }
  }

  FOR(i, M) {
    if (done[i]) continue;
    auto [a, b, c, d] = dat[i];
    assert(b == 1 && d == 1);
    ope(i);
  }

  reverse(all(ANS));

  int ans = SUM<int>(value);
  fill(all(value), 0);
  for (auto&& idx: ANS) {
    auto [a, b, c, d] = dat[idx];
    value[a] = b, value[c] = d;
  }
  assert(SUM<int>(value) == ans);

  for (auto&& x: ANS) ++x;
  print(ans);
  print(ANS);
}

signed main() {
  INT(T);
  FOR(T) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3556kb

input:

2
4 4
1 1 2 2
3 2 4 1
1 2 3 2
2 1 4 1
4 2
3 2 4 1
1 2 3 1

output:

7
4 2 1 3
5
2 1

result:

ok Correct. (2 test cases)

Test #2:

score: 0
Accepted
time: 1ms
memory: 3612kb

input:

1
10 13
1 1 2 2
2 1 3 2
1 2 3 1
3 1 4 2
4 1 5 2
5 1 6 2
4 2 6 1
7 1 8 2
8 1 9 2
7 2 9 1
5 2 9 1
8 2 10 2
1 1 10 1

output:

19
13 8 11 10 9 2 1 7 6 5 4 3 12

result:

ok Correct. (1 test case)

Test #3:

score: 0
Accepted
time: 1ms
memory: 3516kb

input:

1
7 5
2 1 6 2
1 2 6 1
1 1 5 1
2 2 7 1
1 1 7 2

output:

8
3 5 2 1 4

result:

ok Correct. (1 test case)

Test #4:

score: 0
Accepted
time: 1ms
memory: 3604kb

input:

1
7 6
2 1 7 2
2 1 4 2
1 2 4 1
2 1 6 1
1 1 6 2
2 2 6 1

output:

9
4 5 3 2 1 6

result:

ok Correct. (1 test case)

Test #5:

score: 0
Accepted
time: 1ms
memory: 3464kb

input:

1
7 5
5 2 7 1
5 1 6 2
3 2 7 1
3 2 6 1
6 1 7 2

output:

7
5 4 2 3 1

result:

ok Correct. (1 test case)

Test #6:

score: 0
Accepted
time: 1ms
memory: 3512kb

input:

1
7 6
1 2 5 1
2 1 7 2
1 2 7 1
2 2 7 1
1 1 5 2
1 2 3 1

output:

8
6 1 5 2 4 3

result:

ok Correct. (1 test case)

Test #7:

score: 0
Accepted
time: 7ms
memory: 3672kb

input:

2000
15 16
2 2 3 1
12 2 15 1
3 2 9 1
6 2 14 1
2 1 15 2
5 2 6 1
7 1 10 1
9 2 15 1
2 2 3 1
4 2 12 1
2 2 9 1
5 2 8 2
3 2 13 1
12 1 13 2
9 2 13 1
5 1 14 2
15 15
5 2 11 1
1 2 8 1
8 1 15 2
6 2 8 2
8 2 9 1
1 1 6 2
6 1 9 2
2 2 5 1
2 1 10 2
7 2 10 1
1 1 15 2
5 2 15 1
7 1 11 2
1 1 2 1
5 2 9 1
15 14
3 1 5 2
1 ...

output:

23
7 6 4 16 15 13 14 10 9 1 5 11 3 8 2 12
20
14 15 5 7 11 6 1 13 10 9 8 12 3 2 4
21
2 8 4 6 10 11 13 3 9 12 7 1 14 5
18
7 4 8 13 12 6 10 14 9 3 5 11 1 2
21
6 4 5 11 14 17 8 10 7 2 13 3 19 16 9 18 12 1 15
21
3 6 2 14 9 13 8 5 4 11 7 12 10 1
21
3 8 1 5 14 2 15 11 9 7 6 13 4 12 10
19
11 9 5 17 7 10 16 ...

result:

ok Correct. (2000 test cases)

Test #8:

score: 0
Accepted
time: 8ms
memory: 3600kb

input:

2000
15 18
10 1 15 2
10 1 15 2
3 2 13 1
5 1 6 2
2 1 10 2
3 2 5 2
7 1 12 2
2 2 3 1
12 1 13 2
5 2 11 1
7 1 15 2
5 1 15 2
6 1 11 2
2 1 6 1
5 1 10 2
5 2 10 1
2 1 7 2
2 1 15 2
15 17
7 2 15 1
6 2 10 1
3 2 12 1
13 2 14 1
1 1 7 2
6 2 15 1
6 2 13 2
1 2 6 1
10 2 15 1
12 2 15 1
9 1 10 2
13 1 15 2
9 2 12 1
3 1 ...

output:

20
14 10 13 15 12 4 16 2 1 3 9 11 7 18 17 5 8 6
21
17 15 5 8 2 4 14 11 13 3 16 10 9 6 1 12 7
21
1 12 9 18 7 15 6 11 8 16 3 13 5 4 2 17 10 14
19
12 8 1 10 18 6 4 16 7 13 2 15 11 14 5 3 9 17
19
4 3 1 5 2 10 8 14 9 7 11 15 13 16 12 6
21
9 11 1 3 6 12 7 8 13 5 4 10 2
20
6 1 10 3 13 12 9 4 11 7 8 2 5
20
...

result:

ok Correct. (2000 test cases)

Test #9:

score: 0
Accepted
time: 1ms
memory: 3644kb

input:

5
27 33
18 2 23 1
13 1 23 2
2 1 7 2
4 2 7 1
2 1 4 2
9 1 27 2
26 2 27 1
3 2 11 1
2 1 4 2
12 1 18 2
4 2 7 1
25 2 26 1
12 1 17 2
5 1 27 2
5 2 22 1
13 2 25 1
2 1 4 2
4 2 7 1
2 2 26 1
4 2 7 1
2 2 7 1
2 2 17 1
19 1 26 1
3 2 24 1
11 1 24 2
3 2 24 1
3 1 9 2
18 1 22 2
9 1 11 2
5 2 23 2
12 2 17 1
2 2 7 1
4 2 ...

output:

33
23 13 10 31 22 14 15 28 33 1 2 16 32 21 20 18 11 4 17 9 5 3 19 12 7 25 8 29 6 27 26 24 30
37
22 14 13 4 6 21 20 2 16 10 8 24 19 5 3 27 26 17 9 25 11 7 28 1 18 15 23 12
38
22 26 15 30 28 10 4 9 27 13 19 34 33 24 21 16 5 2 35 31 20 12 1 36 18 8 32 3 23 6 14 11 29 7 25 17
34
32 23 33 30 16 13 7 5 34...

result:

ok Correct. (5 test cases)

Test #10:

score: 0
Accepted
time: 0ms
memory: 3544kb

input:

5
27 37
10 2 25 2
18 2 22 1
18 1 22 2
2 1 24 2
14 2 26 1
4 1 27 2
15 2 25 1
24 1 27 2
7 2 20 1
11 1 18 1
2 1 14 2
15 1 25 2
10 2 15 1
9 1 16 2
24 2 27 1
24 1 27 2
10 2 12 1
10 1 15 2
9 2 14 1
6 1 15 2
7 1 27 2
24 1 27 2
6 1 22 2
16 1 20 2
15 1 24 2
4 1 27 2
24 1 27 2
2 1 4 2
24 2 27 1
7 1 26 2
24 1 ...

output:

35
10 17 29 15 32 31 27 22 16 8 5 30 21 9 24 3 7 18 34 25 13 12 23 20 36 2 33 14 19 28 11 4 37 35 26 6 1
37
31 30 6 22 15 16 29 28 26 8 7 21 20 4 34 33 32 23 19 11 18 3 25 14 27 17 13 12 9 2 24 5 10 1
35
22 33 25 10 5 15 32 7 20 34 16 31 23 18 8 21 1 24 4 2 30 3 26 14 6 27 19 13 12 29 17 11 9 28
35
...

result:

ok Correct. (5 test cases)

Test #11:

score: 0
Accepted
time: 32ms
memory: 3812kb

input:

200
739 1933
110 1 669 2
17 2 403 1
39 1 538 2
36 2 267 1
66 2 259 1
55 2 483 1
245 2 450 1
30 1 729 2
318 1 568 2
344 1 681 2
11 2 37 1
15 2 192 1
55 2 344 1
426 2 596 1
3 2 683 1
499 1 614 1
302 1 367 2
220 1 528 1
223 2 563 1
255 2 719 1
153 2 688 1
371 2 648 1
704 2 715 1
367 2 477 1
451 2 698 2...

output:

1031
1924 1806 1757 1726 1724 1672 1632 1620 1583 1578 1535 1484 1406 1367 1363 1280 1243 1128 1127 1051 1005 978 954 935 931 837 833 803 694 675 620 618 602 563 555 440 434 430 428 426 397 391 340 295 212 187 172 131 18 16 1824 1627 1605 1485 1348 1166 1130 1478 1396 1017 823 653 652 388 373 352 12...

result:

ok Correct. (200 test cases)

Test #12:

score: 0
Accepted
time: 36ms
memory: 3768kb

input:

200
748 1673
173 2 219 1
77 1 143 2
19 2 384 1
277 2 371 1
272 2 424 1
203 2 737 1
90 1 129 2
302 1 717 2
527 2 700 1
124 2 673 1
129 2 708 1
546 2 650 1
151 2 689 1
475 2 603 1
173 1 574 2
277 1 605 2
129 2 499 1
373 2 546 1
52 2 66 1
238 1 618 2
373 2 473 1
154 2 244 1
278 1 618 2
112 1 129 2
361 ...

output:

1066
1673 1654 1586 1518 1496 1439 1377 1373 1366 1303 1262 1106 1000 998 970 963 958 952 945 928 920 855 821 817 791 758 756 684 679 656 589 579 543 530 529 496 486 478 443 439 436 395 386 301 267 255 244 214 146 79 1501 1123 1099 551 544 217 144 1415 1151 576 419 1386 1167 847 697 364 314 1458 108...

result:

ok Correct. (200 test cases)

Test #13:

score: 0
Accepted
time: 40ms
memory: 3876kb

input:

200
736 1822
500 2 641 1
91 1 700 2
525 2 576 1
101 2 364 1
304 1 689 2
12 2 636 1
338 2 358 1
15 2 296 1
12 2 123 1
608 1 666 2
135 2 473 1
361 1 667 2
137 2 348 1
381 1 502 2
107 1 277 2
23 1 137 2
262 1 602 2
493 1 573 2
158 2 306 1
137 1 587 2
238 2 682 1
580 2 601 1
364 2 620 1
97 2 403 1
27 1 ...

output:

999
1811 1772 1768 1756 1727 1711 1607 1594 1586 1573 1567 1538 1528 1524 1493 1449 1408 1369 1334 1254 1244 1185 1182 1159 1137 1132 1051 1048 1018 945 891 848 836 825 809 790 780 674 644 635 576 569 516 515 375 344 255 119 86 39 1216 593 361 168 1545 1495 160 9 1678 1604 1541 1342 396 136 1767 164...

result:

ok Correct. (200 test cases)

Test #14:

score: 0
Accepted
time: 39ms
memory: 3856kb

input:

200
745 1668
10 1 215 2
136 2 337 1
528 1 727 2
287 1 314 2
93 1 692 2
37 2 497 1
577 2 597 1
100 1 306 2
313 1 743 2
421 1 597 2
313 1 342 2
236 2 305 1
198 1 617 2
52 1 156 2
144 2 368 1
170 1 428 2
209 1 241 2
125 1 306 2
381 2 715 1
37 1 156 2
395 2 581 1
186 2 580 1
81 1 216 2
120 1 306 2
251 2...

output:

1012
1578 1490 1485 1469 1437 1403 1340 1228 1214 1209 1142 1136 1134 1127 1126 1086 1049 1024 1022 976 924 905 890 866 861 851 804 757 744 707 701 670 662 521 511 431 379 369 345 286 284 282 248 225 197 152 113 102 94 81 1430 1382 1369 1247 1181 1143 347 346 1414 1173 1169 580 365 336 287 177 79 10...

result:

ok Correct. (200 test cases)

Test #15:

score: 0
Accepted
time: 65ms
memory: 12100kb

input:

4
74995 97040
23497 1 31972 2
8788 2 69397 1
51522 2 62220 1
9584 1 11674 2
13370 2 36146 1
39507 1 74477 2
1427 1 33348 2
11493 2 13101 1
32701 2 40560 1
28485 1 47620 2
17874 2 62375 1
20454 2 66633 1
13755 2 61191 1
12861 2 63188 1
52357 1 67165 2
12934 1 59450 2
14794 1 17744 2
61153 1 69340 2
8...

output:

99836
96550 96373 95317 94692 94620 94358 93905 93306 93246 92323 91347 91155 91134 91008 90863 90281 89536 87847 86886 85481 84306 84227 84180 83937 83860 83623 82255 81510 80633 80350 80082 79802 78296 76888 75946 74053 71875 66123 65955 65649 64639 63847 61203 60948 60914 60278 59933 59664 58737 ...

result:

ok Correct. (4 test cases)

Test #16:

score: 0
Accepted
time: 65ms
memory: 12568kb

input:

4
74988 97757
6254 1 14126 2
2960 2 7884 1
264 1 26963 2
16894 1 73361 2
40794 2 62973 1
15845 1 45281 2
26578 1 61068 2
14464 2 40449 1
60333 1 73068 2
15459 2 72767 1
44940 2 46205 1
56974 1 65823 2
673 1 12086 2
31184 2 60179 1
924 1 72427 2
22116 2 30494 1
39764 1 50149 2
8984 2 34549 1
47283 1 ...

output:

99896
92796 91947 91296 90797 88471 88044 85638 85019 84442 83521 83293 82246 81851 81601 79739 79327 79054 78306 77718 75851 75744 74968 72852 71841 69944 69501 69355 68100 67224 66112 65582 65327 65098 64656 64648 62023 61699 59268 54564 54348 53496 53094 52232 52221 50205 49766 49226 48890 48349 ...

result:

ok Correct. (4 test cases)

Test #17:

score: 0
Accepted
time: 84ms
memory: 22860kb

input:

2
150000 197734
56160 1 148935 2
14203 2 142849 1
141811 2 149919 1
12846 1 140822 2
32811 2 104214 1
37237 2 73067 1
39554 1 58164 2
17623 1 30566 2
45475 1 88051 2
2948 1 36363 2
121185 1 130780 2
43705 2 139248 1
105491 2 114240 1
22905 2 102102 1
52418 2 85590 1
85614 1 142446 2
145002 2 148378 ...

output:

200477
197353 197114 196917 196516 196419 196175 196062 195722 195326 195246 195053 194988 194789 194786 194287 194237 194087 193476 193327 193078 192710 192154 191325 191236 191027 190040 189471 189141 189052 188366 188326 188319 188111 187705 187270 186984 186950 186887 186414 186404 186226 185356...

result:

ok Correct. (2 test cases)

Test #18:

score: 0
Accepted
time: 77ms
memory: 22480kb

input:

2
149994 189488
105606 1 132955 2
36574 1 86107 2
101018 2 113530 1
122540 2 143227 1
16632 2 89793 1
25443 1 149904 2
99976 2 136760 1
10596 2 112318 1
84455 1 132258 2
85919 2 93042 1
42680 2 68046 1
60230 2 112109 1
30417 1 79467 2
72216 1 109099 2
24431 2 26346 1
31235 1 109427 2
100973 2 114543...

output:

198916
188815 188263 188258 187842 187428 187373 187305 186675 186476 185892 185469 185291 185267 185084 184151 184075 183568 183436 183247 182893 182681 182526 182434 181336 181320 181227 181217 181199 180737 180226 179029 178339 178306 178022 177574 176432 175653 175614 174904 173871 173868 173506...

result:

ok Correct. (2 test cases)

Test #19:

score: 0
Accepted
time: 99ms
memory: 42552kb

input:

1
299998 436956
66759 1 261790 2
109661 2 298655 1
46487 1 170884 2
76196 2 124936 1
70653 1 154152 2
187319 1 250381 2
131759 1 133674 2
153676 1 231765 2
95797 1 282385 2
95776 1 187606 2
6703 2 106783 1
251760 2 267115 1
54769 2 192966 1
115099 2 180310 1
192901 2 250903 1
35909 2 295379 1
22399 ...

output:

394765
435590 434452 432877 432194 431733 431654 426791 425699 425197 424925 424910 423811 420749 420387 418623 417152 412730 412379 410798 406997 405398 403737 403736 403611 401023 400137 399681 398890 398116 397857 396782 395995 395813 394865 394579 394166 393744 393150 392945 392858 392786 391039...

result:

ok Correct. (1 test case)

Test #20:

score: 0
Accepted
time: 95ms
memory: 42704kb

input:

1
299994 438245
38127 2 88766 1
59431 1 233331 2
225189 2 299437 1
76723 2 250018 1
80328 1 284489 2
135816 2 296190 1
27764 2 225748 1
57528 2 199070 1
60742 1 139855 2
129082 1 134585 2
72351 1 177898 2
6906 1 35622 2
33083 2 135388 1
92785 2 180981 1
102084 2 111670 1
116574 1 276018 2
113641 2 2...

output:

362332
438014 435284 434285 434088 433864 432019 430713 430430 429548 428886 428804 428567 425805 425680 422071 421834 420193 420120 418652 416734 416714 416451 416269 416010 414760 414034 414027 413337 411589 411355 409361 408959 408327 405614 405481 404200 403725 402084 401172 400487 400476 399802...

result:

ok Correct. (1 test case)

Test #21:

score: 0
Accepted
time: 74ms
memory: 40432kb

input:

1
299998 498452
39091 2 59969 1
15828 2 270690 1
163349 2 191051 1
42486 1 110810 2
30384 1 223902 2
75185 1 269916 2
56964 2 162885 1
98233 2 196058 1
116601 1 127054 2
85919 1 102077 2
196200 2 214656 1
54709 1 265378 2
87175 1 234557 2
15966 1 21852 2
197173 1 277230 2
48503 2 49594 1
67349 2 242...

output:

400616
498221 497441 497432 495950 495800 495483 492991 491402 491064 490875 490267 490049 489750 489451 488655 486447 485588 485539 485525 485118 482816 482480 481963 481851 479901 478611 478312 476181 472356 471447 470145 469880 469863 469037 468709 467761 467028 465894 465446 464749 463902 463604...

result:

ok Correct. (1 test case)

Test #22:

score: 0
Accepted
time: 104ms
memory: 41560kb

input:

1
299995 499550
77642 2 123304 1
18605 1 73000 2
172858 1 248852 2
232126 2 281373 1
42007 2 117419 1
223100 2 257268 1
20588 1 213881 2
221459 2 249009 1
151591 2 176060 1
192169 1 210466 2
33033 1 83266 2
149863 2 281213 1
201519 1 223370 2
166375 1 193359 2
9628 2 156701 1
174303 2 207866 1
24592...

output:

400646
497493 496356 496328 495756 495705 490766 488866 487252 484499 484317 483685 482619 481762 481475 481202 480570 479755 478144 476958 476742 476662 476135 475812 475467 474407 474028 472953 472909 471617 471583 471320 470610 470275 469912 469425 468678 466140 465531 465516 464818 463679 462731...

result:

ok Correct. (1 test case)

Test #23:

score: 0
Accepted
time: 134ms
memory: 43376kb

input:

1
500000 499975
309101 2 498946 1
281120 2 349107 1
196611 1 428634 2
366844 1 454632 2
99985 2 491559 1
463849 2 481265 1
15616 2 149720 1
217051 2 272193 1
170421 2 180431 1
286108 1 319941 2
35639 1 479590 2
119301 2 472138 1
143961 2 234120 1
76549 1 381510 2
308177 2 334281 1
320444 2 467256 1
...

output:

800360
499476 496868 490451 490102 489200 488618 486529 484493 484332 481808 481778 481491 478331 478017 476477 473553 472880 470629 467406 464389 460975 459932 457913 457796 457629 456225 455367 453281 453170 452141 451937 451859 451842 450325 450286 450284 449761 448251 448004 447637 447130 446921...

result:

ok Correct. (1 test case)

Test #24:

score: 0
Accepted
time: 134ms
memory: 42376kb

input:

1
500000 499909
166847 2 203459 1
216068 1 237544 2
20036 1 283572 2
307653 1 464166 2
254057 1 287554 2
71599 1 145286 2
41917 1 218529 2
9253 2 472960 1
16916 1 44764 2
139158 2 362692 1
7006 1 462308 2
207592 2 323072 1
38281 1 145367 2
152055 2 258524 1
360540 2 390042 1
199177 1 247048 2
335637...

output:

800362
498573 498480 498322 497187 496973 495776 495665 493010 491677 491216 490966 488956 488865 488143 488114 485856 484732 483804 482350 482022 480957 479598 479044 478574 476625 476079 474585 474439 473258 472111 470369 467883 467271 467068 464649 463910 463824 463765 463325 462452 461971 458391...

result:

ok Correct. (1 test case)

Test #25:

score: 0
Accepted
time: 87ms
memory: 39944kb

input:

1
299992 496559
131746 1 232026 2
19016 2 180433 1
64221 1 70241 2
234723 2 260569 1
215594 2 236635 1
50989 2 176563 1
122707 2 278470 1
121505 1 152774 2
50211 2 130736 1
94525 2 281655 1
173141 1 176255 2
1808 2 168157 1
225766 1 247791 2
96263 1 280574 2
87079 1 200248 2
62377 2 87304 1
40727 2 ...

output:

400632
496240 494734 493505 492858 492671 490074 489963 489842 489602 487115 483540 482781 481844 480255 479630 478905 476400 475921 474561 474213 474174 473848 472351 472286 472230 468898 463910 463857 462321 461937 461594 460043 459844 459502 458653 455468 455197 454066 453976 452940 452355 452014...

result:

ok Correct. (1 test case)

Test #26:

score: 0
Accepted
time: 95ms
memory: 40368kb

input:

1
299989 499616
41124 2 236629 1
1708 2 20000 1
34477 1 34685 2
97 1 78502 2
162521 2 235391 1
937 2 226181 1
158944 1 282924 2
30060 2 98585 1
86033 1 271338 2
220135 1 261253 2
31995 1 91491 2
95080 1 145427 2
80355 2 218928 1
97707 2 187312 1
99043 1 175236 2
100685 1 109409 2
40482 2 216124 1
41...

output:

400613
498663 497950 496936 496888 495284 495276 494301 492233 492071 489876 489611 488583 487637 487302 486884 485238 485119 481049 479976 479279 478852 476193 475375 474053 473908 473713 472917 471564 470723 470337 467461 466552 465607 464064 462426 460031 459772 459706 459371 458364 457596 457580...

result:

ok Correct. (1 test case)

Test #27:

score: 0
Accepted
time: 110ms
memory: 46280kb

input:

1
500000 499960
156495 2 222771 1
192943 1 231434 2
52394 2 129100 1
22349 1 286266 2
252684 2 449139 1
49700 2 421137 1
133905 1 189382 2
278790 2 407847 1
155574 2 156461 1
355506 2 449725 1
73782 1 314244 2
39645 2 471881 1
95343 2 321999 1
382747 2 485247 1
24729 1 481479 2
179015 1 488398 2
211...

output:

800381
495976 495973 494827 493502 491761 488984 488968 488853 487860 485880 479995 477419 477126 475704 475211 474774 473932 472862 471409 471186 470586 469967 469589 469357 469140 467802 465113 464005 463980 463508 461741 460126 458904 458316 457996 457233 456976 456585 455706 453686 453677 453649...

result:

ok Correct. (1 test case)

Test #28:

score: 0
Accepted
time: 133ms
memory: 41456kb

input:

1
500000 499907
85402 2 291981 1
247209 2 375781 1
121657 2 393609 1
145810 2 254554 1
278586 1 476600 2
120097 1 305154 2
134366 1 240630 2
126915 2 404476 1
163364 1 458303 2
298699 1 471885 2
60039 2 134949 1
218817 2 223093 1
76531 2 370130 1
124352 2 128371 1
65133 2 113736 1
24905 2 390647 1
4...

output:

800349
499523 498529 497966 497228 496461 494722 494679 494632 494179 492820 491728 490720 489964 486968 486958 486372 486269 484259 483774 483641 483624 481634 481555 481200 480656 477141 476852 476780 475940 475823 474342 471786 469780 467576 467296 466864 464932 464794 464438 463582 463568 462830...

result:

ok Correct. (1 test case)