QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#135812#6757. 深搜hos_lyric100 ✓1518ms127896kbC++1420.6kb2023-08-06 05:56:592023-08-06 05:57:02

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-06 05:57:02]
  • 评测
  • 测评结果:100
  • 用时:1518ms
  • 内存:127896kb
  • [2023-08-06 05:56:59]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 1000000007;
using Mint = ModInt<MO>;


// T: monoid representing information of an interval.
//   T()  should return the identity.
//   T(S s)  should represent a single element of the array.
//   T::push(T &l, T &r)  should push the lazy update.
//   T::pull(const T &l, const T &r)  should pull two intervals.
template <class T> struct SegmentTreeRange {
  int logN, n;
  vector<T> ts;
  SegmentTreeRange() : logN(0), n(0) {}
  explicit SegmentTreeRange(int n_) {
    for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {}
    ts.resize(n << 1);
  }
  template <class S> explicit SegmentTreeRange(const vector<S> &ss) {
    const int n_ = ss.size();
    for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {}
    ts.resize(n << 1);
    for (int i = 0; i < n_; ++i) at(i) = T(ss[i]);
    build();
  }
  T &at(int i) {
    return ts[n + i];
  }
  void build() {
    for (int u = n; --u; ) pull(u);
  }

  inline void push(int u) {
    ts[u].push(ts[u << 1], ts[u << 1 | 1]);
  }
  inline void pull(int u) {
    ts[u].pull(ts[u << 1], ts[u << 1 | 1]);
  }

  // Applies T::f(args...) to [a, b).
  template <class F, class... Args>
  void ch(int a, int b, F f, Args &&... args) {
    assert(0 <= a); assert(a <= b); assert(b <= n);
    if (a == b) return;
    a += n; b += n;
    for (int h = logN; h; --h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) push(aa);
      } else {
        if ((aa << h) != a) push(aa);
        if ((bb << h) != b) push(bb);
      }
    }
    for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
      if (aa & 1) (ts[aa++].*f)(args...);
      if (bb & 1) (ts[--bb].*f)(args...);
    }
    for (int h = 1; h <= logN; ++h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) pull(aa);
      } else {
        if ((aa << h) != a) pull(aa);
        if ((bb << h) != b) pull(bb);
      }
    }
  }

  // Calculates the product for [a, b).
  T get(int a, int b) {
    assert(0 <= a); assert(a <= b); assert(b <= n);
    if (a == b) return T();
    a += n; b += n;
    for (int h = logN; h; --h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) push(aa);
      } else {
        if ((aa << h) != a) push(aa);
        if ((bb << h) != b) push(bb);
      }
    }
    T prodL, prodR, t;
    for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
      if (aa & 1) { t.pull(prodL, ts[aa++]); prodL = t; }
      if (bb & 1) { t.pull(ts[--bb], prodR); prodR = t; }
    }
    t.pull(prodL, prodR);
    return t;
  }

  // Calculates T::f(args...) of a monoid type for [a, b).
  //   op(-, -)  should calculate the product.
  //   e()  should return the identity.
  template <class Op, class E, class F, class... Args>
#if __cplusplus >= 201402L
  auto
#else
  decltype((std::declval<T>().*F())())
#endif
  get(int a, int b, Op op, E e, F f, Args &&... args) {
    assert(0 <= a); assert(a <= b); assert(b <= n);
    if (a == b) return e();
    a += n; b += n;
    for (int h = logN; h; --h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) push(aa);
      } else {
        if ((aa << h) != a) push(aa);
        if ((bb << h) != b) push(bb);
      }
    }
    auto prodL = e(), prodR = e();
    for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
      if (aa & 1) prodL = op(prodL, (ts[aa++].*f)(args...));
      if (bb & 1) prodR = op((ts[--bb].*f)(args...), prodR);
    }
    return op(prodL, prodR);
  }

  // Find min b s.t. T::f(args...) returns true,
  // when called for the partition of [a, b) from left to right.
  //   Returns n + 1 if there is no such b.
  template <class F, class... Args>
  int findRight(int a, F f, Args &&... args) {
    assert(0 <= a); assert(a <= n);
    if ((T().*f)(args...)) return a;
    if (a == n) return n + 1;
    a += n;
    for (int h = logN; h; --h) push(a >> h);
    for (; ; a >>= 1) if (a & 1) {
      if ((ts[a].*f)(args...)) {
        for (; a < n; ) {
          push(a);
          if (!(ts[a <<= 1].*f)(args...)) ++a;
        }
        return a - n + 1;
      }
      ++a;
      if (!(a & (a - 1))) return n + 1;
    }
  }

  // Find max a s.t. T::f(args...) returns true,
  // when called for the partition of [a, b) from right to left.
  //   Returns -1 if there is no such a.
  template <class F, class... Args>
  int findLeft(int b, F f, Args &&... args) {
    assert(0 <= b); assert(b <= n);
    if ((T().*f)(args...)) return b;
    if (b == 0) return -1;
    b += n;
    for (int h = logN; h; --h) push((b - 1) >> h);
    for (; ; b >>= 1) if ((b & 1) || b == 2) {
      if ((ts[b - 1].*f)(args...)) {
        for (; b <= n; ) {
          push(b - 1);
          if (!(ts[(b <<= 1) - 1].*f)(args...)) --b;
        }
        return b - n - 1;
      }
      --b;
      if (!(b & (b - 1))) return -1;
    }
  }
};

////////////////////////////////////////////////////////////////////////////////


struct HLD {
  int n, rt;
  // needs to be tree
  // vertex lists
  // modified in build(rt) (parent removed, heavy child first)
  vector<vector<int>> graph;
  vector<int> sz, par, dep;
  int zeit;
  vector<int> dis, fin, sid;
  // head vertex (minimum depth) in heavy path
  vector<int> head;

  HLD() : n(0), rt(-1), zeit(0) {}
  explicit HLD(int n_) : n(n_), rt(-1), graph(n), zeit(0) {}
  void ae(int u, int v) {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  void dfsSz(int u) {
    sz[u] = 1;
    for (const int v : graph[u]) {
      auto it = std::find(graph[v].begin(), graph[v].end(), u);
      if (it != graph[v].end()) graph[v].erase(it);
      par[v] = u;
      dep[v] = dep[u] + 1;
      dfsSz(v);
      sz[u] += sz[v];
    }
  }
  void dfsHLD(int u) {
    dis[u] = zeit++;
    const int deg = graph[u].size();
    if (deg > 0) {
      int vm = graph[u][0];
      int jm = 0;
      for (int j = 1; j < deg; ++j) {
        const int v = graph[u][j];
        if (sz[vm] < sz[v]) {
          vm = v;
          jm = j;
        }
      }
      swap(graph[u][0], graph[u][jm]);
      head[vm] = head[u];
      dfsHLD(vm);
      for (int j = 1; j < deg; ++j) {
        const int v = graph[u][j];
        head[v] = v;
        dfsHLD(v);
      }
    }
    fin[u] = zeit;
  }
  void build(int rt_) {
    assert(0 <= rt_); assert(rt_ < n);
    rt = rt_;
    sz.assign(n, 0);
    par.assign(n, -1);
    dep.assign(n, -1);
    dep[rt] = 0;
    dfsSz(rt);
    zeit = 0;
    dis.assign(n, -1);
    fin.assign(n, -1);
    head.assign(n, -1);
    head[rt] = rt;
    dfsHLD(rt);
    assert(zeit == n);
    sid.assign(n, -1);
    for (int u = 0; u < n; ++u) sid[dis[u]] = u;
  }

  friend ostream &operator<<(ostream &os, const HLD &hld) {
    const int maxDep = *max_element(hld.dep.begin(), hld.dep.end());
    vector<string> ss(2 * maxDep + 1);
    int pos = 0, maxPos = 0;
    for (int j = 0; j < hld.n; ++j) {
      const int u = hld.sid[j];
      const int d = hld.dep[u];
      if (hld.head[u] == u) {
        if (j != 0) {
          pos = maxPos + 1;
          ss[2 * d - 1].resize(pos, '-');
          ss[2 * d - 1] += '+';
        }
      } else {
        ss[2 * d - 1].resize(pos, ' ');
        ss[2 * d - 1] += '|';
      }
      ss[2 * d].resize(pos, ' ');
      ss[2 * d] += std::to_string(u);
      if (maxPos < static_cast<int>(ss[2 * d].size())) {
        maxPos = ss[2 * d].size();
      }
    }
    for (int d = 0; d <= 2 * maxDep; ++d) os << ss[d] << '\n';
    return os;
  }

  bool contains(int u, int v) const {
    return (dis[u] <= dis[v] && dis[v] < fin[u]);
  }
  int lca(int u, int v) const {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    for (; head[u] != head[v]; ) (dis[u] > dis[v]) ? (u = par[head[u]]) : (v = par[head[v]]);
    return (dis[u] > dis[v]) ? v : u;
  }
  int jumpUp(int u, int d) const {
    assert(0 <= u); assert(u < n);
    assert(d >= 0);
    if (dep[u] < d) return -1;
    const int tar = dep[u] - d;
    for (u = head[u]; ; u = head[par[u]]) {
      if (dep[u] <= tar) return sid[dis[u] + (tar - dep[u])];
    }
  }
  int jump(int u, int v, int d) const {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    assert(d >= 0);
    const int l = lca(u, v);
    const int du = dep[u] - dep[l], dv = dep[v] - dep[l];
    if (d <= du) {
      return jumpUp(u, d);
    } else if (d <= du + dv) {
      return jumpUp(v, du + dv - d);
    } else {
      return -1;
    }
  }
  // [u, v) or [u, v]
  template <class F> void doPathUp(int u, int v, bool inclusive, F f) const {
    assert(contains(v, u));
    for (; head[u] != head[v]; u = par[head[u]]) f(dis[head[u]], dis[u] + 1);
    if (inclusive) {
      f(dis[v], dis[u] + 1);
    } else {
      if (v != u) f(dis[v] + 1, dis[u] + 1);
    }
  }
  // not path order, include lca(u, v) or not
  template <class F> void doPath(int u, int v, bool inclusive, F f) const {
    const int l = lca(u, v);
    doPathUp(u, l, false, f);
    doPathUp(v, l, inclusive, f);
  }

  // (vs, ps): compressed tree
  // vs: DFS order (sorted by dis)
  // vs[ps[x]]: the parent of vs[x]
  // ids[vs[x]] = x, not set for non-tree vertex
  vector<int> ids;
  pair<vector<int>, vector<int>> compress(vector<int> us) {
    // O(n) first time
    ids.resize(n, -1);
    std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (dis[u] < dis[v]);
    });
    us.erase(std::unique(us.begin(), us.end()), us.end());
    int usLen = us.size();
    assert(usLen >= 1);
    for (int x = 1; x < usLen; ++x) us.push_back(lca(us[x - 1], us[x]));
    std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (dis[u] < dis[v]);
    });
    us.erase(std::unique(us.begin(), us.end()), us.end());
    usLen = us.size();
    for (int x = 0; x < usLen; ++x) ids[us[x]] = x;
    vector<int> ps(usLen, -1);
    for (int x = 1; x < usLen; ++x) ps[x] = ids[lca(us[x - 1], us[x])];
    return make_pair(us, ps);
  }
};

////////////////////////////////////////////////////////////////////////////////


struct Node {
  Mint sum, lz;
  Node() : sum(0), lz(1) {}
  void push(Node &l, Node &r) {
    if (lz != 1) {
      l.mul(lz);
      r.mul(lz);
      lz = 1;
    }
  }
  void pull(const Node &l, const Node &r) {
    sum = l.sum + r.sum;
  }
  void mul(const Mint &val) {
    sum *= val;
    lz *= val;
  }
  // leaf
  void add(const Mint &val) {
    sum += val;
  }
};


int Subtask;
int N, M, K;
vector<int> A, B, C, D;
vector<int> S;

const Mint INV2 = Mint(2).inv();
vector<Mint> two, owt;

HLD hld;


namespace brute {
int d[310][310];
bool check(int m, int u) {
  return (
    d[C[m]][u] == d[C[m]][D[m]] + d[D[m]][u] ||
    d[D[m]][u] == d[D[m]][C[m]] + d[C[m]][u]
  );
}
Mint run() {
  for (int u = 0; u < N; ++u) {
    fill(d[u], d[u] + N, N);
    d[u][u] = 0;
  }
  for (int i = 0; i < N - 1; ++i) {
    chmin(d[A[i]][B[i]], 1);
    chmin(d[B[i]][A[i]], 1);
  }
  for (int w = 0; w < N; ++w) for (int u = 0; u < N; ++u) for (int v = 0; v < N; ++v) {
    chmin(d[u][v], d[u][w] + d[w][v]);
  }
  Mint ans = 0;
  for (int p = 1; p < 1 << K; ++p) {
    int cnt = 0;
    for (int m = 0; m < M; ++m) {
      bool ok = true;
      for (int k = 0; k < K; ++k) if (p >> k & 1) {
        ok = ok && check(m, S[k]);
      }
      if (ok) {
        ++cnt;
      }
    }
// cerr<<"[brute] p = "<<p<<": cnt = "<<cnt<<endl;
    ans -= (__builtin_parity(p)?-1:+1) * two[cnt];
  }
  return ans;
}
}  // brute


Mint run() {
  const auto &dis = hld.dis;
  const auto &fin = hld.fin;
  vector<vector<int>> dss(N);
  vector<vector<pair<int, int>>> cdss(N);
  for (int m = 0; m < M; ++m) {
    int c = C[m], d = D[m];
    if (dis[c] > dis[d]) swap(c, d);
    if (hld.contains(c, d)) {
      dss[c].push_back(d);
    } else {
      const int l = hld.lca(c, d);
      cdss[l].emplace_back(c, d);
    }
  }
  vector<int> inS(N, 0);
  for (int k = 0; k < K; ++k) {
    inS[S[k]] = 1;
  }
  /*
    seg: more PIE vertex
    cross: no more PIE vertex (*2 by tree-edge or cross-edge (1 subtree))
  */
  SegmentTreeRange<Node> seg(N), cross(N);
  vector<int> below(N, 0);
  for (int j = N; --j >= 0; ) {
    const int u = hld.sid[j];
    
    // tree-edge
    for (const int d : dss[u]) {
      seg.ch(dis[d], fin[d], &Node::mul, 2);
      cross.ch(dis[d], fin[d], &Node::mul, 2);
      const int v = hld.jump(u, d, 1);
      ++below[v];
    }
    // to merge *2's
    for (const int v : hld.graph[u]) {
      below[u] += below[v];
      seg.ch(dis[v], fin[v], &Node::mul, owt[below[v]]);
      cross.ch(dis[v], fin[v], &Node::mul, owt[below[v]]);
    }
    
    // cross-edge, 1 subtree
    for (const auto &cd : cdss[u]) {
      for (const int v : {cd.first, cd.second}) {
        cross.ch(dis[v], fin[v], &Node::mul, 2);
      }
    }
    
    // cross-edge, 2 subtrees: *2 for [dis[c], fin[c]) * [dis[d], fin[d])
    {
      vector<int> xs;
      for (const int v : hld.graph[u]) {
        xs.push_back(dis[v]);
      }
      xs.push_back(fin[u]);
      for (const auto &cd : cdss[u]) {
        xs.push_back(dis[cd.first]);
        xs.push_back(fin[cd.first]);
      }
      sort(xs.begin(), xs.end());
      xs.erase(unique(xs.begin(), xs.end()), xs.end());
      const int len = (int)xs.size() - 1;
      vector<vector<pair<int, Mint>>> eventss(len + 1);
      auto add = [&](int x, int d, Mint val) -> void {
        eventss[lower_bound(xs.begin(), xs.end(), x) - xs.begin()].emplace_back(d, val);
      };
      for (const auto &cd : cdss[u]) {
        add(dis[cd.first], cd.second, 2);
        add(fin[cd.first], cd.second, INV2);
      }
      Mint here2 = 0;
      {
        int k = 0;
        for (const int v : hld.graph[u]) {
          for (; xs[k] < fin[v]; ++k) {
            for (const auto &event : eventss[k]) {
              seg.ch(dis[event.first], fin[event.first], &Node::mul, event.second);
            }
            const Mint resL = seg.get(xs[k], xs[k + 1]).sum;
            const Mint resR = seg.get(fin[v], fin[u]).sum;
            here2 += resL * resR;
          }
        }
        for (; k <= len; ++k) {
          for (const auto &event : eventss[k]) {
            seg.ch(dis[event.first], fin[event.first], &Node::mul, event.second);
          }
        }
      }
// cerr<<"cdss["<<u<<"] = "<<cdss[u]<<": 2^below[u] * here2 = 2^"<<below[u]<<" * "<<here2<<" = "<<(two[below[u]]*here2)<<endl;
      cross.ch(dis[u], dis[u] + 1, &Node::add, here2);
    }
    
    // state u (without choosing PIE vertex): take (>= 2) subtrees
    {
      Mint dp[4] = {};
      dp[0] = 1;
      for (const int v : hld.graph[u]) {
        const Mint res = seg.get(dis[v], fin[v]).sum;
        for (int k = 3; k >= 0; --k) {
          dp[min(k + 1, 3)] += dp[k] * res;
        }
      }
// cerr<<"u = "<<u<<": 2^below[u] * dp[2] = 2^"<<below[u]<<" * "<<dp[2]<<" = "<<(two[below[u]]*dp[2])<<endl;
// cerr<<"u = "<<u<<": 2^below[u] * dp[3] = 2^"<<below[u]<<" * "<<dp[3]<<" = "<<(two[below[u]]*dp[2])<<endl;
      seg.ch(dis[u], dis[u] + 1, &Node::add, dp[2] + dp[3]);
      cross.ch(dis[u], dis[u] + 1, &Node::add, dp[3]);
    }
    
    // to merge *2's
    seg.ch(dis[u], fin[u], &Node::mul, two[below[u]]);
    cross.ch(dis[u], fin[u], &Node::mul, two[below[u]]);
    
    // PIE vertex
    if (inS[u]) {
      Mint here = 0;
      here -= two[below[u]];
      here -= seg.get(dis[u], fin[u]).sum;
// cerr<<"u = "<<u<<": here = "<<here<<endl;
      seg.ch(dis[u], dis[u] + 1, &Node::add, here);
      cross.ch(dis[u], dis[u] + 1, &Node::add, here);
    }
  }
  Mint ans = 0;
  ans -= cross.get(0, N).sum;
  return ans;
}


int main() {
  for (; ~scanf("%d", &Subtask); ) {
    scanf("%d%d%d", &N, &M, &K);
    A.resize(N - 1);
    B.resize(N - 1);
    for (int i = 0; i < N - 1; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    C.resize(M);
    D.resize(M);
    for (int m = 0; m < M; ++m) {
      scanf("%d%d", &C[m], &D[m]);
      --C[m];
      --D[m];
    }
    S.resize(K);
    for (int k = 0; k < K; ++k) {
      scanf("%d", &S[k]);
      --S[k];
    }
    sort(S.begin(), S.end());
    
    two.resize(M + 1);
    owt.resize(M + 1);
    two[0] = 1;
    owt[0] = 1;
    for (int m = 1; m <= M; ++m) {
      two[m] = two[m - 1] * 2;
      owt[m] = owt[m - 1] * INV2;
    }
    
    hld = HLD(N);
    for (int i = 0; i < N - 1; ++i) {
      hld.ae(A[i], B[i]);
    }
    hld.build(0);
// cerr<<"========"<<endl<<hld<<endl;
    
    const Mint ans = run();
    printf("%u\n", ans.x);
#ifdef LOCAL
if(K<=20){
 const Mint brt=brute::run();
 if(brt!=ans)cerr<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<S<<": "<<brt<<" "<<ans<<endl;
 assert(brt==ans);
}
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 4
Accepted
time: 1ms
memory: 3760kb

input:

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

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: 4
Accepted
time: 1ms
memory: 3828kb

input:

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

output:

46

result:

ok 1 number(s): "46"

Test #3:

score: 4
Accepted
time: 1ms
memory: 3780kb

input:

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

output:

46

result:

ok 1 number(s): "46"

Test #4:

score: 4
Accepted
time: 1ms
memory: 3768kb

input:

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

output:

1384

result:

ok 1 number(s): "1384"

Test #5:

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

input:

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

output:

744

result:

ok 1 number(s): "744"

Test #6:

score: 4
Accepted
time: 1ms
memory: 3712kb

input:

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

output:

1873

result:

ok 1 number(s): "1873"

Test #7:

score: 4
Accepted
time: 1ms
memory: 3844kb

input:

7
300 168 6
1 184
1 41
10 41
184 18
18 215
184 271
215 267
194 184
165 18
223 18
223 83
123 165
6 165
90 83
41 2
123 40
232 194
177 232
262 123
140 271
40 211
11 271
21 18
15 11
158 177
197 1
41 35
194 110
217 177
215 121
297 123
223 38
15 72
297 46
35 212
228 140
1 89
130 11
158 296
279 35
90 287
1...

output:

144500972

result:

ok 1 number(s): "144500972"

Test #8:

score: 4
Accepted
time: 1ms
memory: 3824kb

input:

8
300 161 6
1 67
67 273
273 213
67 232
213 32
213 111
215 232
137 32
11 232
16 137
20 137
285 273
1 284
110 11
46 285
156 67
46 263
213 250
146 213
263 78
39 110
270 67
175 110
175 170
216 1
111 281
175 209
118 110
112 39
115 285
250 205
237 111
124 284
46 197
187 281
156 60
236 111
1 103
211 156
26...

output:

484515026

result:

ok 1 number(s): "484515026"

Test #9:

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

input:

9
300 157 6
56 1
1 36
56 280
285 1
285 265
166 280
56 213
87 1
191 213
87 232
166 31
280 112
43 213
43 24
293 166
232 91
31 89
259 213
232 68
134 24
168 91
280 257
172 280
87 226
229 36
56 209
239 232
43 72
72 132
265 235
283 72
300 112
1 252
112 174
241 285
24 44
8 235
193 43
256 280
168 287
257 61...

output:

644455703

result:

ok 1 number(s): "644455703"

Test #10:

score: 4
Accepted
time: 2ms
memory: 3916kb

input:

10
300 299 120
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51...

output:

926641430

result:

ok 1 number(s): "926641430"

Test #11:

score: 4
Accepted
time: 2ms
memory: 3856kb

input:

11
300 300 116
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51...

output:

723203191

result:

ok 1 number(s): "723203191"

Test #12:

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

input:

12
300 300 200
198 1
1 169
139 169
139 289
13 139
198 265
1 45
61 13
139 58
265 130
289 210
109 265
169 114
250 139
250 140
89 130
77 169
250 74
136 13
224 45
45 164
61 217
58 148
249 77
248 289
118 210
89 262
136 146
65 45
13 242
293 61
295 224
230 265
115 230
136 98
78 130
164 184
240 109
89 212
2...

output:

316149672

result:

ok 1 number(s): "316149672"

Test #13:

score: 4
Accepted
time: 1ms
memory: 3888kb

input:

13
300 300 200
1 296
1 284
1 160
296 154
109 160
160 134
175 1
134 95
184 154
198 175
184 172
10 109
248 284
51 154
10 33
134 73
181 172
172 55
33 121
218 121
225 248
205 248
91 181
282 248
154 38
218 83
188 284
243 248
225 69
221 160
65 121
154 233
269 243
287 38
154 18
175 100
147 154
23 65
154 24...

output:

383331059

result:

ok 1 number(s): "383331059"

Test #14:

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

input:

14
300 295 200
1 155
1 160
24 155
1 59
160 290
5 155
24 258
59 33
1 264
152 264
160 190
159 190
5 139
1 69
24 68
33 161
81 290
84 33
5 227
45 84
191 1
288 45
6 69
155 114
24 4
159 181
151 227
1 8
151 165
4 221
101 190
74 290
23 221
128 221
246 24
123 290
23 80
25 152
73 80
73 26
133 159
288 7
44 26
...

output:

129954782

result:

ok 1 number(s): "129954782"

Test #15:

score: 4
Accepted
time: 2ms
memory: 3952kb

input:

15
300 299 200
1 272
1 205
238 272
170 205
272 32
32 192
243 170
59 192
170 97
205 169
86 59
252 32
4 32
40 252
50 86
289 169
59 122
289 128
3 1
32 258
289 229
289 145
3 48
97 84
41 40
35 258
238 127
48 179
129 127
9 145
50 295
243 27
177 127
292 295
170 95
295 43
218 170
55 169
278 35
81 50
155 128...

output:

90662829

result:

ok 1 number(s): "90662829"

Test #16:

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

input:

16
300 293 200
1 176
1 230
122 176
3 230
3 110
64 1
110 69
219 176
64 279
152 122
69 170
3 269
152 155
132 122
76 155
246 155
235 230
5 269
123 69
42 3
147 155
267 269
148 122
116 219
148 59
85 69
176 194
24 42
220 64
235 238
54 64
97 76
116 25
122 75
116 297
176 272
258 76
10 1
231 3
262 170
76 251...

output:

693422085

result:

ok 1 number(s): "693422085"

Test #17:

score: 4
Accepted
time: 523ms
memory: 69608kb

input:

17
200000 199883 97179
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
...

output:

306668123

result:

ok 1 number(s): "306668123"

Test #18:

score: 4
Accepted
time: 490ms
memory: 69556kb

input:

18
200000 199876 97287
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
...

output:

740342085

result:

ok 1 number(s): "740342085"

Test #19:

score: 4
Accepted
time: 511ms
memory: 51780kb

input:

19
200000 200000 133333
1 124760
44003 124760
37294 1
163294 1
124760 4221
1 118363
163294 130356
163294 26345
150233 124760
163294 71228
118363 185064
16012 4221
136324 4221
130356 159564
16012 82836
37294 117301
37294 113319
118985 118363
67201 130356
134497 44003
118985 184508
62789 4221
167242 1...

output:

394869877

result:

ok 1 number(s): "394869877"

Test #20:

score: 4
Accepted
time: 492ms
memory: 51864kb

input:

20
200000 200000 133333
140575 1
1 186893
181732 140575
79385 140575
181732 9627
186089 181732
186893 149538
186893 55018
140575 13029
186089 97797
164282 149538
18033 181732
55018 137342
1 136416
140575 148531
97797 1750
9627 9882
181032 55018
34757 55018
72967 181032
137342 70468
85420 136416
1803...

output:

533887309

result:

ok 1 number(s): "533887309"

Test #21:

score: 4
Accepted
time: 485ms
memory: 51788kb

input:

21
200000 200000 133333
1 101322
88622 101322
58269 101322
58269 180983
168242 101322
95381 1
193115 95381
190995 168242
101322 148011
62879 88622
101322 172145
120738 190995
15239 120738
80271 193115
168242 21478
125310 58269
193115 93056
172145 110789
13098 172145
148011 174687
173743 190995
85181...

output:

487737487

result:

ok 1 number(s): "487737487"

Test #22:

score: 4
Accepted
time: 526ms
memory: 54724kb

input:

22
200000 199999 133333
150539 1
150539 74762
1 34140
1 195593
164815 1
32085 195593
156066 32085
21074 34140
162900 34140
70220 195593
6115 32085
155396 70220
73894 150539
22486 195593
195593 19530
6115 146034
21074 40513
61074 146034
146034 198896
120389 195593
34219 61074
113107 1
195593 86095
64...

output:

367879330

result:

ok 1 number(s): "367879330"

Test #23:

score: 4
Accepted
time: 1475ms
memory: 127896kb

input:

23
500000 499999 333333
1 199660
284063 1
156514 199660
346277 1
199660 147381
284063 314108
147381 497971
248228 1
207371 284063
314108 165529
156514 452115
218249 248228
140112 1
395841 452115
337568 156514
314108 125202
310327 218249
446894 140112
490601 1
165529 179615
323005 207371
179615 57231...

output:

716372640

result:

ok 1 number(s): "716372640"

Test #24:

score: 4
Accepted
time: 1518ms
memory: 127144kb

input:

24
500000 499998 333333
65074 1
65074 304288
1 335471
59340 335471
304288 81779
81779 254965
59340 491338
108358 81779
59340 313254
59340 490077
150085 108358
304288 231400
108450 491338
108358 58590
215265 254965
14203 108450
222338 81779
313254 347805
136496 215265
173386 1
490077 183586
108358 20...

output:

47496457

result:

ok 1 number(s): "47496457"

Test #25:

score: 4
Accepted
time: 1481ms
memory: 127652kb

input:

25
500000 499997 333333
1 359765
359765 132729
101821 132729
1 357109
101821 393660
393660 141211
204917 1
393660 160129
204917 59682
165524 160129
359765 184850
39288 1
97240 165524
77900 101821
39288 343784
402651 1
181637 77900
351195 393660
77900 303404
357109 478493
403484 101821
357109 458913
...

output:

661767304

result:

ok 1 number(s): "661767304"

Extra Test:

score: 0
Extra Test Passed