結果

問題 No.399 動的な領主
ユーザー zimphazimpha
提出日時 2019-10-27 16:33:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 3,922 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 211,012 KB
実行使用メモリ 12,256 KB
最終ジャッジ日時 2024-09-14 21:03:53
合計ジャッジ時間 4,372 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 84 ms
11,724 KB
testcase_07 AC 82 ms
11,732 KB
testcase_08 AC 80 ms
11,784 KB
testcase_09 AC 80 ms
11,656 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 8 ms
5,376 KB
testcase_12 AC 65 ms
12,256 KB
testcase_13 AC 64 ms
12,244 KB
testcase_14 AC 56 ms
11,700 KB
testcase_15 AC 56 ms
11,824 KB
testcase_16 AC 57 ms
11,792 KB
testcase_17 AC 81 ms
11,784 KB
testcase_18 AC 77 ms
11,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }


vector<int> t_parent;
vi t_ord;

void tree_getorder(const vector<vi> &g, int root) {
  int n = g.size();
  t_parent.assign(n, -1);
  t_ord.clear();

  vector<int> stk; stk.push_back(root);
  while(!stk.empty()) {
    int i = stk.back(); stk.pop_back();
    t_ord.push_back(i);
    for(int j = (int)g[i].size() - 1; j >= 0; j --) {
      int c = g[i][j];
      if(t_parent[c] == -1 && c != root)
        stk.push_back(c);
      else
        t_parent[i] = c;
    }
  }
}

// Schieber-Vishkin algorithm of LCA in O(n) ~ O(1)
class SchieberVishkinLCA {
public:
  // order[]: preorder of the vertex in the tree
  // parents[]: direct parent of vertex
  // root: root of the tree
  void build(const std::vector<int> &order, const std::vector<int> &parents, int root) {
    const int n = order.size();
    indices.resize(n);
    ascendant.resize(n);
    head.resize(n);

    for (int i = 0; i < n; ++i) indices[order[i]] = i + 1;
    inlabel.assign(indices.begin(), indices.end());
    for (int i = n - 1; i > 0; --i) {
      int v = order[i], p = parents[v];
      if (lowbit(inlabel[p]) < lowbit(inlabel[v])) {
        inlabel[p] = inlabel[v];
      }
    }

    ascendant[root] = 0;
    for (int i = 1; i < n; ++i) {
      int v = order[i], p = parents[v];
      ascendant[v] = ascendant[p] | lowbit(inlabel[v]);
    }

    head[0] = root;
    for (int i = 1; i < n; ++i) {
      int v = order[i], p = parents[v];
      if (inlabel[v] != inlabel[p]) head[indices[v] - 1] = p;
      else head[indices[v] - 1] = head[indices[p] - 1];
    }
  }

  // return the LCA of u and v in O(1)
  int query(int u, int v) const {
    uint Iv = inlabel[v], Iu = inlabel[u];
    uint hIv = lowbit(Iv), hIu = lowbit(Iu);
    uint mask = highbit((Iv ^ Iu) | hIv | hIu);
    uint j = lowbit(ascendant[v] & ascendant[u] & ~mask);
    int x, y;
    if (j == hIv) x = v;
    else {
      mask = highbit(ascendant[v] & (j - 1));
      x = head[(indices[v] & ~mask | (mask + 1)) - 1];
    }
    if (j == hIu) y = u;
    else {
      mask = highbit(ascendant[u] & (j - 1));
      y = head[(indices[u] & ~mask | (mask + 1)) - 1];
    }
    return indices[x] < indices[y] ? x : y;
  }

private:
  using uint = unsigned int;
  static uint lowbit(uint x) { return x & (~x + 1); }
  static uint highbit(uint x) {
    x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16;
    return x >> 1;
  }

  std::vector<uint> indices;
  std::vector<uint> inlabel;
  std::vector<uint> ascendant;
  std::vector<int> head;
};

int main() {
  int N;
  while(~scanf("%d", &N)) {
    vector<vector<int> > g(N);
    for(int i = 0; i < N - 1; ++ i) {
      int u, v;
      scanf("%d%d", &u, &v), -- u, -- v;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    tree_getorder(g, 0);
    SchieberVishkinLCA lca;
    lca.build(t_ord, t_parent, 0);
    vector<int> cnt(N);
    int Q;
    scanf("%d", &Q);
    rep(ii, Q) {
      int A; int B;
      scanf("%d%d", &A, &B), -- A, -- B;
      int C = lca.query(A, B);
      ++ cnt[A];
      ++ cnt[B];
      if(C != 0) -- cnt[t_parent[C]];
      -- cnt[C];
    }
    for(int ix = (int)t_ord.size() - 1; ix > 0; -- ix) {
      int i = t_ord[ix], p = t_parent[i];
      cnt[p] += cnt[i];
    }
    ll ans = 0;
    rep(i, N) {
      int n = cnt[i];
      ans += (ll)n * (n + 1) / 2;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
0