求助,不开O2 100pts,开了O2 全部TLE 代码
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void readin(T &x) {
x = 0;
char ch = getchar();
T fh = 1;
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
x *= fh;
}
template <typename T>
void wt(T x) {
if (x > 9) wt(x / 10);
putchar(x % 10 + 48);
}
template <typename T>
void writeln(T x, char ch) {
wt(x);
putchar(ch);
}
const int N = 1e5 + 5;
struct info{
int to, nex, val;
}l[N << 1];
int n, m, siz[N], f[N], hd[N], g[N][20][2], anc[N][20], dep[N];
int G(int x) {
if (f[x] == x) return x;
return f[x] = G(f[x]);
}
int mer(int x, int y) {
x = G(x);
y = G(y);
if (siz[x] > siz[y]) swap(x, y);
f[x] = y;
siz[y] += siz[x];
}
void dfs(int p) {
for (int i = 1; i <= 19; i ++) {
anc[p][i] = anc[anc[p][i - 1]][i - 1];
g[p][i][0] = max(g[p][i - 1][0], g[anc[p][i - 1]][i - 1][0]);
if (g[p][i - 1][0] < g[anc[p][i - 1]][i - 1][0]) {
g[p][i][1] = max(g[p][i - 1][0], g[anc[p][i - 1]][i - 1][1]);
}
else if (g[p][i - 1][0] > g[anc[p][i - 1]][i - 1][0]) {
g[p][i][1] = max(g[p][i - 1][1], g[anc[p][i - 1]][i - 1][0]);
}
else g[p][i][1] = max(g[p][i - 1][1], g[anc[p][i - 1]][i - 1][1]);
}
int u;
for (int e = hd[p]; e; e = l[e].nex) {
u = l[e].to;
if (u != anc[p][0]) {
anc[u][0] = p;
g[u][0][0] = l[e].val;
g[u][0][1] = -1e9;
dep[u] = dep[p] + 1;
dfs(u);
}
}
}
pair <int, int> lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int w = 0, v = -1e9;
for (int i = 19; i >= 0; i --) {
if (dep[anc[x][i]] >= dep[y]) {
if (g[x][i][0] > w) {
v = max(w, g[x][i][1]);
w = g[x][i][0];
}
else if (g[x][i][0] == w) v = max(v, g[x][i][1]);
else if (g[x][i][0] > v) v = g[x][i][0];
x = anc[x][i];
}
}
if (x == y) return make_pair(w, v);
for (int i = 19; i >= 0; i --) {
if (anc[x][i] != anc[y][i]) {
if (g[x][i][0] > w) {
v = max(w, g[x][i][1]);
w = g[x][i][0];
}
else if (g[x][i][0] == w) v = max(v, g[x][i][1]);
else if (g[x][i][0] > v) v = g[x][i][0];
x = anc[x][i];
if (g[y][i][0] > w) {
v = max(w, g[y][i][1]);
w = g[y][i][0];
}
else if (g[y][i][0] == w) v = max(v, g[y][i][1]);
else if (g[y][i][0] > v) v = g[y][i][0];
y = anc[y][i];
}
}
if (g[x][0][0] > w) {
v = w;
w = g[x][0][0];
}
else if (g[x][0][0] < w && g[x][0][0] > v) {
v = g[x][0][0];
}
if (g[y][0][0] > w) {
v = w;
w = g[y][0][0];
}
else if (g[y][0][0] < w && g[y][0][0] > v) {
v = g[y][0][0];
}
return make_pair(w, v);
}
struct ques{
int x, y, z;
}q[N * 3];
bool comp(ques x, ques y) {
return x.z < y.z;
}
bool vis[N * 3];
int main() {
readin(n); readin(m);
for (int i = 1; i <= n; i ++) {
f[i] = i;
siz[i] = 1;
}
int u, v, w;
for (int i = 1; i <= m; i ++) {
readin(u); readin(v); readin(w);
q[i] = (ques) {u, v, w};
}
sort(q + 1, q + m + 1, comp);
int len = 0;
long long val = 0;
for (int i = 1; i <= m; i ++) {
if (G(q[i].x) != G(q[i].y)) {
mer(q[i].x, q[i].y);
len ++;
l[len] = (info) {q[i].y, hd[q[i].x], q[i].z};
hd[q[i].x] = len;
len ++;
l[len] = (info) {q[i].x, hd[q[i].y], q[i].z};
hd[q[i].y] = len;
val += q[i].z;
vis[i] = true;
}
}
dep[1] = 1;
dfs(1);
pair <int, int> di;
long long ans = 1e18;
for (int i = 1; i <= m; i ++) {
if (!vis[i]) {
di = lca(q[i].x, q[i].y);
w = q[i].z;
if (w > di.first) {
ans = min(ans, val - di.first + w);
}
else if (w > di.second) ans = min(ans, val - di.second + w);
}
}
writeln(ans, '\n');
return 0;
}