This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e5 + 9;
vector<pair<int, int>> tree[inf];
map<pair<int, int>, int> m;
int dist[inf];
int par[inf];
bool vis[inf];
void dfss(int here, int len) {
	dist[here] = len;
	for (auto i : tree[here]) {
		int there = i.first;
		int nlen = len + i.second;
		if (dist[there] == -1) dfss(there, nlen);
	}
}
void dfsm(int here) {
	vis[here] = true;
	for (auto i : tree[here]) {
		if (vis[i.first]) continue;
		dfsm(i.first);
		par[i.first] = here;
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, x, y; cin >> n >> x >> y;
	for (int i = 0; i < n - 1; i++) {
		int a, b, c; cin >> a >> b >> c;
		tree[a].emplace_back(b, c);
		tree[b].emplace_back(a, c);
		m[make_pair(a, b)] = c;
		m[make_pair(b, a)] = c;
	}
	fill(&dist[1], &dist[n + 1], -1);
	dfss(x, 0);
	dfsm(x);
	int here = y;
	int mpath = 0;
	while (here != x) {
		mpath = max(mpath, m[make_pair(here, par[here])]);
		here = par[here];
	}
	cout << dist[y] - mpath << '\n';
	
}
| # | 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... |