結果

問題 No.399 動的な領主
ユーザー kyunakyuna
提出日時 2019-08-21 00:44:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,267 bytes
コンパイル時間 1,021 ms
コンパイル使用メモリ 82,216 KB
実行使用メモリ 24,240 KB
最終ジャッジ日時 2024-04-16 04:29:31
合計ジャッジ時間 4,524 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 17 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 16 ms
5,376 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
    int res = 0;
    for (int v = 0; v < N; v++) res += sum[v] * (sum[v] + 1) / 2;
    cout << res << endl;
    return 0;
}
0