1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Problem : P4180 [BJWC2010]严格次小生成树
// Contest : Luogu Online Judge
// URL : https://www.luogu.com.cn/problem/P4180
// Memory Limit : 500 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
#include "ybwhead/ios.h"
const int maxn = 1e5 + 100;
#define int long long
int f[maxn];
struct line
{
int x, y, z;
} a[maxn * 3 + 5];
int cmp(line a, line b)
{
return a.z < b.z;
}
int head[maxn];
struct edge
{
int v, w, nxt;
} e[maxn << 1];
int tot;
void __ADD(int u, int v, int w)
{
e[++tot].v = v;
e[tot].w = w;
e[tot].nxt = head[u];
head[u] = tot;
}
void add(int u, int v, int w)
{
// cerr << u << " " << v << " " << w << endl;
__ADD(u, v, w);
__ADD(v, u, w);
}
int fa[maxn][23], mx[maxn][23];
int dep[maxn];
void dfs(int u, int f)
{
fa[u][0] = f;
dep[u] = dep[f] + 1;
for (int i = 1; i <= 20; i++)
{
fa[u][i] = fa[fa[u][i - 1]][i - 1], mx[u][i] = max(mx[u][i - 1], mx[fa[u][i - 1]][i - 1]);
}
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].v;
if (v == f)
continue;
mx[v][0] = e[i].w;
dfs(v, u);
}
}
int q(int x, int y, int z)
{
if (dep[x] < dep[y])
swap(x, y);
int ans = 0;
for (int i = 20; i >= 0; i--)
{
if (dep[fa[x][i]] >= dep[y])
{
if (mx[x][i] != z)
ans = max(ans, mx[x][i]);
x = fa[x][i];
}
}
if (x == y)
return ans;
for (int i = 20; i >= 0; i--)
{
if (fa[x][i] != fa[y][i])
{
if (mx[x][i] != z)
ans = max(ans, mx[x][i]);
if (mx[y][i] != z)
ans = max(ans, mx[y][i]);
x = fa[x][i];
y = fa[y][i];
}
}
return max(ans, max(mx[x][0] != z ? mx[x][0] : 0, mx[y][0] != z ? mx[y][0] : 0));
// }
// // yout<<ans<<endl;
// return ans;
}
// int f[maxn];
int getf(int x)
{
// cerr << x << endl;
if (x == f[x])
return x;
return f[x] = getf(f[x]);
}
int merge(int x, int y)
{
int sx = getf(x), sy = getf(y);
if (sx == sy)
return 0;
f[sx] = sy;
return 1;
}
int n, m;
signed main()
{
yin >> n >> m;
for (int i = 1; i <= n; i++)
f[i] = i;
for (int i = 1; i <= m; i++)
{
yin >> a[i].x >> a[i].y >> a[i].z;
}
sort(a + 1, a + m + 1, cmp);
int ans = 0, sum = LLONG_MAX;
for (int i = 1; i <= m; i++)
{
if (merge(a[i].x, a[i].y))
{
add(a[i].x, a[i].y, a[i].z);
ans += a[i].z;
}
}
dfs(1, 0);
for (int i = 1; i <= m; i++)
{
if (fa[a[i].x][0] == a[i].y || fa[a[i].y][0] == a[i].x || a[i].x == a[i].y)
{
continue;
}
int xx;
if ((xx = (a[i].z - q(a[i].x, a[i].y, a[i].z))) != 0)
sum = min(xx, sum);
// yout << xx << endl;
}
yout << sum + ans << endl;
return 0;
}
|