結果

問題 No.1600 Many Shortest Path Problems
ユーザー ei1333333ei1333333
提出日時 2021-07-10 01:04:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 14,176 bytes
コンパイル時間 2,957 ms
コンパイル使用メモリ 231,248 KB
実行使用メモリ 75,520 KB
最終ジャッジ日時 2024-07-01 19:57:44
合計ジャッジ時間 31,711 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1,342 ms
58,368 KB
testcase_05 AC 1,319 ms
58,240 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2 ms
5,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 2 ms
5,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1,038 ms
58,752 KB
testcase_36 AC 394 ms
58,732 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 1,060 ms
66,176 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 TLE -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;
//const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

/**
 * @brief Splay-Tree-Base(Splay木)
 */
template< typename Node >
struct SplayTreeBase {
public:
  using NP = Node *;

  bool is_root(const NP &t) const { return !t->p || (t->p->l != t && t->p->r != t); }

  inline size_t count(const NP &t) const { return t ? t->sz : 0; }

  void splay(NP t) {
    push(t);
    while(!is_root(t)) {
      auto *q = t->p;
      if(!is_root(t)) {
        push(q), push(t);
        if(q->l == t) rotr(t);
        else rotl(t);
      } else {
        auto *r = q->p;
        push(r), push(q), push(t);
        if(r->l == q) {
          if(q->l == t) rotr(q), rotr(t);
          else rotl(t), rotr(t);
        } else {
          if(q->r == t) rotl(q), rotl(t);
          else rotr(t), rotl(t);
        }
      }
    }
  }

  NP erase(NP t) {
    splay(t);
    Node *x = t->l, *y = t->r;
    delete t;
    if(!x) {
      t = y;
      if(t) t->p = nullptr;
    } else if(!y) {
      t = x;
      t->p = nullptr;
    } else {
      x->p = nullptr;
      t = get_right(x);
      splay(t);
      t->r = y;
      y->p = t;
    }
    return t;
  }

  NP splay_front(NP t) {
    splay(t);
    while(t->l) t = t->l;
    splay(t);
    return t;
  }

  NP splay_back(NP t) {
    splay(t);
    while(t->r) t = t->r;
    splay(t);
    return t;
  }

  pair< NP, NP > split(NP t, int k) {
    if(!t) return {nullptr, nullptr};
    push(t);
    if(k <= count(t->l)) {
      auto x = split(t->l, k);
      t->l = x.second;
      t->p = nullptr;
      if(x.second) x.second->p = t;
      return {x.first, update(t)};
    } else {
      auto x = split(t->r, k - count(t->l) - 1);
      t->r = x.first;
      t->p = nullptr;
      if(x.first) x.first->p = t;
      return {update(t), x.second};
    }
  }

  template< typename... Args >
  NP merge(NP p, Args... args) {
    return merge(p, merge(args...));
  }

  NP merge(NP l, NP r) {
    if(!l && !r) return nullptr;
    if(!l) return splay(r), r;
    if(!r) return splay(l), l;
    splay(l), splay(r);
    l = splay_back(l);
    l->r = r;
    r->p = l;
    update(l);
    return l;
  }

  tuple< NP, NP, NP > split3(NP t, int a, int b) {
    splay(t);
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    return make_tuple(x.first, y.first, y.second);
  }

  virtual void push(NP t) = 0;

  virtual Node *update(NP t) = 0;

private:

  void rotr(NP t) {
    auto *x = t->p, *y = x->p;
    if((x->l = t->r)) t->r->p = x;
    t->r = x, x->p = t;
    update(x), update(t);
    if((t->p = y)) {
      if(y->l == x) y->l = t;
      if(y->r == x) y->r = t;
      update(y);
    }
  }

  void rotl(NP t) {
    auto *x = t->p, *y = x->p;
    if((x->r = t->l)) t->l->p = x;
    t->l = x, x->p = t;
    update(x), update(t);
    if((t->p = y)) {
      if(y->l == x) y->l = t;
      if(y->r == x) y->r = t;
      update(y);
    }
  }

  NP build(int l, int r, const vector< NP > &v) {
    if(l + 1 >= r) return v[l];
    return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
  }

protected:

  NP build_node(const vector< NP > &v) {
    return build(0, v.size(), v);
  }

  NP insert_node(NP t, int k, NP v) {
    splay(t);
    auto x = split(t, k);
    return merge(x.first, v, x.second);
  }

  NP erase_node(NP t, int k) {
    splay(t);
    auto x = split(t, k);
    auto y = split(x.second, 1);
    delete y.first;
    return merge(x.first, y.second);
  }
};

/**
 * @brief Reversible-Splay-Tree(反転可能Splay木)
 */
template< typename Tp >
struct ReversibleSplayTreeNode {
  using T = Tp;
  ReversibleSplayTreeNode *l, *r, *p;
  T key, sum;
  bool rev;

  ReversibleSplayTreeNode() : ReversibleSplayTreeNode(Tp()) {}

  ReversibleSplayTreeNode(const T &key) :
      key(key), sum(key), rev(false), l(nullptr), r(nullptr), p(nullptr) {}
};

template< typename Np >
struct ReversibleSplayTree : SplayTreeBase< Np > {
public:
  using Node = Np;
  using T = typename Node::T;
  using super = SplayTreeBase< Node >;
  using NP = typename super::NP;


  using super::splay;
  using super::split;
  using super::count;
  using super::merge;
  using super::build_node;
  using super::insert_node;

  inline const T &sum(const NP t) { return t ? t->sum : 0; }

  NP alloc(const T &x) { return new Node(x); }

  T query(NP &t, int a, int b) {
    splay(t);
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    auto ret = sum(y.first);
    t = merge(x.first, y.first, y.second);
    return ret;
  }

  NP build(const vector< T > &v) {
    vector< NP > vs(v.size());
    for(int i = 0; i < v.size(); i++) vs[i] = alloc(v[i]);
    return build_node(vs);
  }

  void toggle(NP t) {
    swap(t->l, t->r);
    t->rev ^= true;
  }

  NP update(NP t) override {
    t->sum = t->key;
    if(t->l) t->sum += t->l->sum;
    if(t->r) t->sum += t->r->sum;
    return t;
  }

  void push(NP t) override {
    if(t->rev) {
      if(t->l) toggle(t->l);
      if(t->r) toggle(t->r);
      t->rev = false;
    }
  }

  NP insert(NP t, int k, const T &x) {
    return insert_node(t, k, alloc(x));
  }

  NP set_element(NP t, int k, const T &x) {
    splay(t);
    return imp_set_element(t, k, x);
  }

  pair< NP, NP > split_lower_bound(NP t, const T &key) {
    if(!t) return {nullptr, nullptr};
    push(t);
    if(key <= t->key) {
      auto x = split_lower_bound(t->l, key);
      t->l = x.second;
      t->p = nullptr;
      if(x.second) x.second->p = t;
      return {x.first, update(t)};
    } else {
      auto x = split_lower_bound(t->r, key);
      t->r = x.first;
      t->p = nullptr;
      if(x.first) x.first->p = t;
      return {update(t), x.second};
    }
  }

private:

  NP imp_set_element(NP t, int k, const T &x) {
    push(t);
    if(k < count(t->l)) {
      return imp_set_element(t->l, k, x);
    } else if(k == count(t->l)) {
      t->key = x;
      splay(t);
      return t;
    } else {
      return imp_set_element(t->r, k - count(t->l) - 1, x);
    }
  }
};

template< typename T >
using RST = ReversibleSplayTree< ReversibleSplayTreeNode< T > >;

/**
 * @brief Link-Cut-Tree
 */
template< typename STp >
struct LinkCutTree : STp {
  using ST = STp;
  using ST::ST;
  using Node = typename ST::Node;

  Node *expose(Node *t) {
    Node *rp = nullptr;
    for(Node *cur = t; cur; cur = cur->p) {
      this->splay(cur);
      cur->r = rp;
      this->update(cur);
      rp = cur;
    }
    this->splay(t);
    return rp;
  }

  void link(Node *child, Node *parent) {
    expose(child);
    expose(parent);
    child->p = parent;
    parent->r = child;
    this->update(parent);
  }

  void cut(Node *child) {
    expose(child);
    auto *parent = child->l;
    child->l = nullptr;
    parent->p = nullptr;
    this->update(child);
  }

  void evert(Node *t) {
    expose(t);
    this->toggle(t);
    this->push(t);
  }

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

  Node *get_kth(Node *x, int k) {
    expose(x);
    while(x) {
      this->push(x);
      if(x->r && x->r->sz > k) {
        x = x->r;
      } else {
        if(x->r) k -= x->r->sz;
        if(k == 0) return x;
        k -= 1;
        x = x->l;
      }
    }
    return nullptr;
  }

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

/**
 * @brief Union-Find
 * @docs docs/union-find.md
 */
struct UnionFind {
  vector< int > data;

  UnionFind() = default;

  explicit UnionFind(size_t sz) : data(sz, -1) {}

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return false;
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  int find(int k) {
    if(data[k] < 0) return (k);
    return data[k] = find(data[k]);
  }

  int size(int k) {
    return -data[find(k)];
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }
};


template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  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 x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, 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(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt< mod >;

int main() {
  int N, M;
  cin >> N >> M;

  using LCT = LinkCutTree< RST< modint > >;

  LCT lct;
  vector< LCT::Node * > vs(N), es(M);
  for(int i = 0; i < N; i++) vs[i] = lct.alloc(0);

  modint cost = 1;
  UnionFind uf(N);
  vector< int > A(M), B(M);
  vector< int > type(M);
  vector< vector< pair< int, int > > > g(N);
  vector< set< int > > qs(N);
  for(int i = 0; i < M; i++) {
    cost *= 2;
    cin >> A[i] >> B[i];
    --A[i], --B[i];
    es[i] = lct.alloc(cost);
    if(uf.unite(A[i], B[i])) {
      lct.evert(vs[A[i]]);
      lct.link(vs[A[i]], es[i]);
      lct.link(es[i], vs[B[i]]);
      g[A[i]].emplace_back(B[i], i);
      g[B[i]].emplace_back(A[i], i);
      type[i] = 1;
    } else {
      qs[A[i]].emplace(i);
      qs[B[i]].emplace(i);
    }
  }
  vector< int > dep(N);
  MFP([&](auto rec, int idx, int par, int d) -> void {
    dep[idx] = d;
    for(auto &e : g[idx]) {
      int to = e.first;
      if(to != par) {
        rec(to, idx, d + 1);
      }
    }
  })(0, -1, 0);

  int Q;
  cin >> Q;
  vector< int > ans(Q, -1);
  vector< int > X(Q), Y(Q), Z(Q);
  vector< vector< int > > sub(N);
  for(int i = 0; i < Q; i++) {
    cin >> X[i] >> Y[i] >> Z[i];
    --X[i], --Y[i], --Z[i];
    auto check = [&]() -> bool {
      lct.evert(es[Z[i]]);
      return lct.lca(vs[X[i]], vs[Y[i]]) != es[Z[i]];
    };
    if(type[Z[i]] == 0 or check()) {
      lct.evert(vs[X[i]]);
      lct.expose(vs[Y[i]]);
      ans[i] = (vs[Y[i]]->sum).x;
    } else {
      sub[dep[A[Z[i]]] > dep[B[Z[i]]] ? A[Z[i]] : B[Z[i]]].emplace_back(i);
    }
  }

  MFP([&](auto rec, int idx, int par) -> void {
    for(auto &e : g[idx]) {
      int to = e.first;
      if(to != par) {
        rec(to, idx);
        if(qs[to].empty()) continue;
        if(sub[to].empty()) continue;
        {
          lct.evert(es[e.second]);
          lct.cut(vs[idx]);
          lct.cut(vs[to]);
          int p = *begin(qs[to]);

          lct.evert(vs[A[p]]);
          lct.link(vs[A[p]], es[p]);
          lct.link(es[p], vs[B[p]]);

          for(auto &i : sub[to]) {
            lct.evert(vs[X[i]]);
            lct.expose(vs[Y[i]]);
            ans[i] = (vs[Y[i]]->sum).x;
          }

          lct.evert(es[p]);
          lct.cut(vs[A[p]]);
          lct.cut(vs[B[p]]);
          lct.evert(vs[to]);
          lct.link(vs[to], es[e.second]);
          lct.link(es[e.second], vs[idx]);
        }

        if(qs[idx].size() < qs[to].size()) {
          qs[idx].swap(qs[to]);
        }
        for(auto p : qs[to]) {
          if(qs[idx].count(p)) qs[idx].erase(p);
          else qs[idx].emplace(p);
        }
      }
    }
  })(0, -1);


  for(int i = 0; i < Q; i++) {
    cout << ans[i] << "\n";
  }

}
0