結果

問題 No.399 動的な領主
ユーザー parukiparuki
提出日時 2016-07-16 18:58:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 2,779 bytes
コンパイル時間 1,685 ms
コンパイル使用メモリ 170,636 KB
実行使用メモリ 24,832 KB
最終ジャッジ日時 2024-04-25 08:58:21
合計ジャッジ時間 3,730 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 114 ms
17,152 KB
testcase_07 AC 117 ms
17,152 KB
testcase_08 AC 109 ms
17,152 KB
testcase_09 AC 109 ms
17,280 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 77 ms
17,536 KB
testcase_13 AC 78 ms
17,536 KB
testcase_14 AC 59 ms
24,832 KB
testcase_15 AC 66 ms
24,832 KB
testcase_16 AC 67 ms
21,120 KB
testcase_17 AC 113 ms
17,280 KB
testcase_18 AC 107 ms
17,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

class LCA{
public:
    LCA(){ }
    LCA(const vector<vector<int>> &G, int root = 0):V((int)G.size()), depth(V), parent(V){
        rep(i, LG)ancestor[i] = vector<int>(V);
        dfs(root, 0, 0, G);
        rep(i, V)
            ancestor[0][i] = parent[i];
        rep(i,LG-1)rep(v,V)
            ancestor[i + 1][v] = ancestor[i][ancestor[i][v]];
    }
    int get(int u, int v){
        if(depth[u] < depth[v]) swap(u, v);
        int dist = depth[u] - depth[v];
        for(int i = 0; i < LG; ++i)
            if((dist >> i) & 1)
                u = ancestor[i][u];
        if(u == v) return u;
        for(int i = LG - 1; i >= 0; --i){
            if(ancestor[i][u] != ancestor[i][v]){
                u = ancestor[i][u];
                v = ancestor[i][v];
            }
        }
        return ancestor[0][u];
    }
    int getDepth(int u){
        return depth[u];
    }
    int goUp(int u, int len){
        for(int i = 0; i < LG; ++i)
            if(len >> i & 1)u = ancestor[i][u];
        return u;
    }
    int distance(int u, int v){
        return depth[u] + depth[v] - 2 * depth[get(u, v)];
    }
    int getParent(int u){
        return parent[u];
    }
private:
    static const int LG = 18;
    int V;
    vector<int> depth, parent, ancestor[LG];
    void dfs(int v, int p, int d, const vector<vector<int>> &G){
        depth[v] = d;
        parent[v] = p;
        for(int to : G[v]){
            if(to != p){
                dfs(to, v, d + 1, G);
            }
        }
    }
};

void dfs(int u, int pre, vector<vi> &G, vi &imos){
    each(to, G[u])if(to != pre){
        dfs(to, u, G, imos);
        imos[u] += imos[to];
    }
}


int N;

int main(){
    cin >> N;
    vector<vi> G(N);

    rep(i, N - 1){
        int u, v;
        scanf("%d%d", &u, &v);
        --u; --v;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    auto lca = LCA(G, 0);

    vi imos(N);
    int Q;
    cin >> Q;
    while(Q--){
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        int c = lca.get(a, b);

        if(c != 0)
            imos[lca.getParent(c)]--;
        imos[a]++;
        imos[b]++;
        imos[c]--;
    }
    dfs(0, -1, G, imos);
    ll ans = 0;
    each(x, imos)ans += (ll)x*(x + 1) / 2;
    cout << ans << endl;
}
0