結果

問題 No.901 K-ary εxtrεεmε
ユーザー 👑 jupirojupiro
提出日時 2020-01-22 12:11:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 3,000 ms
コード長 3,142 bytes
コンパイル時間 2,017 ms
コンパイル使用メモリ 137,904 KB
実行使用メモリ 28,568 KB
最終ジャッジ日時 2023-09-22 04:33:53
合計ジャッジ時間 9,564 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
28,568 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 158 ms
25,560 KB
testcase_08 AC 156 ms
25,492 KB
testcase_09 AC 161 ms
25,424 KB
testcase_10 AC 158 ms
25,604 KB
testcase_11 AC 157 ms
25,540 KB
testcase_12 AC 161 ms
25,400 KB
testcase_13 AC 166 ms
25,396 KB
testcase_14 AC 161 ms
25,488 KB
testcase_15 AC 160 ms
25,756 KB
testcase_16 AC 164 ms
25,492 KB
testcase_17 AC 167 ms
25,684 KB
testcase_18 AC 166 ms
25,428 KB
testcase_19 AC 169 ms
25,428 KB
testcase_20 AC 166 ms
25,692 KB
testcase_21 AC 166 ms
25,428 KB
testcase_22 AC 152 ms
25,472 KB
testcase_23 AC 168 ms
25,484 KB
testcase_24 AC 158 ms
25,440 KB
testcase_25 AC 146 ms
25,492 KB
testcase_26 AC 157 ms
25,436 KB
testcase_27 AC 157 ms
25,420 KB
testcase_28 AC 150 ms
25,444 KB
testcase_29 AC 149 ms
25,676 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘ll clz(ll)’ 内:
main.cpp:50:1: 警告: 制御が非 void 関数の終りに到達しました [-Wreturn-type]
   50 | }
      | ^

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

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; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

ll N;
vector<vector<ll>> ug;
vector<vector<pair<ll, ll>>> wg;
vector<ll> sum;
vector<ll> ord;
ll now;
ll clz(ll x) {
	for (ll i = 31; i >= 0; i--) {
		if ((x >> i) & 1) {
			return 31 - i;
		}
	}
}

template< typename G >
struct DoublingLowestCommonAncestor {
	const int LOG;
	vector< int > dep;
	const G& g;
	vector< vector< int > > table;

	DoublingLowestCommonAncestor(const G& g) : g(g), dep(g.size()), LOG(32 - clz(g.size())) {
		table.assign(LOG, vector< int >(g.size(), -1));
	}

	void dfs(int idx, int par, int d) {
		table[0][idx] = par;
		dep[idx] = d;
		for (auto& to : g[idx]) {
			if (to != par) dfs(to, idx, d + 1);
		}
	}

	void build() {
		dfs(0, -1, 0);
		for (int k = 0; k + 1 < LOG; k++) {
			for (int i = 0; i < table[k].size(); i++) {
				if (table[k][i] == -1) table[k + 1][i] = -1;
				else table[k + 1][i] = table[k][table[k][i]];
			}
		}
	}

	int query(int u, int v) {
		if (dep[u] > dep[v]) swap(u, v);
		for (int i = LOG - 1; i >= 0; i--) {
			if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
		}
		if (u == v) return u;
		for (int i = LOG - 1; i >= 0; i--) {
			if (table[i][u] != table[i][v]) {
				u = table[i][u];
				v = table[i][v];
			}
		}
		return table[0][u];
	}
};

void dfs(ll cur, ll pre) {
	ord[cur] = now;
	now++;
	for (auto next : wg[cur]) {
		if (next.first == pre) continue;
		sum[next.first] = sum[cur] + next.second;
		dfs(next.first, cur);
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	scanf("%lld", &N);
	ug.resize(N), wg.resize(N);
	ord = vector<ll>(N);
	for (ll i = 0; i < N - 1; i++) {
		ll x, y, w; scanf("%lld %lld %lld", &x, &y, &w);
		ug[x].push_back(y);
		ug[y].push_back(x);
		wg[x].emplace_back(y, w);
		wg[y].emplace_back(x, w);
	}
	sum = vector<ll>(N);
	dfs(0, -1);
	DoublingLowestCommonAncestor<vector<vector<ll>>> lca(ug);
	lca.build();
	ll Q; scanf("%lld", &Q);
	while (Q--) {
		ll k; scanf("%lld", &k);
		vector<pair<ll, ll>> vp(k);
		for (ll i = 0; i < k; i++) {
			scanf("%lld", &vp[i].second);
			vp[i].first = ord[vp[i].second];
		}
		sort(vp.begin(), vp.end());
		ll res = 0;
		for (ll i = 0; i < k; i++) {
			ll p = lca.query(vp[i].second, vp[(i + 1) % k].second);
			res += sum[vp[i].second] + sum[vp[(i + 1) % k].second] - sum[p] * 2;
		}
		res /= 2;
		printf("%lld\n", res);
	}
	return 0;
}
0