結果

問題 No.898 tri-βutree
ユーザー renjyaku_intrenjyaku_int
提出日時 2020-03-27 21:56:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 215 ms / 4,000 ms
コード長 4,319 bytes
コンパイル時間 2,040 ms
コンパイル使用メモリ 181,636 KB
実行使用メモリ 31,420 KB
最終ジャッジ日時 2024-04-26 09:20:42
合計ジャッジ時間 7,574 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
31,420 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 215 ms
25,728 KB
testcase_08 AC 212 ms
25,728 KB
testcase_09 AC 204 ms
25,600 KB
testcase_10 AC 205 ms
25,724 KB
testcase_11 AC 202 ms
25,592 KB
testcase_12 AC 207 ms
25,712 KB
testcase_13 AC 204 ms
25,724 KB
testcase_14 AC 210 ms
25,592 KB
testcase_15 AC 206 ms
25,684 KB
testcase_16 AC 213 ms
25,640 KB
testcase_17 AC 207 ms
25,720 KB
testcase_18 AC 207 ms
25,720 KB
testcase_19 AC 205 ms
25,720 KB
testcase_20 AC 199 ms
25,600 KB
testcase_21 AC 215 ms
25,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#include <tourist>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18) + 1;
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int n, m;
vector<int> a;
template <typename T>
struct edge
{
    int src, to;
    T cost;

    edge(int to, T cost) : src(-1), to(to), cost(cost) {}

    edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

    edge &operator=(const int &x)
    {
        to = x;
        return *this;
    }

    operator int() const { return to; }
};

template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T>
using Matrix = vector<vector<T>>;
template <typename G>
struct DoublingLowestCommonAncestor
{
    const int LOG;
    vector<int> dep;
    const G &g;
    vector<vector<int>> table;

    DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size()))
    {
        table.assign(LOG, vector<int>(g.size(), -1));
    }

    void dfs(int idx, int par, int d)
    {
        table[0][idx] = par;
        dep[idx] = d;
        for (auto &to : g[idx])
        {
            if (to != par)
                dfs(to, idx, d + 1);
        }
    }

    void build()
    {
        dfs(0, -1, 0);
        for (int k = 0; k + 1 < LOG; k++)
        {
            for (int i = 0; i < table[k].size(); i++)
            {
                if (table[k][i] == -1)
                    table[k + 1][i] = -1;
                else
                    table[k + 1][i] = table[k][table[k][i]];
            }
        }
    }

    int query(int u, int v)
    {
        if (dep[u] > dep[v])
            swap(u, v);
        for (int i = LOG - 1; i >= 0; i--)
        {
            if (((dep[v] - dep[u]) >> i) & 1)
                v = table[i][v];
        }
        if (u == v)
            return u;
        for (int i = LOG - 1; i >= 0; i--)
        {
            if (table[i][u] != table[i][v])
            {
                u = table[i][u];
                v = table[i][v];
            }
        }
        return table[0][u];
    }
};
vector<vector<pair<ll,ll>>> v;
vector<ll> dep;
void dfs(int k,int prev,ll d){
    dep[k]=d;
    rep(i,v[k].size()){
        if(v[k][i].first==prev)continue;
        dfs(v[k][i].first,k,d+v[k][i].second);
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    v.resize(n);
    UnWeightedGraph g(n);
    for (int i = 0; i < n-1; i++)
    {
        ll temp,temp1,temp2;
        cin >> temp>>temp1>>temp2;
        v[temp].push_back(make_pair(temp1,temp2));
        v[temp1].push_back(make_pair(temp,temp2));
        g[temp].push_back(temp1);
        g[temp1].push_back(temp);
    }
    dep.resize(n);
    dfs(0,-1,0);
    DoublingLowestCommonAncestor<UnWeightedGraph> lca(g);
    lca.build();
    int q;
    cin>>q;
    vector<ll> ans;
    while(q--){
        ll x,y,z;
        cin>>x>>y>>z;
        ll sum=0;
        int temp=lca.query(x,y);
        sum+=dep[x]-dep[temp];
        sum+=dep[y]-dep[temp];
        temp=lca.query(z,y);
        sum+=dep[z]-dep[temp];
        sum+=dep[y]-dep[temp];
        temp=lca.query(x,z);
        sum+=dep[x]-dep[temp];
        sum+=dep[z]-dep[temp];
        ans.push_back(sum/2);
    }
    rep(i,ans.size())
    cout << ans[i] << "\n";
}
0