#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
#define int long long
const int N = 300010;
struct edge {
int y, w;
};
int n, s, t;
pair<int, int> p;
vector<edge> v[N];
int fa[N], dist[N];
queue<int> q;
bool vis[N];
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') { f = (ch == '-' ? -1 : f); ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
int find(int x) {
if (x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
signed main() {
n = read(), s = read(), t = read();
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i < n; i++) {
int x = read(), y = read(), z = read();
v[x].push_back((edge){y, z});
v[y].push_back((edge){x, z});
}
vis[s] = vis[t] = true;
dist[s] = dist[t] = 0;
q.push(s);
while(q.size()) {
int l = q.front(); q.pop();
for (int i = 0; i < v[l].size(); i++) {
if (vis[v[l][i].y]) continue;
q.push(v[l][i].y);
fa[find(v[l][i].y)] = find(s), vis[v[l][i].y] = true;
dist[v[l][i].y] = dist[l] + v[l][i].w;
}
}
q.push(t);
bool flag = false;
while (q.size()) {
int l = q.front(); q.pop();
for (int i = 0; i < v[l].size(); i++) {
if (find(v[l][i].y) != find(s)) {
if (vis[v[l][i].y]) continue;
q.push(v[l][i].y);
fa[find(v[l][i].y)] = find(t), vis[v[l][i].y] = true;
dist[v[l][i].y] = dist[l] + v[l][i].w;
}
else if (!flag) {
if (dist[v[l][i].y] > dist[l] + v[l][i].w) {
dist[v[l][i].y] = dist[l] + v[l][i].w;
fa[v[l][i].y] = find(t);
}
flag = true;
}
}
}
memset(dist, 0, sizeof(dist));
memset(vis, 0, sizeof(vis));
int sum = 0;
q.push(s);
vis[s] = true;
while (q.size()) {
int l = q.front(); q.pop();
int len = v[l].size();
for (int i = 0; i < len; i++) {
if (vis[v[l][i].y]) continue;
if (fa[find(v[l][i].y)] != find(s)) continue;
dist[v[l][i].y] = dist[l] + v[l][i].w;
sum += dist[v[l][i].y], vis[v[l][i].y] = true;
q.push(v[l][i].y);
}
}
//cout << sum << endl;
q.push(t), vis[t] = true;
while (q.size()) {
int l = q.front(); q.pop();
for (int i = 0; i < v[l].size(); i++) {
if (vis[v[l][i].y]) continue;
dist[v[l][i].y] = dist[l] + v[l][i].w;
sum += dist[v[l][i].y], vis[v[l][i].y] = true;
q.push(v[l][i].y);
}
}
cout << sum << endl;
cout << "IAKIOI";
return 0;
}
比赛的时候忘记交了,但是大样例过不去。
仅针对第一问