結果

問題 No.399 動的な領主
ユーザー fumofumofunifumofumofuni
提出日時 2021-09-18 02:30:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 3,525 bytes
コンパイル時間 2,404 ms
コンパイル使用メモリ 213,668 KB
実行使用メモリ 23,720 KB
最終ジャッジ日時 2023-09-12 12:13:18
合計ジャッジ時間 7,361 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
14,344 KB
testcase_01 AC 12 ms
13,988 KB
testcase_02 AC 12 ms
13,944 KB
testcase_03 AC 11 ms
14,264 KB
testcase_04 AC 13 ms
14,128 KB
testcase_05 AC 25 ms
14,344 KB
testcase_06 AC 236 ms
17,632 KB
testcase_07 AC 229 ms
17,636 KB
testcase_08 AC 220 ms
17,820 KB
testcase_09 AC 226 ms
17,956 KB
testcase_10 AC 13 ms
14,088 KB
testcase_11 AC 24 ms
14,388 KB
testcase_12 AC 176 ms
18,412 KB
testcase_13 AC 172 ms
18,128 KB
testcase_14 AC 127 ms
23,720 KB
testcase_15 AC 140 ms
23,536 KB
testcase_16 AC 151 ms
21,080 KB
testcase_17 AC 222 ms
17,792 KB
testcase_18 AC 224 ms
17,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
} 

struct Edge {
    long long to;
};
using Graph = vector<vector<Edge>>;
struct LCA {
    vector<vector<int>> parent;  // parent[k][u]:= u の 2^k 先の親
    vector<int> dist;            // root からの距離
    LCA(const Graph &G, int root = 0) { init(G, root); }
    void init(const Graph &G, int root = 0) {
        int V = G.size();
        int K = 1;
        while ((1 << K) < V) K++;
        parent.assign(K, vector<int>(V, -1));
        dist.assign(V, -1);
        dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < K; k++) {
            for (int v = 0; v < V; v++) {
                if (parent[k][v] >= 0) {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }
    // 根からの距離と1つ先の頂点を求める
    void dfs(const Graph &G, int v, int p, int d) {
        parent[0][v] = p;
        dist[v] = d;
        for (auto e : G[v]) {
            if (e.to != p) {
                dfs(G, e.to, v, d + 1);
            }
        }
    }
    int query(int u, int v) {
        if (dist[u] < dist[v]) swap(u, v);  // u の方が深いとする
        int K = parent.size();
        // LCA までの距離を同じにする
        for (int k = 0; k < K; k++) {
            if ((dist[u] - dist[v])&(1<<k) ){
                u = parent[k][u];
            }
        }
        // 二分探索で LCA を求める
        if (u == v) return u;
        for (int k = K - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int length(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; }
    bool is_in(int u, int v, int a) { return length(u, a) + length(a, v) == length(u, v); }
};

vector<vector<Edge>> g(100010);
vl dp(100010);
vl lc(100010);
ll dfs(ll v,ll par=-1){
    for(auto p:g[v]){
        if(p.to==par)continue;
        dp[v]+=dfs(p.to,v);
    }
    dp[v]-=lc[v];
    return dp[v]-lc[v];
}
int main(){
    ll n;cin >> n;
    rep(i,n-1){
        ll a,b;cin >> a >> b;a--;b--;
        g[a].push_back({b});
        g[b].push_back({a});
    }
    LCA lca(g);
    ll q;cin >> q;
    while(q--){
        ll a,b;cin >> a >> b;a--;b--;
        ll l=lca.query(a,b);
        lc[l]+=1;
        dp[a]+=1;
        dp[b]+=1;
    }
    dfs(0);
    ll ans=0;
    rep(i,n){
        ans+=dp[i]*(dp[i]+1)/2;
    }
    cout << ans << endl;
}   
0