結果

問題 No.399 動的な領主
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-10-13 18:34:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 2,000 ms
コード長 2,745 bytes
コンパイル時間 2,032 ms
コンパイル使用メモリ 181,704 KB
実行使用メモリ 30,404 KB
最終ジャッジ日時 2023-09-28 00:13:06
合計ジャッジ時間 5,267 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 16 ms
5,080 KB
testcase_06 AC 212 ms
22,396 KB
testcase_07 AC 214 ms
22,376 KB
testcase_08 AC 195 ms
22,668 KB
testcase_09 AC 199 ms
22,660 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 14 ms
4,996 KB
testcase_12 AC 160 ms
23,500 KB
testcase_13 AC 152 ms
23,544 KB
testcase_14 AC 126 ms
30,404 KB
testcase_15 AC 139 ms
30,228 KB
testcase_16 AC 146 ms
26,280 KB
testcase_17 AC 194 ms
22,560 KB
testcase_18 AC 201 ms
22,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.13 18:34:51
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;




struct LowestCommonAncestor {
private:
    int n;
    int log;
    vector<vector<int>> parent;
    vector<int> dep;
    vector<vector<int>> G;
    
    void dfs(const vector<vector<int>> &G, int v, int p, int d) {
        parent[0][v] = p;
        dep[v] = d;
        
        for (int to : G[v]) {
            if (to != p) dfs(G, to, v, d + 1);
            
        }
    }
public:
    LowestCommonAncestor(int n) : n(n) {
        G.resize(n);
    }
    void add_edge(int from, int to) {
        G[from].push_back(to);
        G[to].push_back(from);
    }
    
    void build(int root = 0) {
        log = log2(n) + 1;
        parent.resize(log, vector<int>(n));
        dep.resize(n);
        
        dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < log; k++) {
            for (int v = 0; v < G.size(); v++) {
                if (parent[k][v] < 0) {
                    parent[k + 1][v] = -1;
                }
                else {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }
    int depth(int v) {
        return dep[v];
    }
    
    int lca(int u, int v) {
        if (dep[u] > dep[v]) swap(u, v);
        for (int k = 0; k < log; k++) if ((dep[v] - dep[u]) >> k & 1) v = parent[k][v];
        if (u == v) return u;
        for (int k = log - 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 dep[u] + dep[v] - 2 * dep[lca(u, v)];
        
    }
};
vector<vector<int>> g;
vector<int> par;
vector<ll> ans;
ll kai;
void parent(int v, int p = -1) {
    par[v] = p;
    for(int u : g[v]) {
        if (u == p) continue;
        parent(u,v);
    }
}
void dfs(int v, int p = -1) {
    for(int u : g[v]) {
        if (u == p) continue;
        dfs(u,v);
        ans[v] += ans[u];
    }
    kai += ans[v] * (ans[v] + 1) / 2;
}
int main() {
    int n;cin >> n;
    g.resize(n);
    par.resize(n,-1);
    ans.resize(n,0);
    LowestCommonAncestor lca(n);
    for (int i = 0; i < n-1; i++) {
        int u,v;cin >> u >> v;
        u--;v--;
        g[u].push_back(v);
        g[v].push_back(u);
        lca.add_edge(u,v);
    }
    parent(0);
    lca.build();
    int q;cin >> q;
    while(q--) {
        int a,b;cin >> a >> b;
        a--;b--;
        int p = lca.lca(a,b);
        ans[a]++;
        ans[b]++;
        ans[p]--;
        if (par[p] != -1) ans[par[p]]--;
    }
    dfs(0);
    cout << kai << endl;
    return 0;
}
0