QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#110916#6560. Broken Minimum Spanning TreemaspyAC ✓5ms3884kbC++2323.0kb2023-06-04 19:00:472023-06-04 19:00:48

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-04 19:00:48]
  • 评测
  • 测评结果:AC
  • 用时:5ms
  • 内存:3884kb
  • [2023-06-04 19:00:47]
  • 提交

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/ds/unionfind/unionfind.hpp"

struct UnionFind {
  int n, n_comp;
  vc<int> dat; // par or (-size)
  UnionFind(int n = 0) { build(n); }

  void build(int m) {
    n = m, n_comp = m;
    dat.assign(n, -1);
  }

  void reset() { build(n); }

  int operator[](int x) {
    while (dat[x] >= 0) {
      int pp = dat[dat[x]];
      if (pp < 0) { return dat[x]; }
      x = dat[x] = pp;
    }
    return x;
  }

  ll size(int x) {
    assert(dat[x] < 0);
    return -dat[x];
  }

  bool merge(int x, int y) {
    x = (*this)[x], y = (*this)[y];
    if (x == y) return false;
    if (-dat[x] < -dat[y]) swap(x, y);
    dat[x] += dat[y], dat[y] = x, n_comp--;
    return true;
  }
};
#line 2 "library/graph/ds/link_cut.hpp"

template <typename Node, int NODES>
struct LinkCutTree_base {
  int n;
  Node *nodes;

  LinkCutTree_base(int n = 0) : n(n) {
    nodes = new Node[NODES];
    FOR(i, n) nodes[i] = Node(i);
  }

  Node *operator[](int v) { return &nodes[v]; }

  // パスを表す splay tree の根になっているかどうか
  bool is_root(Node *c) { return state(c) == 0; }
  bool is_root(int c) { return state(&nodes[c]) == 0; }

  Node *get_root(Node *c) {
    expose(c);
    while (c->l) {
      c->push();
      c = c->l;
    }
    splay(c);
    return c;
  }

  int get_root(int c) { return get_root(&nodes[c])->idx; }

  // c の親を p にする。内部で evert するのでいつ呼んでも大丈夫。
  virtual void link(Node *c, Node *p) {
    evert(c);
    expose(p);
    c->p = p;
    p->r = c;
    p->update();
  }

  // c の親を p にする。内部で evert するのでいつ呼んでも大丈夫。
  virtual void link(int c, int p) { return link(&nodes[c], &nodes[p]); }

  void cut(Node *a, Node *b) {
    evert(a);
    expose(b);
    assert(!b->p);
    assert((b->l) == a);
    b->l->p = nullptr;
    b->l = nullptr;
    b->update();
  }

  void cut(int a, int b) { return cut(&nodes[a], &nodes[b]); }

  void evert(Node *c) {
    expose(c);
    c->reverse();
    c->push();
  }

  void evert(int c) { evert(&nodes[c]); }

  Node *lca(Node *u, Node *v) {
    assert(get_root(u) == get_root(v));
    expose(u);
    return expose(v);
  }

  int lca(int u, int v) { return lca(&nodes[u], &nodes[v])->idx; }

  // c と根までが繋がれている状態に変更して、根を return する
  virtual Node *expose(Node *c) {
    Node *now = c;
    Node *rp = nullptr; // 今まで作ったパス
    while (now) {
      splay(now);
      now->r = rp; // 子方向の変更
      now->update();
      rp = now;
      now = now->p;
    }
    splay(c);
    return rp;
  }

  int expose(int c) {
    Node *x = expose(&nodes[c]);
    if (!x) return -1;
    return x->idx;
  }

  Node *get_parent(Node *x) {
    expose(x);
    if (!x->l) return nullptr;
    x = x->l;
    while (x->r) x = x->r;
    return x;
  }

  int get_parent(int x) {
    Node *p = get_parent((*this)[x]);
    return (p ? p->idx : -1);
  }

  void debug() {
    FOR(i, n) { nodes[i].debug(); }
  }

  virtual void rotate(Node *n) {
    // n を根に近づける
    Node *pp, *p, *c;
    p = n->p;
    pp = p->p;

    if (p->l == n) {
      c = n->r;
      n->r = p;
      p->l = c;
    } else {
      c = n->l;
      n->l = p;
      p->r = c;
    }

    if (pp && pp->l == p) pp->l = n;
    if (pp && pp->r == p) pp->r = n;
    n->p = pp;
    p->p = n;
    if (c) c->p = p;
  }

  inline int state(Node *n) {
    if (!n->p) return 0;
    if (n->p->l == n) return 1;
    if (n->p->r == n) return -1;
    return 0;
  }

  void splay(Node *c) {
    c->push();
    while (!is_root(c)) {
      Node *p = c->p;
      Node *pp = (p ? p->p : nullptr);
      if (state(p) == 0) {
        p->push(), c->push();
        rotate(c);
        if (p) p->update();
      }
      elif (state(c) == state(p)) {
        pp->push(), p->push(), c->push();
        rotate(p);
        rotate(c);
        if (pp) pp->update();
        if (p) p->update();
      }
      else {
        pp->push(), p->push(), c->push();
        rotate(c);
        rotate(c);
        if (p) p->update();
        if (pp) pp->update();
      }
    }
    c->update();
  }
};

struct LCT_Node_base {
  LCT_Node_base *l, *r, *p;
  int idx;
  bool rev;
  LCT_Node_base(int i = 0) : l(nullptr), r(nullptr), p(nullptr), idx(i) {}

  void update() {}

  void push() {
    if (rev) {
      if (l) l->reverse();
      if (r) r->reverse();
      rev = 0;
    }
  }

  void reverse() {
    rev ^= 1;
    swap(l, r);
  }
};

template <int NODES>
using LinkCutTree = LinkCutTree_base<LCT_Node_base, NODES>;
#line 2 "library/graph/ds/link_cut_path.hpp"

template <typename Node, int NODES>
struct LinkCutTree_Path_base : public LinkCutTree_base<Node, NODES> {
  using X = typename Node::X;

  LinkCutTree_Path_base(int n) : LinkCutTree_base<Node, NODES>(n) {}

  LinkCutTree_Path_base(vc<X> dat) : LinkCutTree_base<Node, NODES>(len(dat)) {
    FOR(v, len(dat)) {
      Node *c = (*this)[v];
      set(c, dat[v]);
    }
  }

  template <typename F>
  LinkCutTree_Path_base(int n, F f) : LinkCutTree_base<Node, NODES>(n) {
    FOR(v, n) {
      X x = f(v);
      Node *c = (*this)[v];
      set(c, x);
    }
  }

  void set(Node *c, X x) {
    this->evert(c);
    c->x = x;
    c->update();
  }

  void set(int c, X x) { set((*this)[c], x); }

  void multiply(Node *c, X x) { set(c, Node::Mono::op(c->x, x)); }

  void multiply(int c, X x) { multiply((*this)[c], x); }

  X prod_path(Node *a, Node *b) {
    this->evert(a);
    this->expose(b);
    return b->prod;
  }

  X prod_path(int a, int b) { return prod_path((*this)[a], (*this)[b]); }
};

template <typename Monoid>
struct LCT_Node_Monoid {
  using Mono = Monoid;
  using X = typename Monoid::value_type;
  LCT_Node_Monoid *l, *r, *p;
  int idx;
  X x, prod, rev_prod;
  bool rev;
  LCT_Node_Monoid(int i = 0)
      : l(nullptr),
        r(nullptr),
        p(nullptr),
        idx(i),
        x(Monoid::unit()),
        prod(Monoid::unit()),
        rev_prod(Monoid::unit()) {}

  void update() {
    prod = rev_prod = x;
    if (l) {
      prod = Monoid::op(l->prod, prod);
      rev_prod = Monoid::op(rev_prod, l->rev_prod);
    }
    if (r) {
      prod = Monoid::op(prod, r->prod);
      rev_prod = Monoid::op(r->rev_prod, rev_prod);
    }
  }

  void push() {
    if (rev) {
      if (l) l->reverse();
      if (r) r->reverse();
      rev = 0;
    }
  }

  void reverse() {
    rev ^= 1;
    swap(l, r);
    swap(prod, rev_prod);
  }

  void debug() {
    int li = (l ? l->idx : -1);
    int ri = (r ? r->idx : -1);
    int pi = (p ? p->idx : -1);
    print("idx", idx, "l", li, "r", ri, "p", pi, "x", x, "prod", prod,
          "rev_prod", rev_prod);
  }
};

template <typename Monoid>
struct LCT_Node_CommutativeMonoid {
  using Mono = Monoid;
  using X = typename Mono::value_type;
  LCT_Node_CommutativeMonoid *l, *r, *p;
  int idx;
  X x, prod;
  bool rev;
  LCT_Node_CommutativeMonoid(int i = 0)
      : l(nullptr),
        r(nullptr),
        p(nullptr),
        idx(i),
        x(Mono::unit()),
        prod(Mono::unit()) {}

  void update() {
    prod = x;
    if (l) { prod = Mono::op(l->prod, prod); }
    if (r) { prod = Mono::op(prod, r->prod); }
  }

  void push() {
    if (rev) {
      if (l) l->reverse();
      if (r) r->reverse();
      rev = 0;
    }
  }

  void reverse() {
    rev ^= 1;
    swap(l, r);
  }

  void debug() {
    int li = (l ? l->idx : -1);
    int ri = (r ? r->idx : -1);
    int pi = (p ? p->idx : -1);
    print("idx", idx, "l", li, "r", ri, "p", pi, "x", x, "prod", prod);
  }
};

template <typename Monoid, int NODES>
using LinkCutTree_Path = LinkCutTree_Path_base<LCT_Node_Monoid<Monoid>, NODES>;

template <typename Monoid, int NODES>
using LinkCutTree_Path_Commutative
    = LinkCutTree_Path_base<LCT_Node_CommutativeMonoid<Monoid>, NODES>;
#line 6 "main.cpp"

#line 2 "library/alg/monoid/min_idx.hpp"

template <typename T, bool tie_is_left = true>
struct Monoid_Min_Idx {
  using value_type = pair<T, int>;
  using X = value_type;
  static constexpr bool is_small(const X& x, const X& y) {
    if (x.fi < y.fi) return true;
    if (x.fi > y.fi) return false;
    return (tie_is_left ? (x.se < y.se) : (x.se >= y.se));
  }
  static X op(X x, X y) { return (is_small(x, y) ? x : y); }
  static constexpr X unit() { return {infty<T>, -1}; }
  static constexpr bool commute = true;
};
#line 8 "main.cpp"

void solve() {
  LL(N, M);
  vc<int> A(M), B(M), C(M);
  FOR(i, M) read(A[i]), read(B[i]), read(C[i]);
  for (auto&& x: A) --x;
  for (auto&& x: B) --x;
  FOR(i, N - 1) C[i] = 2 * C[i] - 1;
  FOR(i, N - 1, M) C[i] = 2 * C[i];

  vc<int> use(M);
  auto I = argsort(C);
  UnionFind uf(N);
  FOR(i, M) {
    int idx = I[i];
    if (uf.merge(A[idx], B[idx])) use[idx] = 1;
  }

  using Mono = Monoid_Min_Idx<int>;
  LinkCutTree_Path_Commutative<Mono, 5000> LCT(N + N - 1);
  FOR(e, N - 1) {
    LCT.link(N + e, A[e]);
    LCT.link(N + e, B[e]);
    if (!use[e]) { LCT.set(N + e, {0, e}); }
  }

  vc<pi> ANS;

  FOR(e, N - 1, M) {
    if (!use[e]) continue;
    auto [mi, idx] = LCT.prod_path(A[e], B[e]);
    assert(mi == 0);
    ANS.eb(idx, e);
    LCT.cut(A[idx], N + idx);
    LCT.cut(B[idx], N + idx);
    LCT.link(N + e, A[e]);
    LCT.link(N + e, B[e]);
  }

  print(len(ANS));
  for (auto&& [a, b]: ANS) print(1 + a, 1 + b);
}

signed main() {
  int T = 1;
  // INT(T);
  FOR(T) solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3664kb

input:

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

output:

1
1 4

result:

ok correct!

Test #2:

score: 0
Accepted
time: 2ms
memory: 3664kb

input:

6 8
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10
1 3 1
4 6 1

output:

2
2 7
5 8

result:

ok correct!

Test #3:

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

input:

2000 1999
1262 1505 968661582
323 1681 787089412
1132 129 88786681
1909 587 762050278
979 1371 230688681
1686 521 980519364
975 191 887826021
869 461 899130441
1433 259 961154249
1718 547 721696188
1254 1042 458319755
1779 267 85751052
1170 813 283230029
309 20 971682908
224 417 255325364
1084 986 7...

output:

0

result:

ok correct!

Test #4:

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

input:

1999 1998
1757 1820 444157563
1757 395 754598547
1757 1571 432619009
1757 1009 456234067
1757 824 935569725
1757 1698 476714469
1757 1420 901765343
1757 1175 225295107
1757 1512 721959801
1757 1585 955067704
1757 1739 635181418
1757 1686 891225461
1757 84 132683224
1757 1696 48915557
1757 1623 42602...

output:

0

result:

ok correct!

Test #5:

score: 0
Accepted
time: 2ms
memory: 3804kb

input:

1999 1998
1345 647 232183406
40 837 279910457
819 857 137486924
255 1378 517489941
827 1565 894953662
1556 1545 898170464
965 877 72248541
1631 298 635713424
895 197 366305735
966 1160 515776809
1870 1638 220711661
1736 220 716014108
1914 1609 759121968
1293 153 272816132
1936 1433 263859075
985 460...

output:

0

result:

ok correct!

Test #6:

score: 0
Accepted
time: 2ms
memory: 3680kb

input:

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

output:

0

result:

ok correct!

Test #7:

score: 0
Accepted
time: 2ms
memory: 3772kb

input:

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

output:

0

result:

ok correct!

Test #8:

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

input:

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

output:

499
1 500
2 501
3 502
4 503
5 504
6 505
7 506
8 507
9 508
10 509
11 510
12 511
13 512
14 513
15 514
16 515
17 516
18 517
19 518
20 519
21 520
22 521
23 522
24 523
25 524
26 525
27 526
28 527
29 528
30 529
31 530
32 531
33 532
34 533
35 534
36 535
37 536
38 537
39 538
40 539
41 540
42 541
43 542
44 5...

result:

ok correct!

Test #9:

score: 0
Accepted
time: 2ms
memory: 3752kb

input:

500 998
198 227 1
227 315 1
315 426 1
426 400 1
400 61 1
61 143 1
143 487 1
487 65 1
65 415 1
415 434 1
434 327 1
327 190 1
190 411 1
411 51 1
51 91 1
91 364 1
364 185 1
185 393 1
393 89 1
89 53 1
53 66 1
66 69 1
69 13 1
13 5 1
5 45 1
45 314 1
314 291 1
291 490 1
490 92 1
92 175 1
175 458 1
458 218 ...

output:

0

result:

ok correct!

Test #10:

score: 0
Accepted
time: 2ms
memory: 3704kb

input:

500 998
360 250 1
250 71 1
71 170 1
170 492 1
492 419 1
419 145 1
145 188 1
188 433 1
433 186 1
186 161 1
161 398 1
398 19 1
19 479 1
479 401 1
401 40 1
40 176 1
176 212 1
212 353 1
353 290 1
290 43 1
43 322 1
322 447 1
447 47 1
47 468 1
468 456 1
456 343 1
343 339 1
339 52 1
52 251 1
251 130 1
130 ...

output:

0

result:

ok correct!

Test #11:

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

input:

500 998
369 45 2
45 364 2
364 300 2
300 195 2
195 291 2
291 390 2
390 122 2
122 331 2
331 408 2
408 91 2
91 298 2
298 116 2
116 301 2
301 287 2
287 338 2
338 4 2
4 79 2
79 177 2
177 387 2
387 125 2
125 477 2
477 11 2
11 284 2
284 102 2
102 305 2
305 395 2
395 112 2
112 280 2
280 294 2
294 232 2
232 ...

output:

499
10 500
182 501
399 502
429 503
484 504
250 505
172 506
116 507
180 508
82 509
275 510
178 511
148 512
13 513
380 514
54 515
393 516
382 517
53 518
72 519
225 520
485 521
279 522
491 523
216 524
386 525
18 526
122 527
358 528
136 529
230 530
48 531
344 532
315 533
104 534
150 535
151 536
205 537
...

result:

ok correct!

Test #12:

score: 0
Accepted
time: 2ms
memory: 3748kb

input:

500 998
298 314 1
467 314 1
9 314 1
345 298 1
497 298 1
315 467 1
147 345 1
154 345 1
16 345 1
226 497 1
406 147 1
204 298 1
351 406 1
432 314 1
274 406 1
340 274 1
395 226 1
173 315 1
180 274 1
207 9 1
495 204 1
213 298 1
413 207 1
450 204 1
25 147 1
161 497 1
231 180 1
175 467 1
199 231 1
454 231 ...

output:

0

result:

ok correct!

Test #13:

score: 0
Accepted
time: 2ms
memory: 3796kb

input:

500 998
42 349 1
256 42 1
202 349 1
23 42 1
252 42 1
175 42 1
67 252 1
302 67 1
337 252 1
495 252 1
14 349 1
347 202 1
494 495 1
206 347 1
1 302 1
434 349 1
475 206 1
243 206 1
135 494 1
179 495 1
226 202 1
490 226 1
481 1 1
165 243 1
114 495 1
463 256 1
282 114 1
411 202 1
25 1 1
163 67 1
388 179 1...

output:

0

result:

ok correct!

Test #14:

score: 0
Accepted
time: 2ms
memory: 3700kb

input:

500 998
493 328 2
444 493 2
356 328 2
374 328 2
135 328 2
292 444 2
323 135 2
296 328 2
407 493 2
207 374 2
118 296 2
490 135 2
357 323 2
464 292 2
279 323 2
183 493 2
81 356 2
367 407 2
235 356 2
354 292 2
479 464 2
214 118 2
406 357 2
164 279 2
230 356 2
380 164 2
399 135 2
344 81 2
190 490 2
422 ...

output:

499
1 500
2 501
3 502
4 503
5 504
6 505
7 506
8 507
9 508
10 509
11 510
12 511
13 512
14 513
15 514
16 515
17 516
18 517
19 518
20 519
21 520
22 521
23 522
24 523
25 524
26 525
27 526
28 527
29 528
30 529
31 530
32 531
33 532
34 533
35 534
36 535
37 536
38 537
39 538
40 539
41 540
42 541
43 542
44 5...

result:

ok correct!

Test #15:

score: 0
Accepted
time: 2ms
memory: 3736kb

input:

10 20
3 5 132699872
7 3 667475629
10 7 829222331
1 7 265644695
4 3 226461311
2 7 720348681
6 10 703702759
8 4 153004599
9 10 646988804
1 9 45480111
2 4 784301144
1 9 628023542
7 8 449200681
9 2 240371799
3 2 420603433
3 9 838425734
4 6 623790050
1 7 513829155
1 9 883183260
10 3 422484921

output:

5
3 10
6 14
2 15
7 17
9 20

result:

ok correct!

Test #16:

score: 0
Accepted
time: 2ms
memory: 3660kb

input:

100 200
69 52 334673965
90 52 598347660
62 52 671196898
38 90 561150605
97 69 844448459
25 90 865251171
41 38 773653441
49 97 813975775
99 41 996226580
54 69 583281785
34 38 385173507
56 97 285801905
17 38 946715780
67 17 139770128
43 97 890101081
68 90 370458274
74 17 698466900
6 67 19950896
58 56 ...

output:

50
17 103
2 104
3 107
5 108
8 109
4 112
13 115
20 116
6 120
15 121
21 125
41 126
16 127
36 131
45 132
7 134
10 135
87 136
24 137
28 138
9 145
64 148
44 150
52 152
25 153
75 156
19 157
47 160
77 163
69 166
61 168
96 170
35 171
49 174
60 176
58 177
65 180
81 181
74 182
39 183
67 184
95 185
23 186
80 1...

result:

ok correct!

Test #17:

score: 0
Accepted
time: 5ms
memory: 3796kb

input:

2000 3000
1279 1465 434468566
1062 1279 993799662
1494 1465 490141333
529 1279 207090506
119 1279 494706603
1830 1062 798435525
1307 1279 501822892
362 119 776215279
1330 1494 64095945
1823 529 302809447
1882 529 298925061
1394 529 639185117
1852 362 939130818
752 529 845078929
104 752 853251112
126...

output:

617
7 2000
8 2001
13 2002
54 2003
48 2008
3 2009
19 2011
2 2014
16 2016
31 2017
14 2018
51 2019
12 2021
33 2023
49 2025
229 2026
58 2027
55 2029
71 2030
61 2031
67 2033
22 2034
311 2037
34 2038
15 2040
73 2041
45 2043
78 2045
114 2046
57 2047
168 2048
82 2049
127 2050
124 2051
35 2052
176 2055
218 2...

result:

ok correct!

Test #18:

score: 0
Accepted
time: 2ms
memory: 3792kb

input:

2000 3000
285 1808 694643588
224 285 690510187
908 1808 663193044
486 224 663712643
324 908 165916788
1403 285 948845412
1310 324 12561437
1948 285 642808470
883 1310 358793640
396 1808 869731392
1276 1310 621641177
203 1948 231802320
1547 1276 39692873
830 285 636658714
1357 1948 177401445
303 203 ...

output:

613
2 2000
1 2001
11 2003
19 2004
16 2006
41 2007
3 2008
17 2009
20 2010
242 2011
6 2015
8 2016
4 2017
10 2018
61 2019
44 2021
14 2022
100 2023
40 2024
119 2027
263 2028
31 2029
24 2031
307 2034
438 2035
236 2037
368 2039
77 2042
241 2043
178 2045
158 2048
156 2049
90 2050
171 2052
97 2053
167 2054
...

result:

ok correct!

Test #19:

score: 0
Accepted
time: 4ms
memory: 3864kb

input:

2000 3000
1966 1337 886061561
1564 1966 321739163
878 1966 383102115
15 1337 355428698
392 15 389233814
1520 1337 163779508
1349 392 323493610
1126 1349 804548395
1739 1337 508691040
956 1564 924027693
674 1126 845489957
1749 1739 290423046
1926 1966 647294733
456 1966 656155212
1746 1564 106274278
...

output:

603
10 2000
3 2001
1 2002
42 2005
8 2006
11 2007
86 2008
100 2010
9 2011
14 2012
268 2014
41 2016
18 2017
24 2019
21 2021
20 2022
93 2023
73 2024
162 2025
46 2026
54 2027
56 2028
60 2031
102 2032
106 2033
99 2035
132 2037
50 2039
38 2041
55 2042
130 2044
133 2046
22 2047
53 2048
13 2049
206 2050
61 ...

result:

ok correct!

Test #20:

score: 0
Accepted
time: 4ms
memory: 3832kb

input:

2000 3000
487 1828 891595258
848 1828 70120465
399 1828 222566316
2000 1828 390057442
589 487 561090448
1878 399 923567050
1547 848 289163461
724 1828 712597149
856 487 612088317
1932 848 498697630
177 1932 225589816
1541 856 745128386
1229 399 501103338
40 1828 283700123
1206 1878 364593718
519 40 ...

output:

624
285 2000
6 2002
10 2003
21 2007
13 2008
33 2010
633 2011
1 2013
43 2014
82 2017
5 2018
104 2019
29 2020
8 2022
22 2023
79 2024
45 2025
128 2026
12 2027
80 2028
211 2031
30 2032
17 2034
66 2035
41 2039
255 2040
107 2042
9 2045
113 2046
49 2047
44 2048
18 2051
187 2052
178 2053
109 2054
127 2058
5...

result:

ok correct!

Test #21:

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

input:

2000 3000
28 909 901469954
874 909 630039044
1150 874 369081856
180 1150 796073964
199 874 607566492
1260 1150 672891947
233 180 524809142
390 909 531859461
122 874 275924720
457 1260 521407422
872 28 975420599
497 872 901775699
885 390 839588422
1242 199 380484388
1598 28 823494399
202 885 41696165...

output:

618
20 2000
4 2004
10 2005
5 2006
2 2007
6 2008
1 2009
8 2010
19 2013
23 2016
11 2018
12 2019
79 2020
32 2025
28 2026
38 2027
15 2029
46 2030
33 2031
75 2033
18 2035
122 2037
30 2038
285 2039
29 2040
116 2043
52 2044
41 2047
64 2050
47 2051
113 2052
112 2053
192 2054
234 2055
67 2057
166 2058
61 205...

result:

ok correct!

Test #22:

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

input:

2 3000
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1...

output:

0

result:

ok correct!

Test #23:

score: 0
Accepted
time: 3ms
memory: 3816kb

input:

2000 2000
273 274 976318149
1818 1819 911362963
920 921 733992701
1147 1148 968069222
1076 1077 479630568
1576 1577 723601562
860 861 477629418
747 748 636289483
219 220 254346042
610 611 561106993
1173 1174 117741584
1788 1789 433959137
437 438 566901968
723 724 578256290
984 985 201368344
954 955 ...

output:

1
1575 2000

result:

ok correct!

Test #24:

score: 0
Accepted
time: 5ms
memory: 3816kb

input:

2000 2500
1936 1937 470205868
750 751 637463850
75 76 353874306
1012 1013 575007557
679 680 452883390
268 269 382879319
1885 1886 619233286
1617 1618 985926999
365 366 731212904
703 704 136815299
1543 1544 6628104
1586 1587 963856921
1904 1905 377843376
254 255 540189789
690 691 218468543
1169 1170 ...

output:

371
8 2000
69 2002
24 2003
9 2004
39 2006
32 2007
1555 2008
36 2010
183 2011
49 2012
50 2013
22 2015
54 2017
31 2018
61 2022
159 2023
76 2024
88 2025
125 2026
110 2027
409 2028
177 2029
206 2031
166 2032
135 2033
185 2035
762 2036
267 2038
213 2039
672 2040
431 2041
175 2042
147 2043
224 2044
275 20...

result:

ok correct!

Test #25:

score: 0
Accepted
time: 4ms
memory: 3800kb

input:

2000 3000
1100 1101 966680160
584 585 619523116
196 197 969093892
1265 1266 112963336
1463 1464 437550508
1320 1321 888461822
1414 1415 755948833
897 898 48495011
365 366 564439441
869 870 108232038
1323 1324 469077928
1432 1433 609528786
1885 1886 447585062
81 82 480544752
1819 1820 385633491
1371 ...

output:

588
1 2000
3 2001
6 2002
24 2003
215 2004
41 2005
19 2008
17 2009
55 2010
22 2011
45 2018
69 2020
9 2021
75 2025
36 2026
50 2027
122 2028
112 2030
156 2031
39 2034
158 2035
255 2037
98 2040
96 2043
94 2044
164 2045
79 2046
275 2047
51 2053
192 2054
282 2055
95 2057
679 2059
171 2061
205 2064
59 2066...

result:

ok correct!

Test #26:

score: 0
Accepted
time: 2ms
memory: 3688kb

input:

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

output:

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

result:

ok correct!