結果

問題 No.901 K-ary εxtrεεmε
ユーザー pionepione
提出日時 2021-01-29 14:55:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 3,000 ms
コード長 3,795 bytes
コンパイル時間 2,345 ms
コンパイル使用メモリ 186,000 KB
実行使用メモリ 24,052 KB
最終ジャッジ日時 2023-09-09 09:09:46
合計ジャッジ時間 10,322 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
24,052 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 127 ms
19,080 KB
testcase_08 AC 130 ms
18,944 KB
testcase_09 AC 127 ms
18,940 KB
testcase_10 AC 126 ms
19,184 KB
testcase_11 AC 125 ms
18,948 KB
testcase_12 AC 111 ms
18,988 KB
testcase_13 AC 112 ms
18,996 KB
testcase_14 AC 108 ms
18,936 KB
testcase_15 AC 107 ms
19,132 KB
testcase_16 AC 110 ms
19,080 KB
testcase_17 AC 179 ms
19,068 KB
testcase_18 AC 178 ms
18,936 KB
testcase_19 AC 179 ms
19,148 KB
testcase_20 AC 188 ms
18,936 KB
testcase_21 AC 185 ms
19,076 KB
testcase_22 AC 108 ms
19,376 KB
testcase_23 AC 113 ms
19,204 KB
testcase_24 AC 105 ms
19,348 KB
testcase_25 AC 103 ms
19,268 KB
testcase_26 AC 103 ms
19,300 KB
testcase_27 AC 220 ms
19,052 KB
testcase_28 AC 225 ms
19,140 KB
testcase_29 AC 219 ms
19,128 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;

class LCA{
  private:
  int n;
  int root;
  int k; // n<=2^kとなる最小のk
  vc<vc<int>> dp; // dp[i][j]:=要素jの2^i上の要素
  vc<int> depth;  // depth[i]:=rootに対する頂点iの深さ
  vll dist;
  vi nums;
  
  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);
    nums.resize(n);
    int num=0;
    function<void(int, int)> _dfs=[&](int v, int p){
      dp[0][v]=p;
      nums[v]=num;
      num++;
      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(vi& v){
    int k=v.size();
    auto f=[&](int a, int b){ return nums[a]<nums[b]; };
    sort(all(v), f);
    // rep(i,k) cout<<v[i]<<' ';
    ll sum=0;
    rep(i,k){
      if(i==k-1) sum+=getDist(v[i],v[0]);
      else sum+=getDist(v[i], v[i+1]);
    }
    sum/=2;
    return sum;
  }
};

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 k; cin>>k;
    vi v(k);
    rep(i,k) cin>>v[i];
    cout<<lca.getDist(v)<<endl;
  }
  
  // cout<<lca.getDist(0,1)<<endl;
  // cout<<lca.getDist(0,3)<<endl;
  
}
0