結果

問題 No.901 K-ary εxtrεεmε
ユーザー kwm_tkwm_t
提出日時 2023-05-06 20:23:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 154 ms / 3,000 ms
コード長 4,283 bytes
コンパイル時間 3,553 ms
コンパイル使用メモリ 216,932 KB
実行使用メモリ 24,392 KB
最終ジャッジ日時 2023-08-15 17:46:12
合計ジャッジ時間 11,185 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
24,392 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 154 ms
21,708 KB
testcase_08 AC 150 ms
21,760 KB
testcase_09 AC 151 ms
21,712 KB
testcase_10 AC 154 ms
21,824 KB
testcase_11 AC 152 ms
21,732 KB
testcase_12 AC 153 ms
21,704 KB
testcase_13 AC 149 ms
21,756 KB
testcase_14 AC 149 ms
21,708 KB
testcase_15 AC 151 ms
21,708 KB
testcase_16 AC 151 ms
21,824 KB
testcase_17 AC 149 ms
21,820 KB
testcase_18 AC 150 ms
21,728 KB
testcase_19 AC 142 ms
21,812 KB
testcase_20 AC 146 ms
21,820 KB
testcase_21 AC 145 ms
21,816 KB
testcase_22 AC 140 ms
22,072 KB
testcase_23 AC 143 ms
21,972 KB
testcase_24 AC 142 ms
22,020 KB
testcase_25 AC 142 ms
21,968 KB
testcase_26 AC 142 ms
22,064 KB
testcase_27 AC 115 ms
21,772 KB
testcase_28 AC 111 ms
21,772 KB
testcase_29 AC 119 ms
21,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
class Tree {
public:
	Tree(int n, int root) : n(n), root(root) {
		edge.resize(n);
		for (int i = 0; i < MAXLOGV; i++) parent[i].resize(n);
		depth.resize(n);
		dist_.resize(n);
		idx.resize(n);
		cnt = 0;
	}
	// uとvをつなぐ
	// lcaを求めることが主目的なので無向グラフとしている
	void unite(int u, int v, long long w) {
		edge[u].push_back({ v, w });
		edge[v].push_back({ u, w });
		//edge[u].emplace_back(v, w);
		//edge[v].emplace_back(u, w);
	}
	// initする
	// コンストラクタだけじゃなくてこれも呼ばないとlcaが求められないぞ
	void init() {
		dfs(root, -1, 0);
		for (int k = 0; k + 1 < MAXLOGV; k++) {
			for (int v = 0; v < n; v++) {
				if (parent[k][v] < 0) parent[k + 1][v] = -1;
				else parent[k + 1][v] = parent[k][parent[k][v]];
			}
		}
	}
	// uとvのlcaを求める
	int lca(int u, int v) const {
		if (depth[u] > depth[v]) swap(u, v);
		for (int k = 0; k < MAXLOGV; k++) {
			if ((depth[v] - depth[u]) >> k & 1) {
				v = parent[k][v];
			}
		}
		if (u == v) return u;
		for (int k = MAXLOGV - 1; k >= 0; k--) {
			if (parent[k][u] != parent[k][v]) {
				u = parent[k][u];
				v = parent[k][v];
			}
		}
		return parent[0][u];
	}
	// uのn個親を求める
	int pare(int v, int n) {
		if (depth[v] < n)return -1;
		n = min(n, depth[v]);
		int idx = MAXLOGV;
		while (n) {
			for (int i = idx - 1; i >= 0; --i) {
				if (n < (1 << i))continue;
				if (-1 == parent[i][v])continue;
				n -= (1 << i);
				v = parent[i][v];
				idx = i;
				break;
			}
		}
		return v;
	}
	// uからvに向かってd進んだ頂点を返す
	int JumpOnTree(int u, int v, int d) {
		if (0 == d)return u;
		int distuv = dist(u, v);
		if (distuv < d)return -1;
		int l = lca(u, v);
		if (l == u)return pare(v, distuv - d);
		if (l == v)return pare(u, d);
		int distlu = dist(l, u);
		if (distlu >= d)return pare(u, d);
		return pare(v, distuv - d);
	}
	// uとvの距離を求める
	// edgeを定義しないといけない時はこれじゃダメ
	long long dist(int u, int v) const {
		int p = lca(u, v);
		return dist_[u] + dist_[v] - dist_[p] * 2;
		//return (depth[u] - depth[p]) + (depth[v] - depth[p]);
	}
	//頂点wが頂点u,vのパス上に存在するか
	bool on_path(int u, int v, int w) {
		return (dist(u, w) + dist(v, w) == dist(u, v));
	}
	int dfs(int v, int p, int d) {
		idx[v] = cnt;
		cnt++;
		int ret = 1;
		parent[0][v] = p;
		depth[v] = d;
		for (auto e : edge[v]) {
			if (e.first == p) continue;
			dist_[e.first] = dist_[v] + e.second;
			auto get = dfs(e.first, v, d + 1);
			ret += get;
		}
		return ret;
	}
	static const int MAXLOGV = 25;
	// グラフの隣接リスト表現
	vector<vector<pair<int, long long>>>edge;
	// 頂点の数
	int n;
	// 根ノードの番号
	int root;
	// 親ノード
	vector<int> parent[MAXLOGV];
	// 根からの深さ
	vector<int> depth;
	vector<long long>dist_;
	vector<int>idx;
	int cnt;
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	Tree tree(n, 0);
	for (int i = 0; i < n - 1; i++) {
		int x, y, w;
		cin >> x >> y >> w;
		tree.unite(x, y, w);
	}
	tree.init();
	int q; cin >> q;
	while (q--) {
		int k; cin >> k;
		vector<P>v(k);
		rep(i, k) {
			int x; cin >> x;
			v[i] = { tree.idx[x],x };
		}
		sort(all(v));
		long long ans = 0;
		rep(i, k) {
			int v0 = v[i].second;
			int v1 = v[(i + 1) % k].second;
			ans += tree.dist(v0, v1);
		}
		cout << ans / 2 << endl;
	}
	return 0;
}
0