結果

問題 No.898 tri-βutree
ユーザー 👑 tute7627tute7627
提出日時 2019-10-04 21:33:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 312 ms / 4,000 ms
コード長 4,005 bytes
コンパイル時間 2,246 ms
コンパイル使用メモリ 181,536 KB
実行使用メモリ 50,148 KB
最終ジャッジ日時 2024-04-26 08:26:46
合計ジャッジ時間 8,945 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
50,148 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 295 ms
45,720 KB
testcase_08 AC 298 ms
45,836 KB
testcase_09 AC 282 ms
45,692 KB
testcase_10 AC 294 ms
45,696 KB
testcase_11 AC 312 ms
45,824 KB
testcase_12 AC 298 ms
45,824 KB
testcase_13 AC 293 ms
45,696 KB
testcase_14 AC 295 ms
45,696 KB
testcase_15 AC 283 ms
45,696 KB
testcase_16 AC 280 ms
45,696 KB
testcase_17 AC 274 ms
45,696 KB
testcase_18 AC 269 ms
45,696 KB
testcase_19 AC 293 ms
45,696 KB
testcase_20 AC 285 ms
45,820 KB
testcase_21 AC 295 ms
45,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define lfs <<fixed<<setprecision(10)<<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = n; i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = n - 1; i >= (ll)(m); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>
void debug(vector<vector<T>>v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>v,ll n){if(n!=0)cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
template<typename T>
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

struct edge {
  ll to,cost;
  edge(ll x,ll y):to(x),cost(y){};
  edge():to(0LL),cost(0LL){};
}; 

struct LCA{
  ll datasize = 21;
  ll n,root;//rootを根とする頂点数nの根付き木
  vector<ll>depth,dist;//0から
  vector<vector<struct edge>>G;
  vector<vector<ll>>data;
  LCA(vector<vector<struct edge>>g,ll r):G(g),n(g.size()),root(r){
    data.assign(n,vector<ll>(datasize,-1));
    depth.assign(n,-1LL);
    dist.assign(n,-1LL);
    build();
  }
  void build(){
    dfs(root,0,0);
    for(ll i=1;i<datasize;i++){
      for(ll j=0;j<n;j++){
        if(data[j][i-1]==-1)data[j][i]=-1;
        else data[j][i]=data[data[j][i-1]][i-1];
      }
    }
  }
  void dfs(ll k,ll d,ll x){
    depth[k]=d;
    dist[k]=x;
    for(ll i=0;i<G[k].size();i++){
      if(depth[G[k][i].to]==-1){
        data[G[k][i].to][0]=k;
        dfs(G[k][i].to,d+1,x+G[k][i].cost);
      }
    }
  }
  ll deep(ll x){
    return x>=0?depth[x]:-1;
  }
  ll query(ll x,ll y){
    if(depth[x]<depth[y])swap(x,y);
    ll tmp = datasize - 1;
    while(depth[x]!=depth[y]){
      while(tmp>=1&&depth[y]>=deep(data[x][tmp]))tmp--;
      x = data[x][tmp];
    }
    tmp = datasize - 1;
    while(x!=y){
      while(tmp>=1&&data[x][tmp]==data[y][tmp])tmp--;
      x = data[x][tmp];
      y = data[y][tmp];
    }
    return x;
  }
};

int main(){
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  ll res=0,res1=INF,res2=-INF,buf=0;
  bool judge = true;
  ll n;cin>>n;
  vector<vector<struct edge>>g(n);
  for(ll i=0;i<n-1;i++){
    ll u,v,c;
    cin>>u>>v>>c;
    g[u].emplace_back(v,c);
    g[v].emplace_back(u,c);
  }
  struct LCA lca(g,0);
  ll q;cin>>q;
  while(q--){
    vector<ll>x(3);cin>>x[0]>>x[1]>>x[2];
    ll ret=0;
    rep(i,0,2)rep(j,i+1,3){
      ll r=lca.query(x[i],x[j]);
      ret+=lca.dist[x[i]]+lca.dist[x[j]]-lca.dist[r]*2;
      //cout<<lca.dist[x[i]] spa lca.dist[x[j]] spa lca.dist[r]*2<<endl;
    }
    cout<<ret/2<<endl;
  }

  return 0;
}
0