이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define in insert
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define deb(x) cout << #x << " = " << x << '\n';
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
// const int dx[] = {0, d1, 0, -1};
// const int dy[] = {1, 0, -1, 0};
const int inf = 1e9;
const ll INF = 1e18;
const int mod = 1e9 + 7;
const int N = 5e4 + 3;
int n, q, a[10], dis[N], pref[N], w[N];
bool arr[N];
vector <pair <pii, int>> g[N];
bitset <N> used, cur;
void dfs2(int v, int p) {
	for(auto to:g[v]) {
		if(to.ff.ff == p) continue;
		dis[to.ff.ff] = dis[v] + 1;
		pref[dis[to.ff.ff]] = pref[dis[v]] + to.ff.ss;
		dfs2(to.ff.ff, v);
	}
}
void dfs(int v, int p) {
	if(arr[v]) used |= cur;
	for(auto to:g[v]) {
		if(to.ff.ff == p) continue;
		cur[to.ss] = 1;
		dfs(to.ff.ff, v);
		cur[to.ss] = 0;
	}
}
void solve() {
	cin >> n;
	int sum = 0;
	bool sub2 = 1;
	for(int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v >> w[i];
		u++;
		v++;
		sum += w[i];
		g[u].pb({{v, w[i]}, i});
		g[v].pb({{u, w[i]}, i});
		if(sz(g[u]) > 2 || sz(g[v]) > 2) sub2 = 0;
	}
	cin >> q;
	if(n == 5 && q == 1) {
		cout << sum << '\n';
		return;
	}
	if(sub2) {
		int root;
		for(int i = 1; i <= n; i++) {
			if(sz(g[i]) == 1) {
				root = i;
				break;
			}
		}
		dis[root] = 1;
		dfs2(root, root);
		while(q--) {
			int l = inf, r = -inf;
			for(int i = 1; i <= 5; i++) {
				cin >> a[i];
				a[i]++;
				l = min(l, dis[a[i]]);
				r = max(r, dis[a[i]]);
			}
			cout << pref[r] - pref[l] << '\n';
		}
		return;
	}
	while(q--) {
		for(int i = 1; i <= 5; i++) {
			cin >> a[i];
			a[i]++;
			arr[a[i]] = 1;
		}
		for(int i = 1; i <= 5; i++) {
			dfs(a[i], -1);
		}
		int ans = 0;
		for(int i = 1; i < n; i++) {
			if(used[i]) ans += w[i];
		}
		cout << ans << '\n';
		used.reset();
		for(int i = 1; i <= 5; i++) {
			arr[a[i]] = 0;
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int TT = 1;
	// cin >> TT;
	while(TT--) {
		solve();
	}
	return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
roadsideadverts.cpp: In function 'void solve()':
roadsideadverts.cpp:74:7: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   74 |   dfs2(root, root);
      |   ~~~~^~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |