結果

問題 No.399 動的な領主
ユーザー kyunakyuna
提出日時 2020-02-24 00:55:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 2,300 bytes
コンパイル時間 950 ms
コンパイル使用メモリ 84,796 KB
実行使用メモリ 23,228 KB
最終ジャッジ日時 2024-04-19 08:31:33
合計ジャッジ時間 4,128 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 1 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 15 ms
5,376 KB
testcase_06 AC 221 ms
17,112 KB
testcase_07 AC 227 ms
16,856 KB
testcase_08 AC 225 ms
17,040 KB
testcase_09 AC 218 ms
17,040 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 15 ms
5,376 KB
testcase_12 AC 179 ms
17,508 KB
testcase_13 AC 175 ms
17,484 KB
testcase_14 AC 132 ms
23,228 KB
testcase_15 AC 146 ms
23,100 KB
testcase_16 AC 161 ms
20,156 KB
testcase_17 AC 210 ms
17,048 KB
testcase_18 AC 219 ms
17,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

vector<vector<int>> g;
vector<long long> imos;
void dfs(int u, int p) {
    for (int v: g[u]) if (v != p) {
        dfs(v, u);
        imos[u] += imos[v];
    }
}

int main() {
    int n; cin >> n;
    g.resize(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);
    imos.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.par[0][p];
        if (pp == -1) pp = n;
        imos[a]++, imos[p]--;
        imos[b]++, imos[pp]--;
    }
    dfs(0, -1);
    long long sum = 0;
    for (int i = 0; i < n; i++) sum += imos[i] * (imos[i] + 1) / 2;
    cout << sum << endl;
    return 0;
}
0