結果

問題 No.399 動的な領主
ユーザー kyunakyuna
提出日時 2019-08-21 00:45:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 2,279 bytes
コンパイル時間 1,191 ms
コンパイル使用メモリ 81,584 KB
実行使用メモリ 23,992 KB
最終ジャッジ日時 2024-04-16 04:29:39
合計ジャッジ時間 4,510 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 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 18 ms
5,376 KB
testcase_06 AC 249 ms
16,208 KB
testcase_07 AC 248 ms
16,336 KB
testcase_08 AC 245 ms
16,264 KB
testcase_09 AC 230 ms
16,268 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 16 ms
5,376 KB
testcase_12 AC 180 ms
16,856 KB
testcase_13 AC 181 ms
16,712 KB
testcase_14 AC 139 ms
23,988 KB
testcase_15 AC 152 ms
23,992 KB
testcase_16 AC 162 ms
20,016 KB
testcase_17 AC 228 ms
16,268 KB
testcase_18 AC 229 ms
16,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

using UnWeightedGraph = vector<vector<int>>;
struct LowestCommonAncestor {
    const UnWeightedGraph &g;
    int root;
    vector<vector<int>> parent;         // parent[k][v] := 2^k-th parent of v
    vector<int> depth;
    LowestCommonAncestor(const UnWeightedGraph &g, int r = 0) : \
        g(g), root(r), depth(g.size()) { }
    void build() {
        int V = g.size();
        int h = 1; while ((1 << h) < V) ++h;    // 32 - __builtin_clz(V)
        parent.assign(h, vector<int>(V, -1));
        dfs(root, -1, 0);
        for (int k = 0; k + 1 < h; ++k) for (int v = 0; v < V; ++v) {
            if (parent[k][v] != -1) parent[k + 1][v] = parent[k][parent[k][v]];
        }
    }
    void dfs(int u, int p, int d) {
        parent[0][u] = p;
        depth[u] = d;
        for (auto v: g[u]) if (v != p) dfs(v, u, d + 1);
    }
    int get(int u, int v) {
        if (depth[u] > depth[v]) swap(u, v);
        for (int k = 0; k < parent.size(); ++k) {
            if ((depth[v] - depth[u]) >> k & 1) v = parent[k][v];
        }
        if (u == v) return u;
        for (int k = parent.size() - 1; k >= 0; --k) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u]; v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int dist(int u, int v) { return depth[u] + depth[v] - depth[get(u, v)] * 2; }
};

vector<int> sum;
void rec(const UnWeightedGraph &g, int u, int p) {
    for (auto v: g[u]) if (v != p) {
        rec(g, v, u);
        sum[u] += sum[v];
    }
}

int main() {
    int N; cin >> N;
    UnWeightedGraph g(N);
    for (int i = 0; i < N - 1; i++) {
        int u, v; cin >> u >> v; u--, v--;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    LowestCommonAncestor lca(g);
    lca.build();
    sum.resize(N + 1);
    int Q; cin >> Q;
    while (Q--) {
        int a, b; cin >> a >> b; a--, b--;
        int p = lca.get(a, b);
        int pp = lca.parent[0][p];
        if (pp == -1) pp = N;
        sum[a]++, sum[pp]--;
        sum[b]++, sum[p]--;
    }
    rec(g, 0, -1);
    long long res = 0;
    for (int v = 0; v < N; v++) res += 1LL * sum[v] * (sum[v] + 1) / 2;
    cout << res << endl;
    return 0;
}
0