結果

問題 No.901 K-ary εxtrεεmε
ユーザー chocoruskchocorusk
提出日時 2019-10-04 22:02:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 381 ms / 3,000 ms
コード長 3,265 bytes
コンパイル時間 1,704 ms
コンパイル使用メモリ 129,136 KB
実行使用メモリ 22,340 KB
最終ジャッジ日時 2023-07-27 11:54:20
合計ジャッジ時間 10,720 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
22,340 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 239 ms
19,300 KB
testcase_08 AC 243 ms
19,300 KB
testcase_09 AC 247 ms
19,236 KB
testcase_10 AC 241 ms
19,140 KB
testcase_11 AC 238 ms
19,088 KB
testcase_12 AC 234 ms
19,228 KB
testcase_13 AC 241 ms
19,140 KB
testcase_14 AC 239 ms
19,284 KB
testcase_15 AC 236 ms
19,100 KB
testcase_16 AC 233 ms
19,024 KB
testcase_17 AC 331 ms
19,304 KB
testcase_18 AC 343 ms
19,108 KB
testcase_19 AC 317 ms
19,228 KB
testcase_20 AC 320 ms
19,248 KB
testcase_21 AC 314 ms
19,404 KB
testcase_22 AC 232 ms
19,432 KB
testcase_23 AC 230 ms
19,480 KB
testcase_24 AC 232 ms
19,404 KB
testcase_25 AC 229 ms
19,708 KB
testcase_26 AC 237 ms
19,548 KB
testcase_27 AC 380 ms
19,280 KB
testcase_28 AC 381 ms
19,272 KB
testcase_29 AC 371 ms
19,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, ll> P;
template< typename G=vector<vector<int>> >
struct HeavyLightDecomposition {
  G &g;
  vector< int > sz, in, out, head, rev, par;

  HeavyLightDecomposition(G &g) :
      g(g), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) {}

  void dfs_sz(int idx, int p) {
    par[idx] = p;
    sz[idx] = 1;
    if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back());
    for(auto &to : g[idx]) {
      if(to == p) continue;
      dfs_sz(to, idx);
      sz[idx] += sz[to];
      if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to);
    }
  }

  void dfs_hld(int idx, int par, int &times) {
    in[idx] = times++;
    rev[in[idx]] = idx;
    for(auto &to : g[idx]) {
      if(to == par) continue;
      head[to] = (g[idx][0] == to ? head[idx] : to);
      dfs_hld(to, idx, times);
    }
    out[idx] = times;
  }

  void build() {
    dfs_sz(0, -1);
    int t = 0;
    dfs_hld(0, -1, t);
  }

  int la(int v, int k) {
    while(1) {
      int u = head[v];
      if(in[v] - k >= in[u]) return rev[in[v] - k];
      k -= in[v] - in[u] + 1;
      v = par[u];
    }
  }

  int lca(int u, int v) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) return u;
    }
  }

  template< typename T, typename Q, typename F >
  T query(int u, int v, const T &ti, const Q &q, const F &f, bool edge = false) {
    T l = ti, r = ti;
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v), swap(l, r);
      if(head[u] == head[v]) break;
      l = f(q(in[head[v]], in[v] + 1), l);
    }
    return f(f(q(in[u] + edge, in[v] + 1), l), r);
  }

  template< typename Q >
  void add(int u, int v, const Q &q, bool edge = false) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) break;
      q(in[head[v]], in[v] + 1);
    }
    q(in[u] + edge, in[v] + 1);
  }
};
vector<vector<int>> G;
vector<vector<P>> g;
ll d[100010];
void dfs(int x, int p){
	for(auto q:g[x]){
		int y=q.first;
		ll w=q.second;
		if(y!=p){
			d[y]=d[x]+w;
			dfs(y, x);
		}
	}
}
int main()
{
	int n; cin>>n;
	g.resize(n); G.resize(n);
	for(int i=0; i<n-1; i++){
		int u, v;
		ll w;
		cin>>u>>v>>w;
		G[u].push_back(v);
		G[v].push_back(u);
		g[u].push_back({v, w});
		g[v].push_back({u, w});
	}
	HeavyLightDecomposition<> hld(G);
	hld.build();
	dfs(0, -1);
	int Q; cin>>Q;
	for(int i=0; i<Q; i++){
		int k; cin>>k;
		vector<int> v(k), ind(k);
		for(int j=0; j<k; j++){
			ind[j]=j;
			cin>>v[j];
		}
		sort(ind.begin(), ind.end(), [&](int p, int q){ return hld.in[v[p]]<hld.in[v[q]];});
		ll ans=0;
		for(int j=0; j<k; j++){
			int nx=j+1;
			if(nx==k) nx=0;
			int x=v[ind[j]], y=v[ind[nx]];
			int p=hld.lca(x, y);
			ans+=d[x]+d[y]-2*d[p];
		}
		ans/=2;
		cout<<ans<<endl;
	}
	return 0;
}
0