結果

問題 No.399 動的な領主
ユーザー しらっ亭しらっ亭
提出日時 2016-07-21 23:07:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 2,842 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 176,936 KB
実行使用メモリ 24,528 KB
最終ジャッジ日時 2024-04-25 09:10:10
合計ジャッジ時間 3,735 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 9 ms
5,376 KB
testcase_06 AC 129 ms
16,744 KB
testcase_07 AC 118 ms
16,744 KB
testcase_08 AC 118 ms
16,928 KB
testcase_09 AC 113 ms
16,932 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 8 ms
5,376 KB
testcase_12 AC 83 ms
17,264 KB
testcase_13 AC 82 ms
17,248 KB
testcase_14 AC 59 ms
24,520 KB
testcase_15 AC 69 ms
24,528 KB
testcase_16 AC 73 ms
20,556 KB
testcase_17 AC 110 ms
16,804 KB
testcase_18 AC 113 ms
16,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define ALL(x) (x).begin(), (x).end()
#define BIT(n) (1LL<<(n))
#define SZ(x) ((int)(x).size())
#define fst first
#define snd second
typedef vector<int> vi;typedef vector<vi> vvi;typedef pair<int,int> pii;typedef vector<pii> vpii;
typedef long long ll;

struct LCA {
  int H, W;
  int V;
  vector<vector<int> > parent;
  vector<int> depth;

  // LCA(木の頂点数)
  LCA(vector<vector<int> >& E) : W(E.size()), depth(E.size()) {
    H = 1;
    while (1 << H < W) H++;
    parent.resize(H, vector<int>(W));
    // 適当な頂点からdfsして親を求めておく
    dfs(E, 0, -1, 0);
    // 親をダブリングする
    for (int k = 1; k < H; k++) {
      for (int v = 0; v < W; v++) {
        parent[k][v] = parent[k-1][v] == -1 ? -1 : parent[k-1][parent[k-1][v]];
      }
    }
  }

  void dfs(vector<vector<int> >& E, int cur, int par, int dep) {
    parent[0][cur] = par;
    depth[cur] = dep;
    for (int i = 0; i < (int) E[cur].size(); i++) {
      if (E[cur][i] != par) dfs(E, E[cur][i], cur, dep + 1);
    }
  }

  // u と v の LCA を求める
  int query(int u, int v) {
    // 深い方を v にしておく
    if (depth[u] > depth[v]) swap(u, v);
    // 根からの深さが u と同じになるまで v の親をたどる
    for (int k = 0; k < H; k++) {
      if ((depth[v]-depth[u]) >> k & 1) {
        v = parent[k][v];
      }
    }
    if (u == v) return u;
    // 二分探索でLCAを求める
    for (int k = H - 1; k >= 0; k--) {
      if (parent[k][u] != parent[k][v]) {
        u = parent[k][u];
        v = parent[k][v];
      }
    }
    return parent[0][u];
  }
};

void Main() {
  int N;
  scanf("%d", &N);
  vvi E(N);
  rep(i, N-1) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--; v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }

  LCA lca(E);
  vi &par = lca.parent[0];
  vector<ll> C(N);

  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--; b--;
    int l = lca.query(a, b);
    int p = par[l];
    C[a]++;
    C[b]++;
    C[l]--;
    if (p >= 0) C[p]--;
  }

  function<void(int)> dfs = [&](int cur) {
    forr(nex, E[cur]) if (nex != par[cur]) {
      dfs(nex);
      C[cur] += C[nex];
    }
  };

  dfs(0);

  ll ans = 0;
  rep(i, N) ans += C[i] * (C[i]+1) / 2;

  _p("%lld\n", ans);
}
int main() { cin.tie(0); ios::sync_with_stdio(false); Main(); return 0; }
0