結果

問題 No.1787 Do Use Dynamic Tree
ユーザー ei1333333ei1333333
提出日時 2021-12-15 17:48:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,634 bytes
コンパイル時間 2,768 ms
コンパイル使用メモリ 220,220 KB
実行使用メモリ 73,304 KB
最終ジャッジ日時 2023-10-01 03:03:04
合計ジャッジ時間 20,437 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 404 ms
45,408 KB
testcase_23 AC 379 ms
59,996 KB
testcase_24 AC 325 ms
39,280 KB
testcase_25 AC 595 ms
67,184 KB
testcase_26 AC 605 ms
67,252 KB
testcase_27 AC 594 ms
67,060 KB
testcase_28 AC 107 ms
55,000 KB
testcase_29 AC 106 ms
55,136 KB
testcase_30 AC 108 ms
54,984 KB
testcase_31 AC 225 ms
69,820 KB
testcase_32 AC 327 ms
72,116 KB
testcase_33 AC 373 ms
73,304 KB
testcase_34 TLE -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* TLE解 O(QNlogN) */
/* 次数2の頂点を縮約 */
/* priority_queueで値を管理 */

#include<bits/stdc++.h>

using namespace std;

/**
 * @brief Scanner(高速入力)
 */
struct Scanner {
public:

  explicit Scanner(FILE *fp) : fp(fp) {}

  template< typename T, typename... E >
  void read(T &t, E &... e) {
    read_single(t);
    read(e...);
  }

private:
  static constexpr size_t line_size = 1 << 16;
  static constexpr size_t int_digits = 20;
  char line[line_size + 1] = {};
  FILE *fp = nullptr;
  char *st = line;
  char *ed = line;

  void read() {}

  static inline bool is_space(char c) {
    return c <= ' ';
  }

  void reread() {
    ptrdiff_t len = ed - st;
    memmove(line, st, len);
    char *tmp = line + len;
    ed = tmp + fread(tmp, 1, line_size - len, fp);
    *ed = 0;
    st = line;
  }

  void skip_space() {
    while(true) {
      if(st == ed) reread();
      while(*st && is_space(*st)) ++st;
      if(st != ed) return;
    }
  }

  template< typename T, enable_if_t< is_integral< T >::value, int > = 0 >
  void read_single(T &s) {
    skip_space();
    if(st + int_digits >= ed) reread();
    bool neg = false;
    if(is_signed< T >::value && *st == '-') {
      neg = true;
      ++st;
    }
    typename make_unsigned< T >::type y = *st++ - '0';
    while(*st >= '0') {
      y = 10 * y + *st++ - '0';
    }
    s = (neg ? -y : y);
  }

  template< typename T, enable_if_t< is_same< T, string >::value, int > = 0 >
  void read_single(T &s) {
    s = "";
    skip_space();
    while(true) {
      char *base = st;
      while(*st && !is_space(*st)) ++st;
      s += string(base, st);
      if(st != ed) return;
      reread();
    }
  }

  template< typename T >
  void read_single(vector< T > &s) {
    for(auto &d: s) read(d);
  }
};

/**
 * @brief Printer(高速出力)
 */
struct Printer {
public:
  explicit Printer(FILE *fp) : fp(fp) {}

  ~Printer() { flush(); }

  template< bool f = false, typename T, typename... E >
  void write(const T &t, const E &... e) {
    if(f) write_single(' ');
    write_single(t);
    write< true >(e...);
  }

  template< typename... T >
  void writeln(const T &...t) {
    write(t...);
    write_single('\n');
  }

  void flush() {
    fwrite(line, 1, st - line, fp);
    st = line;
  }

private:
  FILE *fp = nullptr;
  static constexpr size_t line_size = 1 << 16;
  static constexpr size_t int_digits = 20;
  char line[line_size + 1] = {};
  char small[32] = {};
  char *st = line;

  template< bool f = false >
  void write() {}

  void write_single(const char &t) {
    if(st + 1 >= line + line_size) flush();
    *st++ = t;
  }

  template< typename T, enable_if_t< is_integral< T >::value, int > = 0 >
  void write_single(T s) {
    if(st + int_digits >= line + line_size) flush();
    if(s == 0) {
      write_single('0');
      return;
    }
    if(s < 0) {
      write_single('-');
      s = -s;
    }
    char *mp = small + sizeof(small);
    typename make_unsigned< T >::type y = s;
    size_t len = 0;
    while(y > 0) {
      *--mp = y % 10 + '0';
      y /= 10;
      ++len;
    }
    memmove(st, mp, len);
    st += len;
  }

  void write_single(const string &s) {
    for(auto &c: s) write_single(c);
  }

  void write_single(const char *s) {
    while(*s != 0) write_single(*s++);
  }

  template< typename T >
  void write_single(const vector< T > &s) {
    for(size_t i = 0; i < s.size(); i++) {
      if(i) write_single(' ');
      write_single(s[i]);
    }
  }
};

/**
 * @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);
  }

  vector< vector< int > > groups() {
    int n = (int) data.size();
    vector< vector< int > > ret(n);
    for(int i = 0; i < n; i++) {
      ret[find(i)].emplace_back(i);
    }
    ret.erase(remove_if(begin(ret), end(ret), [&](const vector< int > &v) {
      return v.empty();
    }));
    return ret;
  }
};

int main() {
  Scanner in(stdin);
  Printer out(stdout);
  int N;
  in.read(N);
  vector< vector< int > > pre_g(N);
  vector< int > V(N);
  iota(begin(V), end(V), 0);
  using pi = pair< int, int >;
  vector< priority_queue< pi > > gg(N);
  vector< int > deg(N);
  for(int i = 1; i < N; i++) {
    int x, y;
    in.read(x, y);
    --x, --y;
    pre_g[x].emplace_back(y);
    pre_g[y].emplace_back(x);
    deg[x]++;
    deg[y]++;
  }
  // 次数2を縮約
  UnionFind uf(N);
  vector< vector< int > > g(N);
  vector< vector< int > > h(N);
  vector< int > L(N, -1), R(N, -1), F(N, -1), T(N, -1);
  for(int i = 0; i < N; i++) {
    for(auto j: pre_g[i]) {
      if(pre_g[i].size() <= 2 and pre_g[j].size() <= 2) {
        uf.unite(i, j);
        h[i].emplace_back(j);
      } else {
        g[i].emplace_back(j);
        gg[i].emplace(V[j], j);
      }
    }
  }
  auto groups = uf.groups();
  for(auto &group: groups) {
    if(group.size() <= 1) {
      continue;
    }
    vector< int > path;
    for(auto &p: group) {
      if(h[p].size() == 1) {
        auto dfs = [&](auto dfs, int idx, int par) -> void {
          path.emplace_back(idx);
          for(auto to: h[idx]) {
            if(to != par) dfs(dfs, to, idx);
          }
        };
        dfs(dfs, p, -1);
        break;
      }
    }
    for(int i = 0; i < (int) path.size(); i++) {
      if(i > 0) L[path[i]] = path[i - 1];
      if(i + 1 < (int) path.size()) R[path[i]] = path[i + 1];
      F[path[i]] = path.front();
      T[path[i]] = path.back();
    }
    L[path.front()] = g[path.front()].empty() ? -1 : g[path.front()][0];
    R[path.back()] = g[path.back()].empty() ? -1 : g[path.back()][0];
    g[path.front()].emplace_back(path.back());
    g[path.back()].emplace_back(path.front());
    gg[path.front()].emplace(V[path.back()], path.back());
    gg[path.back()].emplace(V[path.front()], path.front());
  }


  auto modify = [&](int idx) {
    while(V[gg[idx].top().second] != gg[idx].top().first) {
      gg[idx].pop();
    }
  };
  int x = 0;
  int Q;
  in.read(Q);
  while(Q--) {
    int u, v;
    in.read(u, v);
    --u, --v;
    (u += x) %= N;
    (v += x) %= N;
    swap(V[u], V[v]);
    for(auto &to: g[u]) {
      gg[to].emplace(V[u], u);
      modify(to);
    }
    for(auto &to: g[v]) {
      gg[to].emplace(V[v], v);
      modify(to);
    }
    int p = -1;
    if(uf.size(u) >= 2) {
      if(F[u] == u) {
        if(L[u] == -1 or V[L[u]] < V[R[u]]) {
          p = u;
          u = T[u];
        } else {
          p = u;
          u = L[u];
        }
      } else if(T[u] == u) {
        if(R[u] == -1 or V[R[u]] < V[L[u]]) {
          p = u;
          u = F[u];
        } else {
          p = u;
          u = R[u];
        }
      } else if(V[L[u]] < V[R[u]]) {
        p = F[u];
        u = T[u];
      } else {
        p = T[u];
        u = F[u];
      }
    }
    for(;;) {
      modify(u);
      int to = gg[u].top().second;
      if(p != to) {
        p = u;
        u = to;
      } else if(deg[u] >= 2) {
        while(to == gg[u].top().second) {
          gg[u].pop();
          modify(u);
        }
        int to2 = gg[u].top().second;
        gg[u].emplace(V[to], to);
        p = u;
        u = to2;
      } else {
        out.writeln(x = (u + 1));
        break;
      }
    }
  }
}
0