Language: C++


//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <limits>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18

const int limit = 100010;
using edge = struct { int to; LL cost; };
vector<edge> tree[limit];
LL depth[limit];

void dfs(int pos, int par, int d) {
	depth[pos] = d;

	for (auto e : tree[pos]) {
		if (e.to == par)continue;

		dfs(e.to, pos, d + e.cost);
	}
}

int main() {

	int n; cin >> n;

	for (int i = 0; i < n-1; i++) {
		int a, b, c; cin >> a >> b >> c;

		a--; b--;

		tree[a].emplace_back(b, c);
		tree[b].emplace_back(a, c);
	}

	int q, k; cin >> q >> k;
	k--;

	dfs(k, -1, 0);

	for (int i = 0; i < q; i++) {
		int x, y; cin >> x >> y;
		x--; y--;

		cout << depth[x] + depth[y] << endl;
	}



	return 0;
}

Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/vector:63,
                 from a.cc:4:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = <unnamed struct>; _Args = {int&, int&}; _Tp = <unnamed struct>]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_Tp1> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = <unnamed struct>; _Args = {int&, int&}; _Tp = <unnamed struct>; allocator_type = std::allocator<<unnamed struct> >]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:117:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int&, int&}; _Tp = <unnamed struct>; _Alloc = std::allocator<<unnamed struct> >; reference = <unnamed struct>&]'
  117 |             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118 |                                      std::forward<_Args>(__args)...);
      |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:95:23:   required from here
   95 |                 tree[a].emplace_back(b, c);
      |                 ~~~~~~~~~~~~~~~~~~~~^~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: new initializer expression list treated as compound expression [-fpermissive]
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to '<unnamed struct>::._anon_105(int&)'
a.cc:72:21: note: candidate: '<unnamed struct>::<constructor>()'
   72 | using edge = struct { int to; LL cost; };
      |                     ^
a.cc:72:21: note:   candidate expects 0 arguments, 1 provided
a.cc:72:21: note: candidate: 'constexpr<unnamed struct>::<constructor>(const<unnamed struct>&)'
a.cc:72:21: note:   no known conversion for argument 1 from 'int' to 'const<unnamed struct>&'
a.cc:72:21: note: candidate: 'constexpr<unnamed struct>::<constructor>(<unnamed struct>&&)'
a.cc:72:21: note:   no known conversion for argument 1 from 'int' to '<unnamed struct>&&'