結果

問題 No.1094 木登り / Climbing tree
ユーザー FF256grhyFF256grhy
提出日時 2020-06-25 18:28:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,382 ms / 2,000 ms
コード長 4,267 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 210,992 KB
実行使用メモリ 94,724 KB
最終ジャッジ日時 2024-04-25 18:28:06
合計ジャッジ時間 33,905 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1,318 ms
86,296 KB
testcase_02 AC 431 ms
94,724 KB
testcase_03 AC 242 ms
7,040 KB
testcase_04 AC 331 ms
35,328 KB
testcase_05 AC 603 ms
75,392 KB
testcase_06 AC 620 ms
26,496 KB
testcase_07 AC 1,353 ms
86,448 KB
testcase_08 AC 1,354 ms
86,384 KB
testcase_09 AC 1,361 ms
86,344 KB
testcase_10 AC 1,336 ms
86,304 KB
testcase_11 AC 1,327 ms
86,256 KB
testcase_12 AC 1,314 ms
86,288 KB
testcase_13 AC 1,382 ms
86,452 KB
testcase_14 AC 1,324 ms
86,448 KB
testcase_15 AC 712 ms
21,504 KB
testcase_16 AC 1,244 ms
72,868 KB
testcase_17 AC 940 ms
40,320 KB
testcase_18 AC 817 ms
30,976 KB
testcase_19 AC 1,111 ms
55,372 KB
testcase_20 AC 1,348 ms
86,412 KB
testcase_21 AC 956 ms
43,008 KB
testcase_22 AC 1,346 ms
86,588 KB
testcase_23 AC 1,316 ms
86,396 KB
testcase_24 AC 1,333 ms
86,504 KB
testcase_25 AC 1,370 ms
86,304 KB
testcase_26 AC 1,348 ms
86,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define dec(i, n)  decID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
#define IN(T, ...) T __VA_ARGS__; IN_(__VA_ARGS__);
void IN_() { };
template<typename T, typename ... U> void IN_(T &  a, U &  ... b) { cin >> a; IN_(b ...); };
template<typename T                > void OUT(T && a            ) { cout << a << endl; }
template<typename T, typename ... U> void OUT(T && a, U && ... b) { cout << a << " "; OUT(b ...); }

// ---- ----

#define bit(b, i) (((b) >> (i)) & 1)
template<typename T> T MV(T v) { return v; }
template<typename T, typename ... U> auto MV(T v, int a, U ... b) { return vector<decltype(MV(v, b ...))>(a, MV(v, b ...)); }

template<typename T> class Forest {
private:
	using Graph = vector<vector<pair<int, T>>>;
	using Func = function<T(T, T)>;
	int n;
	Graph g;
	Func f;
	T id;
	bool ok = false;
	int B;
	vector<int> d;
	vector<vector<int>> P;
	vector<vector<T>> U, D;
	vector<bool> vis;
	void dfs(int v, int p, T w) {
		if(vis[v]) { return; }
		vis[v] = true;
		if(v != p) { d[v] = d[p] + 1; }
		P[v][0] = p;
		U[v][0] = w;
		D[v][0] = w;
		RF(e, g[v]) { dfs(e.FI, v, e.SE); }
	}
	T fold_U(int a, int b) {
		assert(d[a] >= d[b]);
		T s = id;
		dec(k, B) {
			if(bit(d[a] - d[b], k) == 0) { continue; }
			s = f(s, U[a][k]);
			a = P[a][k];
		}
		assert(a == b);
		return s;
	}
	T fold_D(int a, int b) {
		assert(d[b] >= d[a]);
		T s = id;
		dec(k, B) {
			if(bit(d[b] - d[a], k) == 0) { continue; }
			s = f(D[b][k], s);
			b = P[b][k];
		}
		assert(b == a);
		return s;
	}
public:
	Forest(Graph const & g, Func f, T id) : n(SI(g)), g(g), f(f), id(id) { }
	Forest(int n, Func f, T id) : n(n), g(n), f(f), id(id) { };
	void add(int a, int b, T w) {
		ok = false;
		assert(inID(a, 0, n));
		assert(inID(b, 0, n));
		assert(a != b);
		g[a].EB(b, w);
		g[b].EB(a, w);
	}
	void init(vector<int> const & r = { }) {
		ok = true;
		B = 1;
		until((1 << (B - 1)) >= n) { B++; }
		d = vector<int>(n);
		P = MV<int>(0, n, B);
		U = MV<T>(id, n, B);
		D = MV<T>(id, n, B);
		vis = vector<bool>(n, false);
		RF(e, r) { assert(inID(e, 0, n)); dfs(e, e, id); }
		inc(i, n) { dfs(i, i, id); }
		inc(k, B - 1) {
		inc(v, n) {
			int p = P[v][k];
			P[v][k + 1] = P[p][k];
			U[v][k + 1] = f(U[v][k], U[p][k]);
			D[v][k + 1] = f(D[p][k], D[v][k]);
		}
		}
	}
	int lca(int a, int b) {
		assert(ok);
		if(P[a][B - 1] != P[b][B - 1]) { return -1; }
		if_not(d[a] >= d[b]) { swap(a, b); }
		inc(k, B) { if(bit(d[a] - d[b], k)) { a = P[a][k]; } }
		if(a == b) { return a; }
		dec(k, B) {
			if(P[a][k] != P[b][k]) {
				a = P[a][k];
				b = P[b][k];
			}
		}
		a = P[a][0];
		b = P[b][0];
		assert(a == b);
		return a;
	}
	T fold(int a, int b) {
		int c = lca(a, b);
		if(c == -1) { return id; }
		return f(fold_U(a, c), fold_D(c, b));
	}
	Graph const & get_g() { return g; }
};
#define OP(s) [&](auto A, auto B) { return s; }

// ----

int main() {
	IN(int, n);
	Forest<int> t(n, OP(A + B), 0);
	inc(i, n - 1) {
		IN(int, a, b, c);
		a--; b--;
		t.add(a, b, c);
	}
	t.init();
	
	IN(int, q);
	inc(qq, q) {
		IN(int, a, b);
		a--; b--;
		OUT(t.fold(a, b));
	}
}
0