Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4180 [BJWC2010]严格次小生成树

P4180 [BJWC2010]严格次小生成树

先求最小生成树。

对于每一条不在最小生成树的边,如果添加了这条边,那么这两点在最小生成树的路径和这条边形成了一个环,将环上严格大于这条边的边权的一条边换成这条边,则可能成为答案。所以需要维护树上最大值和次大值。

这里用树链剖分维护,复杂度多一个 $\log$ ,可以通过。

查看代码
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const LL inf = 1e16;
const int N = 1e5 + 10, M = 3e5 + 10;
int n, m, p[N];
vector<int> v;
int fa(int x)
{
return x == p[x] ? x : p[x] = fa(p[x]);
}
struct Edge
{
int u, v, w;
void input()
{
scanf("%d%d%d", &u, &v, &w);
}
bool operator<(const Edge &_) const
{
return w < _.w;
}
} e[M];
namespace TreeSplit
{
const int M = 6e5 + 10;
int w[N], p[N], d[N], sz[N];
int stmp, dfn[N], rnk[N], son[N], top[N];
int idx, hd[N], nxt[M], edg[M], wt[M];
struct Node
{
int l, r, mx1, mx2;
} tr[N << 2];
void pushup(Node &x, Node l, Node r)
{
vector<int> mxs;
if (~l.mx1)
mxs.push_back(l.mx1);
if (~r.mx1)
mxs.push_back(r.mx1);
if (~l.mx2)
mxs.push_back(l.mx2);
if (~r.mx2)
mxs.push_back(r.mx2);
sort(mxs.begin(), mxs.end());
mxs.erase(unique(mxs.begin(), mxs.end()), mxs.end());
if (mxs.size())
x.mx1 = mxs.back();
else
x.mx1 = -1;
if (mxs.size() > 1)
x.mx2 = mxs[mxs.size() - 2];
else
x.mx2 = -1;
}
void build(int x, int l, int r)
{
tr[x].l = l, tr[x].r = r;
if (l == r)
{
tr[x].mx1 = w[rnk[l]];
tr[x].mx2 = -1;
return;
}
int mid = l + r >> 1;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
pushup(tr[x], tr[x << 1], tr[x << 1 | 1]);
}
Node query(int x, int l, int r)
{
if (tr[x].l >= l && tr[x].r <= r)
return tr[x];
int mid = tr[x].l + tr[x].r >> 1;
if (r <= mid)
return query(x << 1, l, r);
else if (l > mid)
return query(x << 1 | 1, l, r);
Node res;
pushup(res, query(x << 1, l, r), query(x << 1 | 1, l, r));
return res;
}
Node QueryPath(int x, int y)
{
Node res;
res.mx1 = res.mx2 = -1;
while (top[x] != top[y])
{
if (d[top[x]] < d[top[y]])
swap(x, y);
pushup(res, res, query(1, dfn[top[x]], dfn[x]));
x = p[top[x]];
}
if (x == y)
return res;
if (d[x] > d[y])
swap(x, y);
pushup(res, res, query(1, dfn[x] + 1, dfn[y]));
return res;
}
void dfs1(int x)
{
sz[x] = 1;
son[x] = -1;
for (int i = hd[x]; ~i; i = nxt[i])
if (!d[edg[i]])
{
d[edg[i]] = d[x] + 1;
p[edg[i]] = x;
w[edg[i]] = wt[i];
dfs1(edg[i]);
sz[x] += sz[edg[i]];
if (son[x] == -1 || sz[edg[i]] > sz[son[x]])
son[x] = edg[i];
}
}
void dfs2(int x, int t)
{
top[x] = t;
dfn[x] = ++stmp;
rnk[stmp] = x;
if (~son[x])
dfs2(son[x], t);
for (int i = hd[x]; ~i; i = nxt[i])
if (edg[i] != son[x] && edg[i] != p[x])
dfs2(edg[i], edg[i]);
}
void add(int a, int b, int c)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
}
void init()
{
for (int i = 1; i <= n; i++)
hd[i] = -1;
}
void main()
{
d[1] = 1;
dfs1(1);
dfs2(1, 1);
build(1, 2, n);
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
p[i] = i;
for (int i = 1; i <= m; i++)
e[i].input();
sort(e + 1, e + m + 1);
LL tot = 0;
TreeSplit::init();
for (int i = 1; i <= m; i++)
if (fa(e[i].u) != fa(e[i].v))
{
tot += e[i].w;
TreeSplit::add(e[i].u, e[i].v, e[i].w);
TreeSplit::add(e[i].v, e[i].u, e[i].w);
p[p[e[i].u]] = p[e[i].v];
}
else
v.push_back(i);
TreeSplit::main();
LL res = inf;
for (int i : v)
{
TreeSplit::Node t = TreeSplit::QueryPath(e[i].u, e[i].v);
if (~t.mx1 && t.mx1 != e[i].w)
res = min(res, tot - t.mx1 + e[i].w);
else if (~t.mx2)
res = min(res, tot - t.mx2 + e[i].w);
}
printf("%lld", res);
return 0;
}