题目
Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges. For each edge (u, v) find the minimal possible weight of the spanning tree that contains the edge (u, v). The weight of the spanning tree is the sum of weights of all edges included in spanning tree.
输入
First line contains two integers n and m (1 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105) — the number of vertices and edges in graph. Each of the next m lines contains three integers ui, vi, wi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ wi ≤ 109) — the endpoints of the i-th edge and its weight.
输出
Print m lines. i-th line should contain the minimal possible weight of the spanning tree that contains i-th edge. The edges are numbered from 1 to m in order of their appearing in input.
解法
我们先对所有的边构建最小生成树,当我们需要求有u->v这个边的生成树的时候我们只需要在原来的树上通过Lca将边权最大的边删去然后添加上这条边即可
Code
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define endl '\n'
#define pb push_back
#define NO cout << "NO" << endl;
#define YES cout << "YES" << endl;
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<VI> VII;
ll MOD = 998244353;
ll powmod(ll a,ll b) {ll res=1;a%=MOD; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
mt19937 mrand(random_device{}());
const int N = 2e5 + 10;
int f[N];
int find(int x) {return f[x] == x ? f[x] : f[x] = find(f[x]);}
void solve()
{
int n, m; cin >> n >> m;
vector<vector<PII>> e(n + 1);
vector<array<int, 3>> a(m + 1);
rep (i, 1, m) {
int u, v, w; cin >> u >> v >> w;
a[i] = {u, v, w};
}
auto c = a;
rep (i, 1, n) f[i] = i;
sort(a.begin() + 1, a.end(), [](array<int, 3> a, array<int, 3> b){
return a[2] < b[2];
});
ll res = 0;
for (int i = 1; i <= m; i++) {
auto [u, v, w] = a[i];
int fa = find(u), fb = find(v);
if (fa != fb) {
e[u].pb({v, w});
e[v].pb({u, w});
// cout << u << ' ' << v << ' ' << w << endl;
f[fa] = fb;
res += w;
}
}
VI fa(n + 1), de(n + 1);
VII p(n + 1, VI(23));
auto s = p;
function<void(int, int, int)> dfs = [&] (int u, int Fa, int val) {
de[u] = de[Fa] + 1;
p[u][0] = Fa;
fa[u] = Fa;
s[u][0] = val;
for (auto [v, w] : e[u]) {
if (v == Fa) continue;
dfs(v, u, w);
}
};
dfs(1, 0, 0);
for (int j = 1; j < 22; j++)
for (int i = 1; i <= n; i++) {
p[i][j] = p[p[i][j - 1]][j-1];
s[i][j] = max(s[p[i][j - 1]][j-1], s[i][j - 1]);
}
function<ll(int, int)> Lca = [&] (int x, int y) -> ll {
if (de[x] < de[y]) swap(x, y);
ll res = 0;
for (int i = 21; i >= 0; i--) {
if (de[p[x][i]] >= de[y]) {
res = max(res, s[x][i]);
x = p[x][i];
}
}
if (x == y) return res;
for (int i = 21; i >= 0; i--) {
if (p[x][i] != p[y][i]) {
res = max({res, s[x][i], s[y][i]});
x = p[x][i];
y = p[y][i];
}
}
return max({res, s[x][0], s[y][0]});
};
VI ans(m + 1);
rep (i, 1, m) {
auto [u, v, w] = c[i];
ans[i] = res + w - Lca(u, v);
}
rep (i, 1, m) cout << ans[i] << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// int T;cin >> T;
// while ( T -- )
solve();
return 0;
}