結果

問題 No.399 動的な領主
ユーザー drken1215drken1215
提出日時 2018-08-13 23:02:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 2,196 bytes
コンパイル時間 817 ms
コンパイル使用メモリ 80,240 KB
実行使用メモリ 24,640 KB
最終ジャッジ日時 2023-10-24 16:51:20
合計ジャッジ時間 3,186 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 12 ms
4,708 KB
testcase_06 AC 159 ms
16,748 KB
testcase_07 AC 156 ms
16,744 KB
testcase_08 AC 149 ms
16,804 KB
testcase_09 AC 148 ms
16,804 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 12 ms
4,736 KB
testcase_12 AC 120 ms
17,272 KB
testcase_13 AC 118 ms
17,252 KB
testcase_14 AC 95 ms
24,640 KB
testcase_15 AC 107 ms
24,640 KB
testcase_16 AC 117 ms
20,680 KB
testcase_17 AC 147 ms
16,804 KB
testcase_18 AC 149 ms
16,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef vector<vector<int> > Graph;
struct LCA {
    vector<vector<int> > parent; // parent[d][v] := 2^d-th parent of v
    vector<int> depth;
    LCA() { }
    LCA(const Graph &G, int r = 0) { init(G, r); }
    void init(const Graph &G, int r = 0) {
        int V = (int)G.size();
        int h = 1;
        while ((1<<h) < V) ++h;
        parent.assign(h, vector<int>(V, -1));
        depth.assign(V, -1);
        dfs(G, r, -1, 0);
        for (int i = 0; i+1 < (int)parent.size(); ++i)
            for (int v = 0; v < V; ++v)
                if (parent[i][v] != -1)
                    parent[i+1][v] = parent[i][parent[i][v]];
    }
    void dfs(const Graph &G, int v, int p, int d) {
        parent[0][v] = p;
        depth[v] = d;
        for (auto e : G[v]) if (e != p) dfs(G, e, v, d+1);
    }
    int get(int u, int v) {
        if (depth[u] > depth[v]) swap(u, v);
        for (int i = 0; i < (int)parent.size(); ++i)
            if ( (depth[v] - depth[u]) & (1<<i) )
                v = parent[i][v];
        if (u == v) return u;
        for (int i = (int)parent.size()-1; i >= 0; --i) {
            if (parent[i][u] != parent[i][v]) {
                u = parent[i][u];
                v = parent[i][v];
            }
        }
        return parent[0][u];
    }
};

vector<long long> sum;
void rec(const Graph &G, int v, int p) {
    for (auto e : G[v]) {
        if (e == p) continue;
        rec(G, e, v);
        sum[v] += sum[e];
    }
}

int main() {
    int N, Q;
    cin >> N;
    Graph 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);
    }
    LCA lca(G);
    cin >> Q; sum.assign(N+1, 0);
    for (int q = 0; q < Q; ++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]--;
        //cout << a << ", " << b << ": " << p << endl;
    }
    rec(G, 0, -1);
    long long res = 0;
    for (int v = 0; v < N; ++v) res += sum[v] * (sum[v] + 1) / 2;
    cout << res << endl;
}
0