結果

問題 No.898 tri-βutree
ユーザー pionepione
提出日時 2021-01-29 05:10:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 392 ms / 4,000 ms
コード長 4,727 bytes
コンパイル時間 2,020 ms
コンパイル使用メモリ 191,544 KB
実行使用メモリ 28,392 KB
最終ジャッジ日時 2024-04-26 09:33:55
合計ジャッジ時間 9,677 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 232 ms
28,392 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 352 ms
19,364 KB
testcase_08 AC 345 ms
19,492 KB
testcase_09 AC 366 ms
19,364 KB
testcase_10 AC 383 ms
19,360 KB
testcase_11 AC 392 ms
19,356 KB
testcase_12 AC 362 ms
19,484 KB
testcase_13 AC 337 ms
19,488 KB
testcase_14 AC 357 ms
19,364 KB
testcase_15 AC 354 ms
19,336 KB
testcase_16 AC 353 ms
19,352 KB
testcase_17 AC 367 ms
19,360 KB
testcase_18 AC 340 ms
19,356 KB
testcase_19 AC 368 ms
19,360 KB
testcase_20 AC 350 ms
19,364 KB
testcase_21 AC 348 ms
19,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define irreps(i, m, n) for (long long i = ((long long)(n)-1); i > (long long)(m); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) (long long)(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

const ll INF = 1e9+10;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

/**
 * 3頂点を辺数が最小になるように結ぶ
 * その際の辺のコストを求める
 * lcaを重みを含むように拡張する
 * a,b,cを接続する
 * a,b b,c or a,b a,c or b,c a,c .. どの頂点を中間点とするか
 * 3頂点のコスト: 2頂点のコスト+depth(残り)-lca?
 * 位置関係で変わる
 * どういう順でもよい
 * dist(a,b)+dist(a,c)-dist(a,lca(a,b))で求まる?
 * lcaを求めるには辺の数について見る必要がある=>重みでは高さを揃える、lcaの一歩手前まで遡るができない
 * 単純にdist(a,b),dist(a,c)のうち大きい方+小さい方-dist(小さいの組のlca,a(b))
 * 必ずしも遠い方+近い方の片道 になるとは限らない
 * (a,b),(a,c)のlcaのうちrootに近い方+遠い方の片道
 * => 1直線に3点あるとき成り立たない
 * 
 * (a,b),(c,a)だけでは不十分
 * (a,b),(b,c),(c,a)のlcaで最も深い2組の距離 + そのlcaともう1つの距離
 */

class LCA{
  private:
  int n;
  int root;
  int k; // n<=2^kとなる最小のk
  vc<vc<int>> dp; // dp[i][j]:=要素jの2^i上の要素
  vc<ll> depth;  // depth[i]:=rootに対する頂点iの深さ
  vll dist;
  
  public:
  LCA(int _n, int _root, vc<vc<pair<int,ll>>>& _G){
    n=_n;
    root=_root;
    k=1;
    int nibeki=2;
    while(nibeki<n){
      nibeki<<=1;
      k++;
    }
    // 頂点iの親ノードを初期化
    dp = vc<vc<int>>(k+1, vc<int>(n, -1));
    depth.resize(n);
    dist.resize(n);
    function<void(int, int)> _dfs=[&](int v, int p){
      dp[0][v]=p;
      for(auto np: _G[v]){
        int nv; ll c; tie(nv,c)=np;
        if(nv==p) continue;
        depth[nv]=depth[v]+1;
        dist[nv]=dist[v]+c;
        _dfs(nv, v); 
      }
    };
    _dfs(root, -1);
    // ダブリング
    rep(i,k){
      rep(j,n){
        dp[i+1][j]=dp[i][dp[i][j]];
      }
    }
  }
  
  int get(int u, int v){
    if(depth[u]<depth[v]) swap(u,v); // u側を深くする
    if(depth[u]!=depth[v]){
      ll d=depth[u]-depth[v];
      rep(i,k) if((d>>i)&1) u=dp[i][u];
    }
    if(u==v) return u;
    
    rrep(i,k){
      if(dp[i][u]!=dp[i][v]){
        u=dp[i][u], v=dp[i][v];
      }
    }
    return dp[0][u];
  }
  
  ll getDist(int u, int v){
    int lca=get(u,v);
    return dist[u]+dist[v]-2*dist[lca];
  }
  
  ll getDist(int a, int b, int c){
    int lca_ab=get(a,b), lca_bc=get(b,c), lca_ca=get(a,c);
    vtp v(3);
    v[0]=tuple<ll,ll,ll>(depth[lca_ab], getDist(a,b), getDist(lca_ab,c));
    v[1]=tuple<ll,ll,ll>(depth[lca_bc], getDist(b,c), getDist(lca_bc,a));
    v[2]=tuple<ll,ll,ll>(depth[lca_ca], getDist(c,a), getDist(lca_ca,b));
    sort(v.rbegin(),v.rend());
    ll cost, x, y; tie(cost,x,y)=v[0];
    return x+y;
  }
  
};

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  
  ll n; cin>>n;
  vc<vc<pair<int,ll>>> G(n);
  rep(i,n-1){
    ll a,b,c; cin>>a>>b>>c;
    G[a].emplace_back(b,c);
    G[b].emplace_back(a,c);
  }
  auto lca=LCA(n, 0, G);
  ll q; cin>>q;
  while(q--){
    ll a,b,c; cin>>a>>b>>c;
    cout<<lca.getDist(a,b,c)<<endl;
  }
  
}
0