結果
問題 | No.901 K-ary εxtrεεmε |
ユーザー | pione |
提出日時 | 2021-01-29 14:55:35 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 237 ms / 3,000 ms |
コード長 | 3,795 bytes |
コンパイル時間 | 2,001 ms |
コンパイル使用メモリ | 187,276 KB |
実行使用メモリ | 24,308 KB |
最終ジャッジ日時 | 2024-06-27 02:15:28 |
合計ジャッジ時間 | 8,614 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 68 ms
24,308 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 3 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 3 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 3 ms
6,944 KB |
testcase_07 | AC | 125 ms
19,364 KB |
testcase_08 | AC | 131 ms
19,228 KB |
testcase_09 | AC | 127 ms
19,232 KB |
testcase_10 | AC | 128 ms
19,232 KB |
testcase_11 | AC | 123 ms
19,232 KB |
testcase_12 | AC | 116 ms
19,232 KB |
testcase_13 | AC | 110 ms
19,232 KB |
testcase_14 | AC | 114 ms
19,232 KB |
testcase_15 | AC | 110 ms
19,368 KB |
testcase_16 | AC | 111 ms
19,232 KB |
testcase_17 | AC | 187 ms
19,236 KB |
testcase_18 | AC | 180 ms
19,232 KB |
testcase_19 | AC | 182 ms
19,356 KB |
testcase_20 | AC | 196 ms
19,280 KB |
testcase_21 | AC | 190 ms
19,236 KB |
testcase_22 | AC | 105 ms
19,236 KB |
testcase_23 | AC | 104 ms
19,236 KB |
testcase_24 | AC | 105 ms
19,236 KB |
testcase_25 | AC | 117 ms
19,232 KB |
testcase_26 | AC | 106 ms
19,364 KB |
testcase_27 | AC | 228 ms
19,240 KB |
testcase_28 | AC | 237 ms
19,236 KB |
testcase_29 | AC | 227 ms
19,240 KB |
ソースコード
#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; }