Part1. 非严格次小生成树
引理
图的次小生成树是强制在最小生成树中添加一个不在最小生成树上的边时这个图最小生成树的边权之和的最小值所在的那个生成树。
导出
- 一定有且仅有一条边在次小生成树里而不在最小生成素里且有一条边在最小生成树里而不在次小生成树里。
- 导出1中说明的在最小生成树里而不在次小生成树里的边一定是导出1中在次小生成树而不是最小生成树的边的两端之间的边的边权最大的边。
证明
首先,一定存在至少一条边在次小生成树里而不在最小生成树里(否则,最小生成树就和次小生成树一样了)。
因此,我们可以枚举这条不在次小生成树里而不在最小生成树里的边,强制包含这条边然后求强制包含这条边的最小生成树,这样的边权的最小值即为次小生成树。(这样就满足了至少一条边的条件了,也就使得新生成树一定和原生成树不一样,因此直接求最小生成树即可,无需考虑更多的边)
导出1的证明:
强制加入不在最小生成树内的边后,一条边的被选中的条件就更加苛刻了(因为有两个原本后来才被连上的点一开始就被连上了),因此除了这条边外原本就不在最小生成树内的边一定仍然不在新的最小生成树里。
这样加入后一共有条边,而一个生成树只有条边,因此一定要移出一条边。
导出2的证明:
我们的目标是次小,也就是新的边的权值和被删去的边的权值之差最小,等价于使得删去的边的权值最大,此时删去的边恰好为新增的边的两个点之间中边权最大的边。
实现
基于引理,我们可以枚举每一个不在最小生成树上的边,算出最小生成树,再求最小值即可。
复杂度:
优化1
基于导出1,针对每一个边,我们可以去枚举找到边权最大的在这个边两点之间的边,并删去。
复杂度:
CODE:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 1000 + 10, maxm = 1e6 + 10, lg = 10, inf = 1e9 + 10;
int dep[maxn], fa[maxn][lg + 1], delta, bfa[maxn], n, m, ans, inmst[maxm], t,
wi[maxn];
long long mstval;
vector<pair<int, int>> mst[maxn];
struct edge {
int u, v, w;
bool operator<(const edge other) const { return w < other.w; }
};
vector<edge> edges;
int bget(int x) { return bfa[x] == x ? x : bfa[x] = bget(bfa[x]); }
void bmerge(int x, int y) { bfa[bget(x)] = bget(y); }
void dfs(int cur, int pa, int w) {
dep[cur] = dep[pa] + 1;
fa[cur][0] = pa;
wi[cur] = w;
for (int i = 1; i <= lg; i++)
fa[cur][i] = fa[fa[cur][i - 1]][i - 1];
for (pair<int, int> x : mst[cur])
if (x.first != pa)
dfs(x.first, cur, x.second);
}
int lca(int x, int y) {
if (dep[x] < dep[y])
swap(x, y);
for (int i = lg; i >= 0; i--)
if ((dep[x] - dep[y]) & (1 << i))
x = fa[x][i];
if (x == y)
return x;
for (int i = lg; i >= 0; i--)
if (fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
void kruskal() {
for (int i = 1; i <= n; i++)
bfa[i] = i;
for (int i = 0; i < m; i++) {
edge e = edges[i];
if (bget(e.u) != bget(e.v))
bmerge(e.u, e.v), mst[e.u].push_back({e.v, e.w}),
mst[e.v].push_back({e.u, e.w}), mstval += e.w, inmst[i] = 1;
}
}
int getmx(int a, int b) {
int res = 0;
while (dep[a] - dep[b])
res = max(res, wi[a]), a = fa[a][0];
return res;
}
int main() {
#ifdef SUMBIT_UVA
cin >> t;
#else
t = 1;
#endif
while (t--) {
int u, v, w;
cin >> n >> m;
for (int i = 0; i < m; i++)
cin >> u >> v >> w, edges.push_back({u, v, w});
sort(edges.begin(), edges.end());
kruskal();
dfs(1, 1, 0);
delta = inf;
for (int i = 0; i < m; i++)
if (!inmst[i]) {
int u = edges[i].u, v = edges[i].v, w = edges[i].w, k = lca(u, v);
delta = min(delta, w - max(getmx(u, k), getmx(v, k)));
}
cout << mstval << ' ' << mstval + delta << endl;
// cleanup
mstval = 0;
for (int i = 0; i < m; i++)
inmst[i] = false;
for (int i = 1; i <= n; i++)
mst[i].clear();
edges.clear();
}
}优化2
我们就可以用倍增的思想来进一步优化寻找这个最大值的过程,定义表示点在最小生成树上向上跳层途中经过的边的边权最大值,为点在最小生成树上向上跳层的节点。
显然有
这样,我们可以借助数组,从和分别倍增跳到,取较大值。
复杂度:。
CODE
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 1000 + 10, maxm = 1e6 + 10, lg = 10, inf = 1e9 + 10;
int dep[maxn], fa[maxn][lg + 1], mx[maxn][lg + 1], delta, bfa[maxn], n, m, ans,
inmst[maxm], t;
long long mstval;
vector<pair<int, int>> mst[maxn];
struct edge {
int u, v, w;
bool operator<(const edge other) const { return w < other.w; }
};
vector<edge> edges;
int bget(int x) { return bfa[x] == x ? x : bfa[x] = bget(bfa[x]); }
void bmerge(int x, int y) { bfa[bget(x)] = bget(y); }
void dfs(int cur, int pa, int wi) {
dep[cur] = dep[pa] + 1;
fa[cur][0] = pa;
mx[cur][0] = wi;
for (int i = 1; i <= lg; i++)
fa[cur][i] = fa[fa[cur][i - 1]][i - 1];
for (int i = 1; i <= lg; i++)
mx[cur][i] = max(mx[cur][i - 1], mx[fa[cur][i - 1]][i - 1]);
for (pair<int, int> x : mst[cur])
if (x.first != pa)
dfs(x.first, cur, x.second);
}
int lca(int x, int y) {
if (dep[x] < dep[y])
swap(x, y);
for (int i = lg; i >= 0; i--)
if ((dep[x] - dep[y]) & (1 << i))
x = fa[x][i];
if (x == y)
return x;
for (int i = lg; i >= 0; i--)
if (fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
void kruskal() {
for (int i = 1; i <= n; i++)
bfa[i] = i;
for (int i = 0; i < m; i++) {
edge e = edges[i];
if (bget(e.u) != bget(e.v))
bmerge(e.u, e.v), mst[e.u].push_back({e.v, e.w}),
mst[e.v].push_back({e.u, e.w}), mstval += e.w, inmst[i] = 1;
}
}
int getmx(int a, int b) {
int res = 0;
for (int i = lg; i >= 0; i--)
if ((dep[a] - dep[b]) & (1 << i))
res = max(res, mx[a][i]), a = fa[a][i];
return res;
}
int main() {
#ifdef SUMBIT_UVA
cin >> t;
#else
t = 1;
#endif
while (t--) {
int u, v, w;
cin >> n >> m;
for (int i = 0; i < m; i++)
cin >> u >> v >> w, edges.push_back({u, v, w});
sort(edges.begin(), edges.end());
kruskal();
dfs(1, 1, 0);
delta = inf;
for (int i = 0; i < m; i++)
if (!inmst[i]) {
int u = edges[i].u, v = edges[i].v, w = edges[i].w, k = lca(u, v);
delta = min(delta, w - max(getmx(u, k), getmx(v, k)));
}
cout << mstval << ' ' << mstval + delta << endl;
// cleanup
mstval = 0;
for (int i = 0; i < m; i++)
inmst[i] = false;
for (int i = 1; i <= n; i++)
mst[i].clear();
edges.clear();
}
}模板
标准版本:UVA
加强版本:hihocoder
Port2. 严格次小生成树
其实和非严格次小生成树很像,唯一需要注意的是,当最大值和新加的边的权值一样时,为了确保单调性,应当替换严格次大边。
定义表示点在最小生成树上向上跳层途中经过的边的边权的严格次大值,和的定义已在 Port1 中说明,此处不再赘述。
显然,可以通过转移,可这样会漏掉一些情况,如果发现,此时,也可以产生次大值,我们应当额外与取。
接下来,我们对于统计的部分做一些改动,如果发现恰好等于正在处理的边的权值,则应改为而非。
似乎有什么神奇的性质导致如果不判两个最小值相等的情况也对,但我不会证明。




