lmori's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub lmorinn/library

:heavy_check_mark: verify/AizuOnlineJudge/graph/connectivity/GraphConstruction2235lct.test.cpp

Depends on

Code

#include "../../../../template/template.hpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2235"
#include "../../../../graph/connectivity/OfflineDynamicConnectivityLCT.hpp"

using R = int;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, q;
  in(n, q);
  OfflineDynamicConnectivity<R> g(n);

  rep(i, q) {
    int com, u, v;
    in(com, u, v);
    if (com == 1) {
      g.link(u, v);
    } else if (com == 2) {
      g.cut(u, v);
    } else if (com == 3) {
      g.same(u, v);
    }
  }

  for (auto x : g.build()) {
    out(x ? "YES" : "NO");
  }
}
#line 2 "template/template.hpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

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 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << pair_var.first << " " << pair_var.second;
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

void outf() { cout << '\n'; }
template <class T, class... Ts>
void outf(const T &a, const Ts &...b) {
  cout << fixed << setprecision(14) << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

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

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion
#line 2 "verify/AizuOnlineJudge/graph/connectivity/GraphConstruction2235lct.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2235"
#line 2 "graph/dynamic-tree/LinkCutTreeEdge.hpp"

// u,v,valを定義
template <class S, auto op, auto e, class F, auto mapping, auto composition, auto id, auto reverseprod>
struct LinkCutTree {
 private:
  struct Node {
    Node *l = 0;
    Node *r = 0;
    Node *p = 0;
    Node *pp = 0;
    // 値、集約値、作用値
    S val = e();
    S acc = e();
    F lazy = id();
    int idx = -1;
    int z = 0;
    int sumz = 0;
    int w = 0;
    int sumw = 0;
    bool rev = 0;
    int u = -1;
    int v = -1;
  };

  using pNode = shared_ptr<Node>;
  pNode pNIL;
  Node *NIL = nullptr;
  vector<pNode> A;
  vector<unordered_map<int, Node *>> ed;
  queue<int> unused;

  void push(Node *v) {
    if (v->l != NIL) {
      v->l->val = mapping(v->lazy, v->l->val);
      v->l->acc = mapping(v->lazy, v->l->acc);
      v->l->lazy = composition(v->lazy, v->l->lazy);
    }
    if (v->r != NIL) {
      v->r->val = mapping(v->lazy, v->r->val);
      v->r->acc = mapping(v->lazy, v->r->acc);
      v->r->lazy = composition(v->lazy, v->r->lazy);
    }
    if (v->rev) {
      swap(v->l, v->r);
      if (v->l != NIL) {
        v->l->rev ^= 1;
        reverseprod(v->l->acc);
      }
      if (v->r != NIL) {
        v->r->rev ^= 1;
        reverseprod(v->r->acc);
      }
      v->rev = 0;
    }
    v->lazy = id();
  }

  void update(Node *v) {
    v->sumz = v->l->sumz + v->r->sumz + 1;
    v->sumw = v->l->sumw + v->r->sumw + 1;
    v->acc = op(op(v->l->acc, v->val), v->r->acc);
  }

  Node *&parentchild(Node *v) {
    if (v->p == NIL) return NIL;
    if (v->p->l == v) {
      return v->p->l;
    } else {
      return v->p->r;
    }
  }

  Node *at(int idx) {
    return A[idx].get();
  }

  void rotL(Node *v) {
    Node *p = v->p;
    if (p->p == NIL) {
      swap(p->pp, v->pp);
    } else {
      parentchild(p) = v;
    }
    v->p = p->p;
    p->p = v;
    if (v->l != NIL) v->l->p = p;
    p->r = v->l;
    v->l = p;
  }

  void rotR(Node *v) {
    Node *p = v->p;
    if (p->p == NIL) {
      swap(p->pp, v->pp);
    } else {
      parentchild(p) = v;
    }
    v->p = p->p;
    p->p = v;
    if (v->r != NIL) v->r->p = p;
    p->l = v->r;
    v->r = p;
  }

  void splay(Node *v) {
    push(v);
    while (v->p != NIL) {
      Node *p = v->p;
      Node *pp = p->p;
      if (pp != NIL) push(pp);
      push(p);
      push(v);

      // zig zag
      if (p->l == v) {
        if (pp == NIL) {
          rotR(v);
        } else if (pp->l == p) {
          rotR(p);
          rotR(v);
        } else if (pp->r == p) {
          rotR(v);
          rotL(v);
        }
      } else {
        if (pp == NIL) {
          rotL(v);
        } else if (pp->r == p) {
          rotL(p);
          rotL(v);
        } else if (pp->l == p) {
          rotL(v);
          rotR(v);
        }
      }

      if (pp != NIL) update(pp);
      update(p);
    }
    update(v);
  }

  Node *find_root(Node *v) {
    expose(v);
    while (v->l != NIL) {
      push(v);
      v = v->l;
    }
    splay(v);
    return v;
  }

  void expose(Node *v) {
    auto p = v;
    while (p != NIL) {
      splay(p);
      p = p->pp;
    }
    p = v;
    while (p->pp != NIL) {
      auto prev = p->pp->r;
      if (prev != NIL) swap(prev->pp, prev->p);
      swap(p->p, p->pp);
      p->p->r = p;
      p = p->p;
    }
    splay(v);
  }

  void evert(Node *v) {
    expose(v);
    if (v->r != NIL) {
      v->r->pp = v->r->p;
      v->r->p = NIL;
      v->r = NIL;
    }
    v->rev ^= 1;
    reverseprod(v->acc);
    push(v);
  }

  void link(Node *u, Node *v) {
    evert(u);
    evert(v);
    if (u->p != NIL or u->pp != NIL) return;
    u->pp = v;
  }

  void cut(Node *v) {
    expose(v);
    if (v->l == NIL) return;
    v->l->p = NIL;
    v->l = NIL;
  }

  Node *between(Node *u, Node *v) {
    evert(u);
    expose(v);
    push(v->l);
    return v->l;
  }

  S prod(Node *u, Node *v) {
    S res = between(u, v)->acc;
    res = op(res, v->val);
    return res;
  }

  S get(Node *v) {
    expose(v);
    return v->val;
  }

  void set(Node *v, S x) {
    expose(v);
    v->val = x;
    update(v);
  }

 public:
  // コンストラクタ
  LinkCutTree() {}
  LinkCutTree(vector<S> &w) {
    if (!pNIL) {
      pNIL = make_shared<Node>();
      NIL = pNIL.get();
      NIL->l = NIL->r = NIL->p = NIL->pp = NIL;
    }
    int n = w.size();
    A.resize(2 * n + 1);
    ed.resize(n);
    for (int i = 0; i < n; i++) {
      A[i] = make_shared<Node>(*NIL);
      A[i]->w = A[i]->sumw = 0;
      A[i]->z = A[i]->sumz = 1;
      A[i]->idx = i;
      A[i]->val = A[i]->acc = w[i];
    }

    for (int i = n; i < n * 2 - 1; i++) {
      A[i] = make_shared<Node>(*NIL);
      A[i]->w = A[i]->sumw = 1;
      A[i]->z = A[i]->sumz = 0;
      A[i]->idx = i;
      A[i]->val = A[i]->acc = e();
      unused.push(i);
    }
  }

  // u,v間のパス上に書かれた総積
  S prod(int u, int v) {
    return prod(at(u), at(v));
  }

  // 頂点idxにxを代入
  void set(int idx, S x) {
    set(at(idx), x);
  }

  // 頂点uとvを直接結ぶ辺にxを代入
  void set(int u, int v, S x) {
    if (u > v) swap(u, v);
    int edidx = ed[u][v]->idx;
    set(edidx, x);
  }

  // 頂点idxの値を取得
  S get(int idx) {
    return get(at(idx));
  }

  // 頂点uとvを直接結ぶ辺の値を取得
  S get(int u, int v) {
    if (u > v) swap(u, v);
    int edidx = ed[u][v]->idx;
    return get(edidx);
  }

  // uとvを結ぶ重みxの辺を追加
  void add(int u, int v, int x) {
    if (u > v) swap(u, v);
    int edidx = unused.front();
    unused.pop();
    S newedge;
    newedge.u = u;
    newedge.v = v;
    newedge.val = x;
    Node *edge = A[edidx].get();
    ed[u][v] = edge;
    link(at(u), at(edidx));
    link(at(edidx), at(v));
    set(edidx, newedge);
    edge->u = u;
    edge->v = v;
  }

  // uとvを結ぶ辺を削除
  void erase(int u, int v) {
    if (u > v) swap(u, v);
    Node *edge = ed[u][v];
    int edidx = edge->idx;
    unused.push(edidx);
    set(edidx, e());
    ed[u].erase(v);
    evert(at(u));
    cut(at(edidx));
    evert(at(edidx));
    cut(at(v));
    edge->u = -1;
    edge->v = -1;
  }

  bool is_connected(int u, int v) {
    return find_root(at(u)) == find_root(at(v));
  }
};
#line 3 "graph/connectivity/OfflineDynamicConnectivityLCT.hpp"

struct S {
  int val, u, v;
};

S op(S a, S b) {
  if (a.val < b.val) {
    return a;
  } else {
    return b;
  }
}

S e() {
  return {10000000, -1, -1};
}

using F = int;
S mapping(F x, S a) {
  return a;
}

F composition(F g, F f) {
  return f;
}

F id() {
  return 0;
}

void reverseprod(S a) {
}

template <class R>
class OfflineDynamicConnectivity {
 private:
  struct Query {
    int com;
    int u, v;
    int pos;
    int finish;
  };
  int Q_INF = 1e8;
  int n;
  LinkCutTree<S, op, e, F, mapping, composition, id, reverseprod> t;
  vector<map<int, int>> ed;
  vector<set<int>> msf;
  int qtime;
  vector<Query> q;

 public:
  OfflineDynamicConnectivity() {}

  OfflineDynamicConnectivity(int siz) {
    vector<S> nodew(siz, {int(1e8), -1, -1});
    t = LinkCutTree<S, op, e, F, mapping, composition, id, reverseprod>(nodew);
    ed.resize(siz);
    msf.resize(siz);
    qtime = 0;
  }

  void link(int u, int v) {
    q.push_back({0, u, v, qtime, Q_INF});
    ed[u][v] = qtime;
    ed[v][u] = qtime;
    qtime++;
  }

  void cut(int u, int v) {
    q.push_back({1, u, v, qtime, -1});
    int idx = ed[u][v];
    q[idx].finish = qtime;
    ed[u].erase(v);
    ed[v].erase(u);
    qtime++;
  }

  void same(int u, int v) {
    q.push_back({2, u, v, qtime, -1});
    qtime++;
  }

  vector<R> build() {
    int u, v, com;
    vector<R> res;
    for (int i = 0; i < qtime; i++) {
      com = q[i].com;
      int u = q[i].u;
      int v = q[i].v;
      int w = q[i].finish;
      if (com == 0) {
        if (!t.is_connected(u, v)) {
          t.add(u, v, w);
          msf[u].insert(v);
          msf[v].insert(u);
        } else {
          S path = t.prod(u, v);
          if (path.val < w and path.u != -1 and path.v != -1) {
            t.erase(path.u, path.v);
            t.add(u, v, w);
            msf[path.u].erase(path.v);
            msf[path.v].erase(path.u);
            msf[u].insert(v);
            msf[v].insert(u);
          }
        }
      } else if (com == 1) {
        if (msf[u].find(v) != msf[u].end()) {
          int p = t.prod(u, v).val;
          t.erase(u, v);
          msf[u].erase(v);
          msf[v].erase(u);
        }
      } else if (com == 2) {
        res.emplace_back(t.is_connected(u, v));
      }
    }
    return res;
  }
};
#line 4 "verify/AizuOnlineJudge/graph/connectivity/GraphConstruction2235lct.test.cpp"

using R = int;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, q;
  in(n, q);
  OfflineDynamicConnectivity<R> g(n);

  rep(i, q) {
    int com, u, v;
    in(com, u, v);
    if (com == 1) {
      g.link(u, v);
    } else if (com == 2) {
      g.cut(u, v);
    } else if (com == 3) {
      g.same(u, v);
    }
  }

  for (auto x : g.build()) {
    out(x ? "YES" : "NO");
  }
}
Back to top page