結果

問題 No.399 動的な領主
ユーザー mamekinmamekin
提出日時 2016-10-08 00:10:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 4,287 bytes
コンパイル時間 1,669 ms
コンパイル使用メモリ 125,704 KB
実行使用メモリ 23,168 KB
最終ジャッジ日時 2024-05-01 15:08:51
合計ジャッジ時間 6,187 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 18 ms
6,940 KB
testcase_06 AC 229 ms
21,164 KB
testcase_07 AC 244 ms
20,736 KB
testcase_08 AC 230 ms
21,072 KB
testcase_09 AC 241 ms
20,096 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 15 ms
6,944 KB
testcase_12 AC 171 ms
18,624 KB
testcase_13 AC 174 ms
18,916 KB
testcase_14 AC 157 ms
23,168 KB
testcase_15 AC 172 ms
23,168 KB
testcase_16 AC 181 ms
21,760 KB
testcase_17 AC 218 ms
19,072 KB
testcase_18 AC 202 ms
20,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

template <class T>
class EdgeBase
{
public:
    int to;
    T cost;
    EdgeBase(){};
    EdgeBase(int to0, T cost0){to = to0; cost = cost0;}
};
typedef EdgeBase<int> Edge;

template<class T>
class LowestCommonAncestor
{
private:
    vector<vector<int> > to; // ダブリング先のノード
    vector<int> depth;       // 根からの深さ
    vector<T> dist;          // 根からの距離
public:
    int climb(int curr, int len)
    {
        int i = 0;
        while(len > 0){
            if(len % 2 == 1)
                curr = to[curr][i];
            len /= 2;
            ++ i;
        }
        return curr;
    }
    LowestCommonAncestor(const vector<vector<EdgeBase<T> > >& edges, int root)
    {
        int n = edges.size();
        to.assign(n, vector<int>());
        dist.assign(n, 0);
        depth.assign(n, 0);

        queue<pair<int, int> > q;
        q.push(make_pair(root, -1));

        int cnt = 0;
        while(!q.empty()){
            int m = q.size();
            while(--m >= 0){
                int curr, prev;
                tie(curr, prev) = q.front();
                q.pop();

                if(prev != -1){
                    to[curr].push_back(prev);
                    int j = prev;
                    for(unsigned k=0; k<to[j].size(); ++k){
                        j = to[j][k];
                        to[curr].push_back(j);
                    }
                }

                for(const EdgeBase<T>& e : edges[curr]){
                    if(e.to != prev){
                        depth[e.to] = depth[curr] + 1;
                        dist[e.to] = dist[curr] + e.cost;
                        q.push(make_pair(e.to, curr));
                    }
                }
            }
            ++ cnt;
        }
    }
    // 2つのノードの最小共通祖先を取得
    int getAncestor(int a, int b)
    {
        int diff = depth[a] - depth[b];
        if(diff < 0)
            b = climb(b, -diff);
        else
            a = climb(a, diff);
        if(a == b)
            return a;

        for(int i=to[a].size()-1; i>=0; --i){
            if(i < (int)to[a].size() && to[a][i] != to[b][i]){
                a = to[a][i];
                b = to[b][i];
            }
        }
        return to[a][0];
    }
    // ノードの深さを取得
    int getDepth(int a)
    {
        return depth[a];
    }
    // 2つのノードの距離を取得
    T getDist(int a, int b)
    {
        int c = getAncestor(a, b);
        return dist[a] + dist[b] - dist[c] * 2;
    }
};

int main()
{
    int n;
    cin >> n;
    vector<vector<Edge> > edges(n);
    for(int i=0; i<n-1; ++i){
        int u, v;
        cin >> u >> v;
        -- u;
        -- v;
        edges[u].push_back(Edge(v, 1));
        edges[v].push_back(Edge(u, 1));
    }

    int root = 0;
    LowestCommonAncestor<int> lca(edges, root);

    int query;
    cin >> query;
    vector<int> cnt(n);
    while(--query >= 0){
        int a, b;
        cin >> a >> b;
        -- a;
        -- b;
        ++ cnt[a];
        ++ cnt[b];

        int c = lca.getAncestor(a, b);
        -- cnt[c];
        if(c != root){
            int d = lca.climb(c, 1);
            -- cnt[d];
        }
    }

    queue<int> q;
    vector<int> index;
    vector<int> parent(n, -1);
    q.push(root);
    index.push_back(root);
    while(!q.empty()){
        int curr = q.front();
        q.pop();
        for(const Edge& e : edges[curr]){
            if(e.to != root && parent[e.to] == -1){
                q.push(e.to);
                index.push_back(e.to);
                parent[e.to] = curr;
            }
        }
    }
    for(int i=n-1; i>0; --i)
        cnt[parent[index[i]]] += cnt[index[i]];

    long long ans = 0;
    for(int i=0; i<n; ++i)
        ans += cnt[i] * (cnt[i] + 1LL) / 2;
    cout << ans << endl;

    return 0;
}
0