QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#733694#9539. Disrupting CommunicationsmaspyAC ✓121ms24044kbC++2341.1kb2024-11-10 20:30:402024-11-10 20:30:43

Judging History

This is the latest submission verdict.

  • [2024-11-10 20:30:43]
  • Judged
  • Verdict: AC
  • Time: 121ms
  • Memory: 24044kb
  • [2024-11-10 20:30:40]
  • Submitted

answer

#line 1 "/home/maspy/compro/library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else

// https://codeforces.com/blog/entry/96344
#pragma GCC optimize("Ofast,unroll-loops")
// いまの CF だとこれ入れると動かない?
// #pragma GCC target("avx2,popcnt")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
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); }
int popcnt_sgn(int x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
// (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>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

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

#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) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  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;
    (check(x) ? ok : ng) = 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;
    (check(x) ? ok : ng) = 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;
}

template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &... others) {
  vc<T> &res = first;
  (res.insert(res.end(), others.begin(), others.end()), ...);
}
#endif
#line 1 "/home/maspy/compro/library/other/io.hpp"
#define FASTIO
#include <unistd.h>

// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
  char num[10000][4];
  constexpr Pre() : num() {
    for (int i = 0; i < 10000; i++) {
      int n = i;
      for (int j = 3; j >= 0; j--) {
        num[i][j] = n % 10 | '0';
        n /= 10;
      }
    }
  }
} constexpr pre;

inline void load() {
  memcpy(ibuf, ibuf + pil, pir - pil);
  pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
  pil = 0;
  if (pir < SZ) ibuf[pir++] = '\n';
}

inline void flush() {
  fwrite(obuf, 1, por, stdout);
  por = 0;
}

void rd(char &c) {
  do {
    if (pil + 1 > pir) load();
    c = ibuf[pil++];
  } while (isspace(c));
}

void rd(string &x) {
  x.clear();
  char c;
  do {
    if (pil + 1 > pir) load();
    c = ibuf[pil++];
  } while (isspace(c));
  do {
    x += c;
    if (pil == pir) load();
    c = ibuf[pil++];
  } while (!isspace(c));
}

template <typename T>
void rd_real(T &x) {
  string s;
  rd(s);
  x = stod(s);
}

template <typename T>
void rd_integer(T &x) {
  if (pil + 100 > pir) load();
  char c;
  do
    c = ibuf[pil++];
  while (c < '-');
  bool minus = 0;
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (c == '-') { minus = 1, c = ibuf[pil++]; }
  }
  x = 0;
  while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (minus) x = -x;
  }
}

void rd(int &x) { rd_integer(x); }
void rd(ll &x) { rd_integer(x); }
void rd(i128 &x) { rd_integer(x); }
void rd(u32 &x) { rd_integer(x); }
void rd(u64 &x) { rd_integer(x); }
void rd(u128 &x) { rd_integer(x); }
void rd(double &x) { rd_real(x); }
void rd(long double &x) { rd_real(x); }
void rd(f128 &x) { rd_real(x); }

template <class T, class U>
void rd(pair<T, U> &p) {
  return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
  if constexpr (N < std::tuple_size<T>::value) {
    auto &x = std::get<N>(t);
    rd(x);
    rd_tuple<N + 1>(t);
  }
}
template <class... T>
void rd(tuple<T...> &tpl) {
  rd_tuple(tpl);
}

template <size_t N = 0, typename T>
void rd(array<T, N> &x) {
  for (auto &d: x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
  for (auto &d: x) rd(d);
}

void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
  rd(h), read(t...);
}

void wt(const char c) {
  if (por == SZ) flush();
  obuf[por++] = c;
}
void wt(const string s) {
  for (char c: s) wt(c);
}
void wt(const char *s) {
  size_t len = strlen(s);
  for (size_t i = 0; i < len; i++) wt(s[i]);
}

template <typename T>
void wt_integer(T x) {
  if (por > SZ - 100) flush();
  if (x < 0) { obuf[por++] = '-', x = -x; }
  int outi;
  for (outi = 96; x >= 10000; outi -= 4) {
    memcpy(out + outi, pre.num[x % 10000], 4);
    x /= 10000;
  }
  if (x >= 1000) {
    memcpy(obuf + por, pre.num[x], 4);
    por += 4;
  } else if (x >= 100) {
    memcpy(obuf + por, pre.num[x] + 1, 3);
    por += 3;
  } else if (x >= 10) {
    int q = (x * 103) >> 10;
    obuf[por] = q | '0';
    obuf[por + 1] = (x - q * 10) | '0';
    por += 2;
  } else
    obuf[por++] = x | '0';
  memcpy(obuf + por, out + outi + 4, 96 - outi);
  por += 96 - outi;
}

template <typename T>
void wt_real(T x) {
  ostringstream oss;
  oss << fixed << setprecision(15) << double(x);
  string s = oss.str();
  wt(s);
}

void wt(int x) { wt_integer(x); }
void wt(ll x) { wt_integer(x); }
void wt(i128 x) { wt_integer(x); }
void wt(u32 x) { wt_integer(x); }
void wt(u64 x) { wt_integer(x); }
void wt(u128 x) { wt_integer(x); }
void wt(double x) { wt_real(x); }
void wt(long double x) { wt_real(x); }
void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first);
  wt(' ');
  wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::read;
using fastio::print;
using fastio::flush;

#if defined(LOCAL)
#define SHOW(...) SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define SHOW1(x) print(#x, "=", (x)), flush()
#define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), flush()
#define SHOW6(x, y, z, w, v, u) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), #u, "=", (u)), flush()
#else
#define SHOW(...)
#endif

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __VA_ARGS__; \
  read(__VA_ARGS__)
#define U32(...)   \
  u32 __VA_ARGS__; \
  read(__VA_ARGS__)
#define U64(...)   \
  u64 __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 "/home/maspy/compro/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 {
  static constexpr bool is_directed = directed;
  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; }

  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;
  }

#ifdef FASTIO
  // 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();
  }
#endif

  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];
  }

#ifdef FASTIO
  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);
    }
  }
#endif

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

  // G における頂点 V[i] が、新しいグラフで i になるようにする
  // {G, es}
  // sum(deg(v)) の計算量になっていて、
  // 新しいグラフの n+m より大きい可能性があるので注意
  Graph<T, directed> rearrange(vc<int> V, bool keep_eid = 0) {
    if (len(new_idx) != N) new_idx.assign(N, -1);
    int n = len(V);
    FOR(i, n) new_idx[V[i]] = i;
    Graph<T, directed> G(n);
    vc<int> history;
    FOR(i, n) {
      for (auto&& e: (*this)[V[i]]) {
        if (len(used_e) <= e.id) used_e.resize(e.id + 1);
        if (used_e[e.id]) continue;
        int a = e.frm, b = e.to;
        if (new_idx[a] != -1 && new_idx[b] != -1) {
          history.eb(e.id);
          used_e[e.id] = 1;
          int eid = (keep_eid ? e.id : -1);
          G.add(new_idx[a], new_idx[b], e.cost, eid);
        }
      }
    }
    FOR(i, n) new_idx[V[i]] = -1;
    for (auto&& eid: history) used_e[eid] = 0;
    G.build();
    return G;
  }

  Graph<T, true> to_directed_tree(int root = -1) {
    if (root == -1) root = 0;
    assert(!is_directed && prepared && M == N - 1);
    Graph<T, true> G1(N);
    vc<int> par(N, -1);
    auto dfs = [&](auto& dfs, int v) -> void {
      for (auto& e: (*this)[v]) {
        if (e.to == par[v]) continue;
        par[e.to] = v, dfs(dfs, e.to);
      }
    };
    dfs(dfs, root);
    for (auto& e: edges) {
      int a = e.frm, b = e.to;
      if (par[a] == b) swap(a, b);
      assert(par[b] == a);
      G1.add(a, b, e.cost);
    }
    G1.build();
    return G1;
  }

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 2 "/home/maspy/compro/library/graph/tree.hpp"

#line 4 "/home/maspy/compro/library/graph/tree.hpp"

// HLD euler tour をとっていろいろ。
template <typename GT>
struct Tree {
  using Graph_type = GT;
  GT &G;
  using WT = typename GT::cost_type;
  int N;
  vector<int> LID, RID, head, V, parent, VtoE;
  vc<int> depth;
  vc<WT> depth_weighted;

  Tree(GT &G, int r = 0, bool hld = 1) : G(G) { build(r, hld); }

  void build(int r = 0, bool hld = 1) {
    if (r == -1) return; // build を遅延したいとき
    N = G.N;
    LID.assign(N, -1), RID.assign(N, -1), head.assign(N, r);
    V.assign(N, -1), parent.assign(N, -1), VtoE.assign(N, -1);
    depth.assign(N, -1), depth_weighted.assign(N, 0);
    assert(G.is_prepared());
    int t1 = 0;
    dfs_sz(r, -1, hld);
    dfs_hld(r, t1);
  }

  void dfs_sz(int v, int p, bool hld) {
    auto &sz = RID;
    parent[v] = p;
    depth[v] = (p == -1 ? 0 : depth[p] + 1);
    sz[v] = 1;
    int l = G.indptr[v], r = G.indptr[v + 1];
    auto &csr = G.csr_edges;
    // 使う辺があれば先頭にする
    for (int i = r - 2; i >= l; --i) {
      if (hld && depth[csr[i + 1].to] == -1) swap(csr[i], csr[i + 1]);
    }
    int hld_sz = 0;
    for (int i = l; i < r; ++i) {
      auto e = csr[i];
      if (depth[e.to] != -1) continue;
      depth_weighted[e.to] = depth_weighted[v] + e.cost;
      VtoE[e.to] = e.id;
      dfs_sz(e.to, v, hld);
      sz[v] += sz[e.to];
      if (hld && chmax(hld_sz, sz[e.to]) && l < i) { swap(csr[l], csr[i]); }
    }
  }

  void dfs_hld(int v, int &times) {
    LID[v] = times++;
    RID[v] += LID[v];
    V[LID[v]] = v;
    bool heavy = true;
    for (auto &&e: G[v]) {
      if (depth[e.to] <= depth[v]) continue;
      head[e.to] = (heavy ? head[v] : e.to);
      heavy = false;
      dfs_hld(e.to, times);
    }
  }

  vc<int> heavy_path_at(int v) {
    vc<int> P = {v};
    while (1) {
      int a = P.back();
      for (auto &&e: G[a]) {
        if (e.to != parent[a] && head[e.to] == v) {
          P.eb(e.to);
          break;
        }
      }
      if (P.back() == a) break;
    }
    return P;
  }

  int heavy_child(int v) {
    int k = LID[v] + 1;
    if (k == N) return -1;
    int w = V[k];
    return (parent[w] == v ? w : -1);
  }

  int e_to_v(int eid) {
    auto e = G.edges[eid];
    return (parent[e.frm] == e.to ? e.frm : e.to);
  }
  int v_to_e(int v) { return VtoE[v]; }
  int get_eid(int u, int v) {
    if (parent[u] != v) swap(u, v);
    assert(parent[u] == v);
    return VtoE[u];
  }

  int ELID(int v) { return 2 * LID[v] - depth[v]; }
  int ERID(int v) { return 2 * RID[v] - depth[v] - 1; }

  // 目標地点へ進む個数が k
  int LA(int v, int k) {
    assert(k <= depth[v]);
    while (1) {
      int u = head[v];
      if (LID[v] - k >= LID[u]) return V[LID[v] - k];
      k -= LID[v] - LID[u] + 1;
      v = parent[u];
    }
  }
  int la(int u, int v) { return LA(u, v); }

  int LCA(int u, int v) {
    for (;; v = parent[head[v]]) {
      if (LID[u] > LID[v]) swap(u, v);
      if (head[u] == head[v]) return u;
    }
  }

  int meet(int a, int b, int c) { return LCA(a, b) ^ LCA(a, c) ^ LCA(b, c); }
  int lca(int u, int v) { return LCA(u, v); }

  int subtree_size(int v, int root = -1) {
    if (root == -1) return RID[v] - LID[v];
    if (v == root) return N;
    int x = jump(v, root, 1);
    if (in_subtree(v, x)) return RID[v] - LID[v];
    return N - RID[x] + LID[x];
  }

  int dist(int a, int b) {
    int c = LCA(a, b);
    return depth[a] + depth[b] - 2 * depth[c];
  }

  WT dist_weighted(int a, int b) {
    int c = LCA(a, b);
    return depth_weighted[a] + depth_weighted[b] - WT(2) * depth_weighted[c];
  }

  // a is in b
  bool in_subtree(int a, int b) { return LID[b] <= LID[a] && LID[a] < RID[b]; }

  int jump(int a, int b, ll k) {
    if (k == 1) {
      if (a == b) return -1;
      return (in_subtree(b, a) ? LA(b, depth[b] - depth[a] - 1) : parent[a]);
    }
    int c = LCA(a, b);
    int d_ac = depth[a] - depth[c];
    int d_bc = depth[b] - depth[c];
    if (k > d_ac + d_bc) return -1;
    if (k <= d_ac) return LA(a, k);
    return LA(b, d_ac + d_bc - k);
  }

  vc<int> collect_child(int v) {
    vc<int> res;
    for (auto &&e: G[v])
      if (e.to != parent[v]) res.eb(e.to);
    return res;
  }

  vc<int> collect_light(int v) {
    vc<int> res;
    bool skip = true;
    for (auto &&e: G[v])
      if (e.to != parent[v]) {
        if (!skip) res.eb(e.to);
        skip = false;
      }
    return res;
  }

  vc<pair<int, int>> get_path_decomposition(int u, int v, bool edge) {
    // [始点, 終点] の"閉"区間列。
    vc<pair<int, int>> up, down;
    while (1) {
      if (head[u] == head[v]) break;
      if (LID[u] < LID[v]) {
        down.eb(LID[head[v]], LID[v]);
        v = parent[head[v]];
      } else {
        up.eb(LID[u], LID[head[u]]);
        u = parent[head[u]];
      }
    }
    if (LID[u] < LID[v]) down.eb(LID[u] + edge, LID[v]);
    elif (LID[v] + edge <= LID[u]) up.eb(LID[u], LID[v] + edge);
    reverse(all(down));
    up.insert(up.end(), all(down));
    return up;
  }

  // 辺の列の情報 (frm,to,str)
  // str = "heavy_up", "heavy_down", "light_up", "light_down"
  vc<tuple<int, int, string>> get_path_decomposition_detail(int u, int v) {
    vc<tuple<int, int, string>> up, down;
    while (1) {
      if (head[u] == head[v]) break;
      if (LID[u] < LID[v]) {
        if (v != head[v]) down.eb(head[v], v, "heavy_down"), v = head[v];
        down.eb(parent[v], v, "light_down"), v = parent[v];
      } else {
        if (u != head[u]) up.eb(u, head[u], "heavy_up"), u = head[u];
        up.eb(u, parent[u], "light_up"), u = parent[u];
      }
    }
    if (LID[u] < LID[v]) down.eb(u, v, "heavy_down");
    elif (LID[v] < LID[u]) up.eb(u, v, "heavy_up");
    reverse(all(down));
    concat(up, down);
    return up;
  }

  vc<int> restore_path(int u, int v) {
    vc<int> P;
    for (auto &&[a, b]: get_path_decomposition(u, v, 0)) {
      if (a <= b) {
        FOR(i, a, b + 1) P.eb(V[i]);
      } else {
        FOR_R(i, b, a + 1) P.eb(V[i]);
      }
    }
    return P;
  }

  // path [a,b] と [c,d] の交わり. 空ならば {-1,-1}.
  // https://codeforces.com/problemset/problem/500/G
  pair<int, int> path_intersection(int a, int b, int c, int d) {
    int ab = lca(a, b), ac = lca(a, c), ad = lca(a, d);
    int bc = lca(b, c), bd = lca(b, d), cd = lca(c, d);
    int x = ab ^ ac ^ bc, y = ab ^ ad ^ bd; // meet(a,b,c), meet(a,b,d)
    if (x != y) return {x, y};
    int z = ac ^ ad ^ cd;
    if (x != z) x = -1;
    return {x, x};
  }
};
#line 4 "/home/maspy/compro/library/graph/tree_dp/rerooting_dp.hpp"

template <typename TREE, typename Data>
struct Rerooting_dp {
  static_assert(!TREE::Graph_type::is_directed);
  TREE& tree;
  vc<Data> dp_1; // 辺 pv に対して、部分木 v
  vc<Data> dp_2; // 辺 pv に対して、部分木 p
  vc<Data> dp;   // full tree

  template <typename F1, typename F2, typename F3>
  Rerooting_dp(TREE& tree, F1 f_ee, F2 f_ev, F3 f_ve, const Data unit)
      : tree(tree) {
    build(f_ee, f_ev, f_ve, unit);
  }

  // v を根としたときの full tree
  Data operator[](int v) { return dp[v]; }

  // root を根としたときの部分木 v
  Data get(int v, int root) {
    if (root == v) return dp[v];
    if (!tree.in_subtree(root, v)) { return dp_1[v]; }
    int w = tree.jump(v, root, 1);
    return dp_2[w];
  }

  template <typename F1, typename F2, typename F3>
  void build(F1 f_ee, F2 f_ev, F3 f_ve, const Data unit) {
    int N = tree.N;
    // dp1: subtree
    dp_1.assign(N, unit);
    FOR_R(i, N) {
      int v = tree.V[i];
      for (auto&& e: tree.G[v]) {
        if (e.to == tree.parent[v]) continue;
        dp_1[v] = f_ee(dp_1[v], f_ve(dp_1[e.to], e));
      }
      dp_1[v] = f_ev(dp_1[v], v);
    }

    // dp2[v]: subtree of p, rooted at v
    dp_2.assign(N, unit);
    // dp[v]: fulltree, rooted at v
    dp.assign(N, unit);
    FOR(i, N) {
      int p = tree.V[i];
      vc<int> ch;
      vc<Data> ch_data;
      Data x = unit;
      for (auto&& e: tree.G[p]) {
        if (e.to == tree.parent[p]) {
          x = f_ve(dp_2[p], e);
        } else {
          ch.eb(e.to);
          ch_data.eb(f_ve(dp_1[e.to], e));
        }
      }
      int n = len(ch);
      if (!n) {
        dp[p] = f_ev(x, p);
        continue;
      }
      vc<Data> prod_left(n, x);
      FOR(i, n - 1) prod_left[i + 1] = f_ee(prod_left[i], ch_data[i]);
      Data prod_right = unit;
      FOR_R(i, n) {
        dp_2[ch[i]] = f_ev(f_ee(prod_left[i], prod_right), p);
        prod_right = f_ee(prod_right, ch_data[i]);
      }
      dp[p] = f_ev(f_ee(x, prod_right), p);
    }
  }
};
#line 2 "/home/maspy/compro/library/mod/modint_common.hpp"

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

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

template <typename mint>
mint inv(int n) {
  static const int mod = mint::get_mod();
  static vector<mint> dat = {0, 1};
  assert(0 <= n);
  if (n >= mod) n %= mod;
  while (len(dat) <= n) {
    int k = len(dat);
    int q = (mod + k - 1) / k;
    dat.eb(dat[k * q - mod] * mint::raw(q));
  }
  return dat[n];
}

template <typename mint>
mint fact(int n) {
  static const int mod = mint::get_mod();
  assert(0 <= n && n < mod);
  static vector<mint> dat = {1, 1};
  while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * mint::raw(len(dat)));
  return dat[n];
}

template <typename mint>
mint fact_inv(int n) {
  static vector<mint> dat = {1, 1};
  if (n < 0) return mint(0);
  while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * inv<mint>(len(dat)));
  return dat[n];
}

template <class mint, class... Ts>
mint fact_invs(Ts... xs) {
  return (mint(1) * ... * fact_inv<mint>(xs));
}

template <typename mint, class Head, class... Tail>
mint multinomial(Head &&head, Tail &&... tail) {
  return fact<mint>(head) * fact_invs<mint>(std::forward<Tail>(tail)...);
}

template <typename mint>
mint C_dense(int n, int k) {
  static vvc<mint> C;
  static int H = 0, W = 0;
  auto calc = [&](int i, int j) -> mint {
    if (i == 0) return (j == 0 ? mint(1) : mint(0));
    return C[i - 1][j] + (j ? C[i - 1][j - 1] : 0);
  };
  if (W <= k) {
    FOR(i, H) {
      C[i].resize(k + 1);
      FOR(j, W, k + 1) { C[i][j] = calc(i, j); }
    }
    W = k + 1;
  }
  if (H <= n) {
    C.resize(n + 1);
    FOR(i, H, n + 1) {
      C[i].resize(W);
      FOR(j, W) { C[i][j] = calc(i, j); }
    }
    H = n + 1;
  }
  return C[n][k];
}

template <typename mint, bool large = false, bool dense = false>
mint C(ll n, ll k) {
  assert(n >= 0);
  if (k < 0 || n < k) return 0;
  if constexpr (dense) return C_dense<mint>(n, k);
  if constexpr (!large) return multinomial<mint>(n, k, n - k);
  k = min(k, n - k);
  mint x(1);
  FOR(i, k) x *= mint(n - i);
  return x * fact_inv<mint>(k);
}

template <typename mint, bool large = false>
mint C_inv(ll n, ll k) {
  assert(n >= 0);
  assert(0 <= k && k <= n);
  if (!large) return fact_inv<mint>(n) * fact<mint>(k) * fact<mint>(n - k);
  return mint(1) / C<mint, 1>(n, k);
}

// [x^d](1-x)^{-n}
template <typename mint, bool large = false, bool dense = false>
mint C_negative(ll n, ll d) {
  assert(n >= 0);
  if (d < 0) return mint(0);
  if (n == 0) { return (d == 0 ? mint(1) : mint(0)); }
  return C<mint, large, dense>(n + d - 1, d);
}
#line 3 "/home/maspy/compro/library/mod/modint.hpp"

template <int mod>
struct modint {
  static constexpr u32 umod = u32(mod);
  static_assert(umod < u32(1) << 31);
  u32 val;

  static modint raw(u32 v) {
    modint x;
    x.val = v;
    return x;
  }
  constexpr modint() : val(0) {}
  constexpr modint(u32 x) : val(x % umod) {}
  constexpr modint(u64 x) : val(x % umod) {}
  constexpr modint(u128 x) : val(x % umod) {}
  constexpr modint(int x) : val((x %= mod) < 0 ? x + mod : x){};
  constexpr modint(ll x) : val((x %= mod) < 0 ? x + mod : x){};
  constexpr modint(i128 x) : val((x %= mod) < 0 ? x + mod : x){};
  bool operator<(const modint &other) const { return val < other.val; }
  modint &operator+=(const modint &p) {
    if ((val += p.val) >= umod) val -= umod;
    return *this;
  }
  modint &operator-=(const modint &p) {
    if ((val += umod - p.val) >= umod) val -= umod;
    return *this;
  }
  modint &operator*=(const modint &p) {
    val = u64(val) * p.val % umod;
    return *this;
  }
  modint &operator/=(const modint &p) {
    *this *= p.inverse();
    return *this;
  }
  modint operator-() const { return modint::raw(val ? mod - val : u32(0)); }
  modint operator+(const modint &p) const { return modint(*this) += p; }
  modint operator-(const modint &p) const { return modint(*this) -= p; }
  modint operator*(const modint &p) const { return modint(*this) *= p; }
  modint operator/(const modint &p) const { return modint(*this) /= p; }
  bool operator==(const modint &p) const { return val == p.val; }
  bool operator!=(const modint &p) const { return val != p.val; }
  modint inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b), swap(u -= t * v, v);
    }
    return modint(u);
  }
  modint pow(ll n) const {
    assert(n >= 0);
    modint ret(1), mul(val);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  static constexpr int get_mod() { return mod; }
  // (n, r), r は 1 の 2^n 乗根
  static constexpr pair<int, int> ntt_info() {
    if (mod == 120586241) return {20, 74066978};
    if (mod == 167772161) return {25, 17};
    if (mod == 469762049) return {26, 30};
    if (mod == 754974721) return {24, 362};
    if (mod == 880803841) return {23, 211};
    if (mod == 943718401) return {22, 663003469};
    if (mod == 998244353) return {23, 31};
    if (mod == 1004535809) return {21, 582313106};
    if (mod == 1012924417) return {21, 368093570};
    return {-1, -1};
  }
  static constexpr bool can_ntt() { return ntt_info().fi != -1; }
};

#ifdef FASTIO
template <int mod>
void rd(modint<mod> &x) {
  fastio::rd(x.val);
  x.val %= mod;
  // assert(0 <= x.val && x.val < mod);
}
template <int mod>
void wt(modint<mod> x) {
  fastio::wt(x.val);
}
#endif

using modint107 = modint<1000000007>;
using modint998 = modint<998244353>;
#line 2 "/home/maspy/compro/library/alg/monoid/add.hpp"

template <typename E>
struct Monoid_Add {
  using X = E;
  using value_type = X;
  static constexpr X op(const X &x, const X &y) noexcept { return x + y; }
  static constexpr X inverse(const X &x) noexcept { return -x; }
  static constexpr X power(const X &x, ll n) noexcept { return X(n) * x; }
  static constexpr X unit() { return X(0); }
  static constexpr bool commute = true;
};
#line 3 "/home/maspy/compro/library/ds/fenwicktree/fenwicktree.hpp"

template <typename Monoid>
struct FenwickTree {
  using G = Monoid;
  using MX = Monoid;
  using E = typename G::value_type;
  int n;
  vector<E> dat;
  E total;

  FenwickTree() {}
  FenwickTree(int n) { build(n); }
  template <typename F>
  FenwickTree(int n, F f) {
    build(n, f);
  }
  FenwickTree(const vc<E>& v) { build(v); }

  void build(int m) {
    n = m;
    dat.assign(m, G::unit());
    total = G::unit();
  }
  void build(const vc<E>& v) {
    build(len(v), [&](int i) -> E { return v[i]; });
  }
  template <typename F>
  void build(int m, F f) {
    n = m;
    dat.clear();
    dat.reserve(n);
    total = G::unit();
    FOR(i, n) { dat.eb(f(i)); }
    for (int i = 1; i <= n; ++i) {
      int j = i + (i & -i);
      if (j <= n) dat[j - 1] = G::op(dat[i - 1], dat[j - 1]);
    }
    total = prefix_sum(m);
  }

  E prod_all() { return total; }
  E sum_all() { return total; }
  E sum(int k) { return prefix_sum(k); }
  E prod(int k) { return prefix_prod(k); }
  E prefix_sum(int k) { return prefix_prod(k); }
  E prefix_prod(int k) {
    chmin(k, n);
    E ret = G::unit();
    for (; k > 0; k -= k & -k) ret = G::op(ret, dat[k - 1]);
    return ret;
  }
  E sum(int L, int R) { return prod(L, R); }
  E prod(int L, int R) {
    chmax(L, 0), chmin(R, n);
    if (L == 0) return prefix_prod(R);
    assert(0 <= L && L <= R && R <= n);
    E pos = G::unit(), neg = G::unit();
    while (L < R) { pos = G::op(pos, dat[R - 1]), R -= R & -R; }
    while (R < L) { neg = G::op(neg, dat[L - 1]), L -= L & -L; }
    return G::op(pos, G::inverse(neg));
  }

  vc<E> get_all() {
    vc<E> res(n);
    FOR(i, n) res[i] = prod(i, i + 1);
    return res;
  }

  void add(int k, E x) { multiply(k, x); }
  void multiply(int k, E x) {
    static_assert(G::commute);
    total = G::op(total, x);
    for (++k; k <= n; k += k & -k) dat[k - 1] = G::op(dat[k - 1], x);
  }
  void set(int k, E x) { add(k, G::op(G::inverse(prod(k, k + 1)), x)); }

  template <class F>
  int max_right(const F check, int L = 0) {
    assert(check(G::unit()));
    E s = G::unit();
    int i = L;
    // 2^k 進むとダメ
    int k = [&]() {
      while (1) {
        if (i % 2 == 1) { s = G::op(s, G::inverse(dat[i - 1])), i -= 1; }
        if (i == 0) { return topbit(n) + 1; }
        int k = lowbit(i) - 1;
        if (i + (1 << k) > n) return k;
        E t = G::op(s, dat[i + (1 << k) - 1]);
        if (!check(t)) { return k; }
        s = G::op(s, G::inverse(dat[i - 1])), i -= i & -i;
      }
    }();
    while (k) {
      --k;
      if (i + (1 << k) - 1 < len(dat)) {
        E t = G::op(s, dat[i + (1 << k) - 1]);
        if (check(t)) { i += (1 << k), s = t; }
      }
    }
    return i;
  }

  // check(i, x)
  template <class F>
  int max_right_with_index(const F check, int L = 0) {
    assert(check(L, G::unit()));
    E s = G::unit();
    int i = L;
    // 2^k 進むとダメ
    int k = [&]() {
      while (1) {
        if (i % 2 == 1) { s = G::op(s, G::inverse(dat[i - 1])), i -= 1; }
        if (i == 0) { return topbit(n) + 1; }
        int k = lowbit(i) - 1;
        if (i + (1 << k) > n) return k;
        E t = G::op(s, dat[i + (1 << k) - 1]);
        if (!check(i + (1 << k), t)) { return k; }
        s = G::op(s, G::inverse(dat[i - 1])), i -= i & -i;
      }
    }();
    while (k) {
      --k;
      if (i + (1 << k) - 1 < len(dat)) {
        E t = G::op(s, dat[i + (1 << k) - 1]);
        if (check(i + (1 << k), t)) { i += (1 << k), s = t; }
      }
    }
    return i;
  }

  template <class F>
  int min_left(const F check, int R) {
    assert(check(G::unit()));
    E s = G::unit();
    int i = R;
    // false になるところまで戻る
    int k = 0;
    while (i > 0 && check(s)) {
      s = G::op(s, dat[i - 1]);
      k = lowbit(i);
      i -= i & -i;
    }
    if (check(s)) {
      assert(i == 0);
      return 0;
    }
    // 2^k 進むと ok になる
    // false を維持して進む
    while (k) {
      --k;
      E t = G::op(s, G::inverse(dat[i + (1 << k) - 1]));
      if (!check(t)) { i += (1 << k), s = t; }
    }
    return i + 1;
  }

  int kth(E k, int L = 0) {
    return max_right([&k](E x) -> bool { return x <= k; }, L);
  }
};
#line 3 "/home/maspy/compro/library/graph/ds/tree_abelgroup.hpp"

template <typename TREE, typename AbelGroup, bool edge, bool path_query, bool subtree_query>
struct Tree_AbelGroup {
  using MX = AbelGroup;
  using X = typename MX::value_type;
  TREE &tree;
  int N;
  FenwickTree<MX> bit, bit_subtree;

  Tree_AbelGroup(TREE &tree) : tree(tree), N(tree.N) {
    build([](int i) -> X { return MX::unit(); });
  }

  Tree_AbelGroup(TREE &tree, vc<X> &dat) : tree(tree), N(tree.N) {
    build([&](int i) -> X { return dat[i]; });
  }

  template <typename F>
  Tree_AbelGroup(TREE &tree, F f) : tree(tree), N(tree.N) {
    build(f);
  }

  template <typename F>
  void build(F f) {
    vc<X> bit_raw_1(2 * N);
    vc<X> bit_raw_2(N);
    FOR(v, N) {
      X x = MX::unit();
      if (!edge) x = f(v);
      if (edge) x = (v == 0 ? MX::unit() : f(tree.v_to_e(v)));
      bit_raw_1[tree.ELID(v)] = x;
      bit_raw_1[tree.ERID(v)] = MX::inverse(x);
      bit_raw_2[tree.LID[v]] = x;
    }
    if constexpr (path_query) bit.build(bit_raw_1);
    if constexpr (subtree_query) bit_subtree.build(bit_raw_2);
  }

  void add(int i, X x) {
    int v = (edge ? tree.e_to_v(i) : i);
    if constexpr (path_query) {
      bit.add(tree.ELID(v), x);
      bit.add(tree.ERID(v), MX::inverse(x));
    }
    if constexpr (subtree_query) bit_subtree.add(tree.LID[v], x);
  }
  void multiply(int i, X x) { add(i, x); }
  X prod_path(int frm, int to) {
    static_assert(path_query);
    int lca = tree.LCA(frm, to);
    // [frm, lca)
    X x1 = bit.prod(tree.ELID(lca) + 1, tree.ELID(frm) + 1);
    // edge なら (lca, to]、vertex なら [lca, to]
    X x2 = bit.prod(tree.ELID(lca) + edge, tree.ELID(to) + 1);
    return MX::op(x1, x2);
  }

  X prod_subtree(int u, int root = -1) {
    static_assert(subtree_query);
    int l = tree.LID[u], r = tree.RID[u];
    if (root == -1) return bit_subtree.prod(l + edge, r);
    if (root == u) return bit_subtree.prod_all();
    if (tree.in_subtree(u, root)) return bit_subtree.prod(l + edge, r);
    return MX::op(bit_subtree.prod(0, l + 1), bit_subtree.prod(r, N));
  }
};
#line 7 "main.cpp"

// 点や辺を含む連結集合を数えよう
void solve() {
  LL(N, Q);
  Graph<int, 0> G(N);
  FOR(v, 1, N) {
    INT(p);
    --p;
    G.add(p, v);
  }
  G.build();
  Tree<decltype(G)> tree(G);

  using mint = modint998;
  using Data = mint;
  Data unit = 1;
  auto fee = [&](Data x, Data y) -> Data { return x * y; };
  auto fev = [&](Data x, int v) -> Data { return x; };
  // e は v に入る有向辺
  auto fve = [&](Data x, auto& e) -> Data { return x + 1; };
  Rerooting_dp<decltype(tree), Data> dp(tree, fee, fev, fve, unit);

  vc<mint> A(N), B(N - 1);
  FOR(v, N) { A[v] = dp.get(v, v); }
  FOR(i, N - 1) {
    int a = G.edges[i].frm;
    int b = G.edges[i].to;
    mint ans = dp.get(a, a);
    ans -= dp.get(a, b);
    assert(ans == dp.get(b, b) - dp.get(b, a));
    B[i] = ans;
  }
  SHOW(B);

  Tree_AbelGroup<decltype(tree), Monoid_Add<mint>, false, 1, 0> Xv(tree, A);
  Tree_AbelGroup<decltype(tree), Monoid_Add<mint>, true, 1, 0> Xe(tree, B);

  FOR(Q) {
    INT(a, b);
    --a, --b;
    mint x = Xv.prod_path(a, b);
    mint y = Xe.prod_path(a, b);
    print(x - y);
  }
}

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

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

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

input:

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

output:

6
5
14
13
15

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 76ms
memory: 3948kb

input:

3000
98 100
1 2 1 3 5 3 2 5 5 4 3 7 12 10 12 8 10 4 4 3 10 14 11 11 22 23 14 20 29 1 18 7 12 29 20 29 12 21 6 20 3 25 7 21 16 44 38 44 7 11 5 24 34 24 41 48 56 58 56 3 26 55 62 33 9 38 63 39 3 67 14 24 60 35 1 22 74 36 57 61 55 46 44 12 16 60 44 50 22 58 78 15 57 57 75 88 15
43 28
67 66
3 39
6 19
84...

output:

964062690
949799024
949777463
964050185
119859605
949794873
949799267
964064991
836980963
964045750
964065023
959849545
536098301
964045791
964064966
964046253
964052677
949782329
964050627
949794848
188617843
964065041
2617316
949782330
964046253
536098346
949777935
964052584
949777939
964046254
94...

result:

ok 300000 lines

Test #3:

score: 0
Accepted
time: 83ms
memory: 4060kb

input:

300
998 1000
1 2 1 3 3 2 2 8 5 2 8 8 12 3 13 3 7 8 16 14 10 22 10 1 24 17 16 1 16 21 2 23 2 1 20 11 1 1 22 19 5 15 10 37 15 39 13 16 33 37 37 36 37 16 3 45 10 28 14 4 16 17 55 6 6 5 31 67 51 35 47 48 10 16 75 21 45 71 28 64 39 9 37 5 65 79 28 84 29 79 21 50 21 16 93 72 58 35 30 14 86 90 60 65 33 47 ...

output:

327306708
121504060
970333956
71256467
492200713
164920447
56359491
54857868
62271655
175858304
373532115
138628785
54854112
616763633
41337286
837501264
861536431
572242958
417784906
22152900
460075855
89587278
985881197
291627546
96610921
437457168
101307362
180803897
373532108
80109336
837492247
...

result:

ok 300000 lines

Test #4:

score: 0
Accepted
time: 119ms
memory: 23716kb

input:

3
100000 100000
1 1 2 4 2 4 6 5 1 8 9 7 12 10 7 12 10 4 9 7 6 22 10 5 18 3 8 18 5 20 12 26 10 11 14 5 28 29 33 12 5 10 30 21 36 24 1 26 39 29 2 42 40 33 41 39 23 2 50 11 47 61 61 52 3 27 65 4 24 1 15 41 68 5 62 1 44 60 44 79 68 6 53 72 21 58 66 24 54 78 29 39 75 74 13 52 71 35 40 85 47 19 60 44 101 ...

output:

174648911
988966670
586060443
352691812
610467698
718056854
353397034
944134980
945506609
743772159
17398768
898225958
509929535
581516662
124983919
679181027
890516256
792976265
81256963
846568565
990778256
394490295
307247131
281874314
78565559
438162317
218440246
677940950
608561943
237178689
748...

result:

ok 300000 lines

Test #5:

score: 0
Accepted
time: 116ms
memory: 23780kb

input:

3
100000 100000
1 2 3 2 2 3 4 3 7 6 10 8 6 4 3 6 5 8 3 17 2 7 1 1 17 3 4 11 25 26 5 7 2 20 2 32 35 11 9 39 16 42 26 43 29 22 35 18 3 34 45 19 27 3 41 27 10 14 4 4 45 53 35 49 57 37 24 43 68 6 44 5 15 12 62 22 11 26 37 41 8 73 56 76 78 9 46 14 81 67 49 12 74 15 16 69 86 48 47 77 58 77 87 54 79 80 99 ...

output:

761112418
717651384
861477152
134730845
623546487
488508714
852403783
522543884
880846196
809656417
876270841
575462796
111884802
845956357
990889899
222833220
7564761
917539269
355409810
261089607
166264493
612109684
526575279
410009284
848925228
885468503
90907188
969960703
663719627
309794696
503...

result:

ok 300000 lines

Test #6:

score: 0
Accepted
time: 27ms
memory: 17612kb

input:

3
100000 100000
1 2 1 2 3 5 1 4 2 6 10 3 10 6 13 14 4 18 13 4 21 19 9 16 14 5 13 15 10 29 11 4 33 31 25 15 19 32 12 17 22 24 35 38 3 1 46 28 16 8 13 5 46 44 25 49 37 30 47 37 10 56 32 29 36 7 13 47 11 2 24 6 51 65 36 52 15 74 62 65 25 34 35 61 12 4 20 81 64 39 21 79 90 29 68 30 7 34 60 1 1 1 1 1 1 1...

output:

47613014
867989885
314355471
515168737
161160818
552527
328577529
705173752
262933227
431743363
481168259
545043565
319743713
655278418
20733052
900938971
104104163
575553196
635937653
545910595
298966873
864807486
817091004
10697715
66840685
327639210
80580410
489368876
303238352
545043565
26533356...

result:

ok 300000 lines

Test #7:

score: 0
Accepted
time: 121ms
memory: 16528kb

input:

10000
10 10
1 2 2 4 4 3 4 7 9
5 4
6 7
1 9
9 5
2 2
2 1
4 5
5 5
4 6
7 6
10 10
1 1 2 4 3 4 7 5 6
10 7
7 5
10 2
10 1
4 6
1 4
1 9
8 9
4 2
10 5
9 9
1 2 1 4 2 5 2 5
5 6
9 4
4 4
3 4
6 9
8 2
7 9
9 4
1 3
10 10
1 1 2 4 4 2 7 6 1
2 10
3 10
2 6
8 3
4 8
5 6
8 5
3 6
6 10
4 7
9 9
1 2 1 3 5 2 5 2
7 8
9 6
7 2
2 3
2 6...

output:

89
106
100
108
90
91
89
45
89
106
71
58
60
50
68
63
66
60
59
71
72
55
50
68
73
57
46
55
63
110
90
113
113
114
99
115
118
118
113
83
83
73
77
82
57
82
57
83
90
91
92
92
91
59
91
93
88
66
79
69
80
72
66
66
63
33
171
169
169
170
106
160
159
157
168
169
56
91
23
95
86
80
87
91
92
89
40
34
41
37
31
37
33...

result:

ok 290206 lines

Test #8:

score: 0
Accepted
time: 88ms
memory: 23264kb

input:

10000
9 9
1 2 2 4 2 5 1 7
1 2
4 7
5 6
5 7
7 4
8 5
5 8
7 4
8 5
10 10
1 2 1 1 5 2 1 7 3
5 3
5 9
5 6
10 9
3 5
2 6
6 6
8 10
7 7
6 6
10 10
1 1 2 4 5 1 2 3 7
3 5
4 7
5 9
7 8
1 3
5 8
1 9
6 9
8 6
7 10
10 10
1 2 3 1 1 1 3 3 2
1 1
3 10
10 6
4 10
4 10
3 2
8 9
6 8
6 3
8 9
10 10
1 2 1 3 4 6 3 7 5
10 6
10 7
3 6
5...

output:

62
57
68
44
57
70
70
57
70
133
134
83
123
133
132
42
133
80
42
96
94
97
92
83
86
84
98
87
57
152
171
172
172
172
170
154
180
179
154
63
65
60
30
46
49
42
63
62
54
97
110
98
114
114
115
98
110
112
112
170
171
170
158
160
170
171
79
169
160
74
74
77
73
75
76
26
76
112
110
112
100
112
91
99
99
58
99
36...

result:

ok 290080 lines

Test #9:

score: 0
Accepted
time: 76ms
memory: 17188kb

input:

10000
9 9
1 2 3 1 3 6 3 6
2 1
9 1
7 9
7 7
6 6
2 7
6 8
3 6
4 6
10 10
1 1 3 3 5 2 7 3 6
8 7
4 9
3 10
10 8
9 4
5 1
9 9
4 6
8 5
4 2
9 9
1 1 2 4 4 5 4 4
5 2
9 8
2 2
8 1
3 5
6 1
9 3
2 8
4 6
8 8
1 2 3 1 2 5 2
1 6
8 4
7 1
8 4
8 3
7 3
8 1
1 5
9 9
1 2 1 3 5 3 7 1
7 2
6 3
4 3
7 7
9 7
4 4
4 4
8 6
4 4
10 10
1 1 ...

output:

65
90
70
35
68
88
85
84
85
39
82
86
96
82
87
41
86
93
88
101
98
75
102
104
102
103
100
97
52
52
42
52
51
56
52
41
61
57
64
38
66
23
23
60
23
87
116
86
115
118
136
87
135
129
115
123
130
82
120
82
119
120
133
132
129
85
89
87
90
41
85
86
45
63
53
48
23
46
35
45
47
41
36
58
64
62
62
61
67
20
69
56
56
...

result:

ok 289972 lines

Test #10:

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

input:

10000
9 9
1 2 2 1 2 3 1 1
3 3
3 3
2 7
8 2
3 5
1 1
9 2
3 1
7 5
8 8
1 1 2 4 5 1 7
6 6
6 2
4 8
4 7
2 4
8 7
2 1
6 5
9 9
1 2 2 2 1 4 2 1
5 2
3 1
8 5
8 2
1 5
7 2
2 5
4 3
8 1
9 9
1 2 1 4 4 4 2 8
5 7
1 2
7 7
7 4
9 2
7 7
8 2
3 6
8 5
9 9
1 1 2 4 3 6 7 5
2 2
7 8
3 5
7 2
6 4
7 3
5 9
6 6
2 7
10 10
1 2 1 2 4 6 6 ...

output:

74
74
111
117
119
104
117
118
120
10
34
40
39
31
23
34
19
121
125
122
121
125
123
121
123
125
66
69
33
65
63
33
62
79
80
24
17
38
38
39
29
17
21
38
66
74
70
50
68
69
45
70
45
81
95
103
96
100
75
75
75
61
100
91
87
92
92
86
43
55
86
82
80
95
100
102
49
25
108
71
94
104
89
92
98
91
94
93
96
63
63
98
6...

result:

ok 289954 lines

Test #11:

score: 0
Accepted
time: 102ms
memory: 23396kb

input:

10000
9 9
1 1 3 1 4 3 5 7
7 7
7 6
2 3
2 6
2 7
4 1
6 5
6 7
6 7
8 8
1 1 2 1 5 6 2
8 2
8 3
1 1
6 8
1 4
2 8
3 5
8 4
8 8
1 2 1 4 3 1 5
3 2
5 5
7 8
4 4
6 7
2 8
2 1
1 3
9 9
1 2 3 2 3 3 3 7
9 9
4 4
4 3
9 9
8 6
3 8
6 1
1 1
7 6
8 8
1 1 3 3 2 2 1
4 1
7 1
4 7
2 8
2 2
8 5
4 8
1 5
10 10
1 2 2 3 4 2 3 6 7
3 5
10 3...

output:

44
68
70
73
72
71
74
68
68
37
46
40
50
45
37
44
38
29
20
39
27
39
41
35
37
42
61
121
42
122
121
126
51
123
55
55
60
55
44
56
56
55
101
127
51
83
127
125
127
131
123
127
44
132
135
118
79
136
128
137
137
128
112
92
99
92
111
108
106
106
114
112
55
56
51
60
55
54
55
56
52
22
42
53
31
44
45
45
50
45
40...

result:

ok 289847 lines

Test #12:

score: 0
Accepted
time: 97ms
memory: 16580kb

input:

10000
8 8
1 1 3 3 1 4 2
2 5
1 3
7 1
2 7
7 8
1 6
5 6
4 4
8 8
1 2 1 3 4 6 5
4 3
5 3
6 4
6 4
5 5
2 8
4 8
7 2
9 9
1 2 3 3 1 2 3 4
5 6
1 3
2 8
5 2
7 5
6 1
8 3
8 5
2 8
9 9
1 1 1 2 4 6 3 3
3 9
6 2
6 5
6 3
6 6
4 5
4 5
9 4
3 4
8 8
1 1 3 4 2 5 1
1 2
7 2
4 2
8 3
1 7
5 1
2 4
3 6
9 9
1 2 1 4 5 1 3 5
9 6
1 9
7 4
...

output:

51
48
51
53
54
43
50
30
30
20
20
20
14
26
33
30
94
92
91
91
92
55
85
86
91
53
67
68
69
34
66
66
68
67
32
42
39
35
40
39
39
37
42
58
54
50
49
42
61
51
53
18
88
68
78
85
36
81
63
24
84
28
40
31
39
38
24
38
30
84
73
75
57
82
78
57
65
84
68
70
53
70
53
67
68
50
71
74
72
23
31
45
66
73
65
22
108
101
102
...

result:

ok 290127 lines

Test #13:

score: 0
Accepted
time: 109ms
memory: 19956kb

input:

10000
9 9
1 1 3 2 3 1 1 3
9 8
3 9
5 8
1 9
6 4
7 3
2 2
8 8
2 5
9 9
1 2 1 3 5 4 7 6
9 5
2 3
6 1
4 1
2 9
1 1
8 5
5 8
5 1
9 9
1 1 2 1 4 1 1 2
8 3
9 1
4 6
6 1
9 4
1 9
7 6
3 3
9 2
8 8
1 2 3 2 1 3 1
5 6
8 5
6 7
3 6
2 8
2 7
4 2
5 3
9 9
1 1 3 1 2 6 1 2
2 1
1 4
5 9
1 4
3 6
6 9
7 4
7 7
5 4
8 8
1 2 1 3 5 2 4
3 ...

output:

118
105
112
117
106
117
74
55
75
24
29
38
27
35
24
42
42
36
114
119
71
121
105
119
122
57
103
56
56
60
59
55
55
55
55
90
87
92
87
94
81
96
28
88
38
38
21
37
21
44
35
21
37
42
48
33
46
42
48
14
39
32
95
97
39
78
94
63
96
106
86
34
100
104
97
85
99
113
106
71
47
65
70
67
39
60
63
64
60
74
53
60
52
60
...

result:

ok 289926 lines

Test #14:

score: 0
Accepted
time: 88ms
memory: 23388kb

input:

10000
8 8
1 2 1 2 4 2 7
4 5
2 6
5 8
1 1
7 7
6 5
2 1
4 4
10 10
1 1 2 2 5 6 5 3 6
3 8
10 8
8 3
9 3
5 5
1 1
7 2
2 9
7 2
2 10
8 8
1 1 1 1 3 3 6
5 7
8 8
4 5
4 4
5 2
4 2
4 2
3 3
9 9
1 1 1 4 5 6 6 5
1 3
1 3
6 3
9 3
3 6
1 2
5 9
8 5
8 1
8 8
1 2 1 3 5 3 6
1 5
7 3
5 2
5 7
1 5
1 3
5 5
5 7
9 9
1 2 3 1 2 1 7 2
7 ...

output:

54
54
52
39
34
55
51
28
104
96
104
49
90
69
103
94
103
103
64
20
58
29
58
58
58
54
49
49
74
71
74
49
61
65
74
40
33
38
36
40
37
27
36
93
87
94
79
90
28
93
88
93
52
43
49
45
45
43
31
54
48
52
31
22
53
52
50
46
104
107
100
94
102
96
108
105
104
96
112
92
115
94
93
62
111
92
107
62
32
44
44
33
42
23
44...

result:

ok 289769 lines

Test #15:

score: 0
Accepted
time: 85ms
memory: 23104kb

input:

10000
8 8
1 2 1 3 4 6 4
1 8
8 5
7 7
5 4
1 6
3 1
8 7
1 8
9 9
1 2 3 2 5 1 7 4
4 8
9 6
3 9
4 8
4 7
3 9
5 2
6 1
2 6
8 8
1 2 2 4 2 4 6
6 5
1 1
8 4
2 6
7 4
7 8
1 2
1 6
9 9
1 1 1 4 4 2 1 5
2 7
1 4
4 4
8 3
5 2
6 7
2 6
7 6
6 9
10 10
1 1 2 1 1 4 3 2 6
3 7
2 5
10 9
3 5
5 7
1 7
1 10
1 9
8 3
3 6
9 9
1 1 3 2 5 3 ...

output:

35
41
12
40
36
33
34
35
59
57
42
59
58
42
50
54
51
67
31
67
62
53
68
61
63
59
90
78
86
94
94
93
94
82
137
133
136
129
136
135
129
133
87
130
78
78
71
64
80
65
64
71
65
164
168
165
164
164
167
167
167
166
163
36
40
36
37
37
37
39
38
95
92
92
86
94
57
92
87
90
80
29
57
55
60
59
57
51
57
54
64
39
59
64...

result:

ok 290030 lines

Test #16:

score: 0
Accepted
time: 89ms
memory: 20592kb

input:

10000
8 8
1 2 1 4 5 6 7
8 8
7 5
3 5
5 5
8 4
4 6
1 7
1 2
8 8
1 1 3 3 2 5 4
4 3
7 1
8 5
6 3
2 8
5 7
4 4
2 8
8 8
1 1 1 3 1 4 2
2 5
7 1
7 8
5 6
5 8
4 8
7 8
7 1
9 9
1 1 2 3 2 2 1 7
6 6
7 6
4 7
3 5
7 9
8 6
2 5
6 4
4 2
9 9
1 2 3 3 2 5 2 5
8 5
8 5
6 6
9 6
3 1
4 2
4 7
7 5
9 7
8 8
1 2 3 4 4 2 1
3 8
3 5
5 2
3 ...

output:

8
25
30
20
30
27
32
20
38
42
41
42
44
27
26
44
59
57
60
58
60
59
60
57
43
87
87
55
59
92
93
86
85
103
103
45
104
99
99
96
77
78
44
40
46
42
43
48
33
49
62
66
45
45
31
70
62
64
67
34
80
86
78
75
48
86
80
84
82
71
64
70
69
68
63
58
68
62
138
145
154
156
151
114
157
151
113
158
51
31
51
22
49
51
50
44
...

result:

ok 289869 lines

Test #17:

score: 0
Accepted
time: 118ms
memory: 16504kb

input:

10000
8 8
1 1 2 3 3 4 6
7 8
1 6
2 3
5 8
2 1
2 5
8 4
2 5
8 8
1 2 3 2 4 2 4
4 2
7 8
5 8
7 4
6 4
6 4
7 4
1 7
8 8
1 1 2 2 2 4 3
6 3
3 2
5 1
7 5
6 1
2 5
3 2
7 8
8 8
1 1 2 3 3 5 7
6 8
8 8
4 1
1 6
7 3
8 2
8 1
7 2
8 8
1 2 3 3 2 1 7
8 3
1 4
6 5
8 2
3 3
3 6
1 3
5 7
9 9
1 2 1 3 2 5 5 8
3 7
9 6
5 9
1 1
4 2
3 1
...

output:

43
36
37
34
31
38
42
38
57
59
59
58
41
41
58
50
54
53
52
52
52
49
53
57
39
11
30
36
37
43
41
42
50
48
46
46
36
45
47
50
56
65
51
34
51
57
56
55
48
23
65
66
74
74
69
74
23
63
116
113
119
66
112
116
99
105
116
113
59
53
25
59
41
41
49
59
116
110
125
110
83
110
123
109
111
122
79
74
79
79
75
51
51
51
5...

result:

ok 289830 lines

Test #18:

score: 0
Accepted
time: 76ms
memory: 23464kb

input:

10000
8 8
1 2 2 4 3 3 4
4 7
7 7
5 1
4 7
6 4
3 6
3 1
2 6
10 10
1 1 1 4 1 5 3 1 9
10 10
4 9
9 2
3 3
8 1
7 1
4 8
1 10
8 7
3 9
8 8
1 2 2 2 5 6 5
6 5
1 8
8 6
3 5
4 8
8 7
2 1
3 7
8 8
1 2 2 4 4 3 3
8 6
1 7
2 6
1 6
8 3
3 2
3 2
8 4
8 8
1 2 2 1 2 3 2
8 1
7 6
5 7
8 5
1 5
1 2
2 2
3 3
8 8
1 1 1 2 3 6 4
6 6
1 4
1...

output:

59
23
56
59
59
45
55
55
50
149
147
98
147
150
150
147
153
148
56
64
57
63
64
58
57
66
60
56
55
56
45
54
54
59
75
76
78
76
51
74
72
50
22
38
39
44
43
39
22
27
41
21
41
40
41
49
51
47
126
109
135
122
135
129
126
133
51
135
135
135
101
140
135
135
140
134
137
134
45
59
45
56
60
60
56
45
220
219
74
218
...

result:

ok 290002 lines

Test #19:

score: 0
Accepted
time: 81ms
memory: 17140kb

input:

10000
10 10
1 2 1 1 3 5 6 6 1
5 9
1 3
6 6
1 5
8 10
9 5
9 9
5 10
3 4
9 8
10 10
1 1 3 4 1 1 1 2 9
5 2
1 1
4 1
7 1
7 2
9 4
9 2
3 4
7 10
7 3
10 10
1 2 3 3 4 3 7 3 7
2 2
2 3
2 10
8 6
9 1
10 4
1 6
7 5
4 2
9 6
8 8
1 2 3 1 5 4 5
2 7
1 8
8 7
1 3
2 3
7 5
1 4
5 6
8 8
1 1 3 2 1 4 4
2 2
8 5
8 1
2 6
3 7
5 2
2 3
5...

output:

102
95
60
86
101
102
31
87
96
62
137
128
133
129
132
138
101
101
135
132
122
182
187
188
184
187
186
185
184
184
30
30
40
32
27
39
34
25
26
49
46
39
40
27
43
14
57
66
64
54
62
38
59
67
59
110
113
75
75
111
112
75
111
113
83
97
23
93
95
94
83
86
86
84
156
144
159
69
156
159
143
156
145
73
168
165
168...

result:

ok 289994 lines

Test #20:

score: 0
Accepted
time: 96ms
memory: 19876kb

input:

10000
9 9
1 2 3 4 5 2 2 1
2 4
1 9
9 2
2 4
7 4
8 5
9 5
7 3
1 5
8 8
1 2 1 1 5 2 2
8 1
5 3
5 3
5 2
1 1
3 1
3 4
7 3
9 9
1 1 3 1 1 2 2 7
6 5
2 9
2 8
3 5
1 4
5 7
8 2
1 1
7 6
9 9
1 2 1 2 5 1 1 5
3 1
9 6
1 7
1 5
4 9
4 9
8 1
1 3
3 4
8 8
1 1 2 3 3 6 5
5 5
6 3
7 2
1 4
3 4
2 4
3 1
8 4
10 10
1 1 3 3 2 5 4 8 4
3 ...

output:

67
43
63
67
68
70
72
65
71
63
65
65
64
54
63
64
58
86
81
79
87
87
93
79
84
93
99
78
89
102
104
104
89
99
100
26
38
44
33
42
23
39
45
90
94
97
97
87
47
96
96
69
90
114
118
110
110
119
121
120
114
119
82
43
51
31
44
46
16
52
52
68
33
66
70
71
70
37
71
57
51
51
52
34
18
18
49
73
64
74
66
61
70
49
65
75...

result:

ok 289988 lines

Test #21:

score: 0
Accepted
time: 98ms
memory: 24044kb

input:

10000
8 8
1 2 2 2 2 4 4
6 6
3 7
8 7
5 3
1 6
8 4
6 2
4 4
10 10
1 2 3 4 3 5 5 6 7
4 4
1 10
4 9
9 4
7 5
6 9
6 1
3 2
6 2
9 10
10 10
1 1 1 3 4 5 3 5 3
1 8
9 3
6 6
6 2
6 5
7 1
10 5
4 2
7 3
3 9
8 8
1 2 3 1 3 3 3
6 8
5 8
4 8
1 6
3 8
5 5
7 3
2 4
8 8
1 1 2 1 5 4 2
2 6
2 1
1 5
7 4
5 8
1 2
8 4
4 1
8 8
1 2 3 4 4...

output:

41
86
70
82
82
69
81
68
70
91
82
82
68
51
77
74
76
91
147
145
44
130
153
151
145
129
145
145
66
71
66
70
65
19
65
68
51
48
44
31
51
48
45
50
60
59
59
51
54
59
55
45
74
73
77
77
75
75
75
75
160
171
160
160
157
170
168
158
159
171
90
90
117
114
118
117
107
114
37
113
47
37
45
37
19
45
50
48
121
116
11...

result:

ok 290036 lines

Test #22:

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

input:

10000
9 9
1 2 2 1 2 2 7 3
8 1
6 1
2 4
4 4
2 7
6 5
2 9
9 9
1 9
9 9
1 1 3 1 5 6 5 7
9 5
3 5
7 5
1 3
4 6
7 4
6 6
8 1
8 7
8 8
1 1 3 2 1 5 1
2 1
5 5
4 7
5 2
1 1
7 5
2 5
1 6
10 10
1 1 2 1 5 3 2 7 4
10 3
5 6
4 10
9 3
3 3
4 6
7 7
4 7
9 3
7 7
9 9
1 2 2 4 3 2 1 7
7 8
4 6
9 7
4 3
8 3
2 1
8 6
8 3
2 5
9 9
1 1 2 ...

output:

113
111
109
55
110
112
111
38
113
62
64
61
56
68
70
45
63
62
51
28
57
41
48
29
41
49
96
59
55
69
66
95
46
97
69
46
86
86
57
85
86
83
87
86
84
48
68
63
54
20
68
17
61
65
69
49
49
25
70
69
66
60
66
105
112
68
117
117
116
69
113
113
115
49
45
47
48
45
48
39
40
160
169
172
159
169
170
171
171
169
160
79...

result:

ok 289982 lines

Test #23:

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

input:

10000
8 8
1 1 2 3 2 4 6
7 8
3 4
5 2
2 4
5 3
5 3
2 2
1 7
9 9
1 2 3 1 4 1 3 2
9 6
5 8
9 7
7 4
5 8
3 1
3 1
2 7
1 8
9 9
1 1 3 3 5 1 1 5
8 2
6 4
1 7
8 5
7 3
3 2
5 7
2 4
6 8
9 9
1 2 3 1 3 2 5 1
6 4
4 6
2 1
8 1
8 5
9 2
7 2
3 1
1 2
8 8
1 2 2 2 4 1 1
7 7
7 4
5 8
4 5
8 2
3 4
8 6
4 4
10 10
1 1 1 4 1 5 4 8 4
6 ...

output:

42
43
42
38
23
23
36
42
80
82
76
83
82
80
80
75
81
90
96
89
103
99
99
103
100
104
62
62
76
69
47
77
71
80
76
27
67
66
63
65
63
68
42
154
154
172
171
111
171
154
153
154
173
112
113
112
112
114
113
114
108
109
105
122
123
125
131
132
55
122
123
123
65
62
53
63
61
53
64
67
166
81
165
166
166
133
161
1...

result:

ok 289985 lines

Test #24:

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

input:

2
2 1
1
2 1
2 4
1
1 1
1 2
2 1
2 2

output:

3
2
3
3
2

result:

ok 5 lines

Test #25:

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

input:

10000
9 9
1 1 3 1 4 2 6 4
6 1
9 1
2 2
7 3
8 5
8 3
3 8
7 4
8 3
9 9
1 2 1 1 3 1 1 5
9 5
1 5
1 5
4 7
1 4
6 7
7 9
2 8
6 5
10 10
1 2 3 2 5 6 5 1 6
10 2
4 2
1 1
10 7
3 4
5 5
4 8
3 2
6 1
4 6
8 8
1 2 3 3 2 4 2
2 8
1 4
7 8
5 7
1 4
4 6
5 5
7 5
8 8
1 2 1 4 5 4 6
8 7
3 7
6 8
6 2
2 8
1 7
2 8
5 3
8 8
1 1 1 1 3 6 ...

output:

63
62
34
58
65
58
58
64
58
67
98
98
98
97
103
100
100
104
114
102
68
86
69
100
113
101
115
116
57
65
66
58
65
65
28
58
39
39
21
42
43
36
43
41
50
59
59
59
50
49
58
49
45
49
54
52
52
54
45
16
14
15
25
23
27
26
26
23
85
94
94
92
93
88
94
91
54
154
177
176
85
177
177
177
103
171
175
92
94
94
92
92
59
9...

result:

ok 290047 lines

Test #26:

score: 0
Accepted
time: 80ms
memory: 23492kb

input:

10000
10 10
1 1 2 4 3 4 3 1 1
8 9
5 8
2 8
2 10
1 1
1 8
5 7
9 8
6 5
7 7
10 10
1 2 3 2 5 1 3 4 2
3 1
2 10
2 6
2 6
10 2
9 5
6 3
4 3
8 8
6 1
9 9
1 1 3 3 5 2 3 1
8 1
2 3
6 4
5 8
4 3
1 5
1 4
2 5
8 6
10 10
1 1 1 4 4 3 5 6 6
7 2
5 2
8 10
4 4
8 1
10 2
4 7
9 3
3 7
1 9
10 10
1 1 3 3 3 2 5 7 9
8 8
1 2
10 8
7 7
...

output:

126
135
130
126
120
125
90
126
135
45
134
127
129
129
127
137
135
116
58
131
91
92
88
87
85
92
91
94
88
100
114
113
105
114
117
114
118
67
116
26
69
90
45
75
87
65
78
62
78
83
29
73
84
29
37
73
83
70
74
75
77
74
73
26
76
74
33
38
35
40
40
26
49
48
86
78
102
101
96
30
87
75
84
100
62
64
23
57
46
73
6...

result:

ok 290027 lines

Test #27:

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

input:

10000
10 10
1 2 2 2 3 2 7 4 5
8 7
4 9
10 4
10 5
5 10
6 10
4 2
7 8
6 10
8 9
8 8
1 2 2 2 3 3 1
5 8
5 4
4 4
8 8
6 4
8 1
8 1
4 8
8 8
1 2 3 4 3 5 6
3 5
2 7
8 1
5 7
1 6
3 3
6 4
4 8
10 10
1 1 3 1 1 1 3 2 8
1 6
5 2
3 3
3 8
2 3
7 7
4 8
4 9
5 10
2 6
9 9
1 1 3 4 3 6 3 4
7 3
2 2
2 5
2 6
1 8
3 9
5 3
1 7
9 9
9 9
...

output:

111
111
167
111
111
168
164
111
168
168
64
62
31
22
66
43
43
64
41
44
42
23
41
36
41
42
169
171
150
152
176
85
153
178
178
171
93
32
98
95
93
95
95
95
39
146
145
148
147
148
146
147
146
147
49
51
50
53
42
51
51
43
35
99
100
100
102
97
100
99
101
99
33
71
65
51
70
71
67
66
136
131
129
135
118
115
129...

result:

ok 290108 lines

Extra Test:

score: 0
Extra Test Passed