Language: C++


#include<iostream>
#include<algorithm>
#include<functional>
#include <string>
#include<cstdio>
#include<math.h>
#include<stack>
#include<queue>
#include<cstring>
#include<vector>
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define EREP(i,n) for(int i=(n-1);i>=0;--i)
#define D(n,retu) REP(i,n){cin>>retu[i];}
#define mod 1000000007
#define MIN -93193111451418101
#define INF 931931114518101
using namespace std;
typedef long long int ll;
template<typename T>
void fill_all(T& arr, const T& v) {
	arr = v;
}
template<typename T, typename ARR>
void fill_all(ARR& arr, const T& v) {
	for (auto& i : arr) { fill_all(i, v); }
}
ll combination(ll n) {
	ll nck[100][100];
	nck[0][0] = 1;
	for (int i = 1; i < 1 + n; i++) {
		nck[i][0] = nck[i][i] = 1;
		for (int j = 1; j < i; j++) {
			nck[i][j] = nck[i - 1][j - 1] + nck[i - 1][j];
		}
	}
	return 0;
}
ll GCD(ll u, ll v) {
	while (v != 0) {
		ll r = u % v;
		u = v;
		v = r;
	}
	return u;
}

//------------------変数-----------------------//
vector<vector<ll>>G(100001), cost(100001);
//-------------------関数----------------------//

bool vis[100001] = {}; ll goukei[100001] = {};
ll DFS(ll v, ll atai) {
	goukei[v] = atai;
	vis[v] = true;
	REP(i, G[v].size())
	{
		if (!vis[G[v][i]]) {
			DFS(G[v][i], atai + cost[v][i]);
		}
	}
	return 0;
}

int main() {
	ll n; cin >> n;
	fill_all(goukei, INF);
	REP(i, n-1) {
		ll a, b, c; cin >> a >> b >> c; a--; b--;
		G[a].push_back(b); G[b].push_back(a);
		cost[a].push_back(c); cost[b].push_back(c);
	}
	ll q, k; cin >> q >> k; k--; goukei[k] = 0;
	DFS(k, 0); ll cnt = 0;
	REP(i, q) {
		ll x, y; cin >> x >> y; x--, y--;
		cout << goukei[x] + goukei[y] << endl;
	}
	return 0;
}

Compiler output:
a.cc: In instantiation of 'void fill_all(ARR&, const T&) [with T = long int; ARR = long long int]':
a.cc:27:32:   required from 'void fill_all(ARR&, const T&) [with T = long int; ARR = long long int [100001]]'
   27 |         for (auto& i : arr) { fill_all(i, v); }
      |                               ~~~~~~~~^~~~~~
a.cc:68:10:   required from here
   68 |         fill_all(goukei, INF);
      |         ~~~~~~~~^~~~~~~~~~~~~
a.cc:27:9: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   27 |         for (auto& i : arr) { fill_all(i, v); }
      |         ^~~
      |         std::begin
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:27:9: error: 'end' was not declared in this scope; did you mean 'std::end'?
   27 |         for (auto& i : arr) { fill_all(i, v); }
      |         ^~~
      |         std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~