求次小生成树:
方法1:
记录最小生成树中的所有边,再枚举删边然后重新建树,取权值最小的生成树,就是次小生成树。
因为n个点的边有n-1条,用kruskal只需要排序一次所以每次建树需要遍历m条边,所有复杂度是O(nm)。
例题:poj 1679
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int fa[110];
void init() {
for(int i = 0; i <= 105; i++) fa[i] = i;
}
struct Edge {
int w, x, y;
bool operator < (const Edge &i) const {
return w < i.w;
}
}edge[5100];
int findfa(int a) {
if(fa[a] == a) return a;
return fa[a] = findfa(fa[a]);
}
void unit(int a, int b) {
int t1 = findfa(a), t2 = findfa(b);
fa[a] = min(t1, t2);
fa[b] = min(t1, t2);
}
bool specialjudge(int n) {
int p = findfa(1);
for(int i = 2; i <= n; i++)
if(findfa(i) != p)
return false;
return true;
}
int ed[200];
main() {
// freopen("in.in", "r", stdin);
int t, n, m;
scanf("%d", &t);
while(t--) {
int val = 0;
init();
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) scanf("%d %d %d", &edge[i].x, &edge[i].y, &edge[i].w);
sort(edge, edge + m);
bool flag = false;
int cnt = 0;
for(int i = 0; i < m; i++) {
int fa1 = findfa(edge[i].x), fa2 = findfa(edge[i].y);
if(fa1 == fa2 && !flag) {
flag = true;
}
if(fa1 != fa2) {
unit(fa1, fa2);
val += edge[i].w;
ed[cnt++] = i;
}
}
if(!specialjudge(n)) {
puts("0");
continue;
}
if(!flag) {
printf("%d\n", val);
continue;
}
int vval = 0x7fffffff;
for(int i = 0; i < cnt; i++) {
init();
int val2 = 0;
for(int j = 0; j < m; j++) {
if(ed[i] == j) continue;
int fa1 = findfa(edge[j].x), fa2 = findfa(edge[j].y);
if(fa1 != fa2) {
unit(fa1, fa2);
val2 += edge[j].w;
}
}
if(specialjudge(n)) {
vval = min(vval, val2);
}
}
if(vval == val)
puts("Not Unique!");
else
printf("%d\n", val);
}
}
方法2:
先求出所有最小生成树的边的集合T,然后枚举和每个点相连距离第二长的边Li,在T中添加Li,这样必形成环,删除环中除了Li外最长的边,枚举所有点后取权值最小的生成树就是次小生成树。
如图,最小生成树是1-2-3-4-5,如果添加边1-3,这样在环里减去边2-3,就是次小生成树2-1-3-4-5。
可以通过建有根树来更新任意两点间路径最长边,dp[x,v]=max(dp[x,u], w[u, v]) u是v的父节点,x是已经计算过距离的点,v是新加的节点。在lrj的《挑战程序设计竞赛训练指南》里也有讲解。
时间复杂度是(n^2+mlogm)。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int fa[110], edmax[110][110], ver[110], cnt;
int t, n, m;
bool vis[110], vis2[5100];
void init() {
for(int i = 0; i <= 105; i++) fa[i] = i;
memset(edmax, -1, sizeof edmax);
memset(vis, 0, sizeof vis);
cnt = 0;
memset(vis2, 0, sizeof vis2);
}
struct Edge {
int w, x, y;
bool operator < (const Edge &i) const {
return w < i.w;
}
}edge[5100];
int findfa(int a) {
if(fa[a] == a) return a;
return fa[a] = findfa(fa[a]);
}
void unit(int a, int b) {
int t1 = findfa(a), t2 = findfa(b);
fa[a] = min(t1, t2);
fa[b] = min(t1, t2);
}
bool specialjudge(int n) {
int p = findfa(1);
for(int i = 2; i <= n; i++)
if(findfa(i) != p)
return false;
return true;
}
void dfs(int a, int father) {//更新任意两点间路径最长边
for(int i = 0; i < cnt; i++)
edmax[ver[i]][a] = edmax[a][ver[i]] = max(edmax[ver[i]][father], edmax[a][father]);
ver[cnt++] = a;
vis[a] = true;
for(int i = 1; i <= n; i++) {
if(edmax[a][i] != -1 && !vis[i]) {
dfs(i, a);
}
}
}
main() {
// freopen("in.in", "r", stdin);
scanf("%d", &t);
while(t--) {
int val = 0;
init();
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) scanf("%d %d %d", &edge[i].x, &edge[i].y, &edge[i].w);
sort(edge, edge + m);
bool flag = false;
for(int i = 0; i < m; i++) {//kruskal
int fa1 = findfa(edge[i].x), fa2 = findfa(edge[i].y);
if(fa1 == fa2 && !flag) {
flag = true;
}
if(fa1 != fa2) {
unit(fa1, fa2);
val += edge[i].w;
edmax[edge[i].x][edge[i].y] = edmax[edge[i].y][edge[i].x] = edge[i].w;
vis2[i] = true;
}
}
if(!specialjudge(n)) {
puts("0");
continue;
}
if(!flag) {
printf("%d\n", val);
continue;
}
dfs(1, 1);
int vval = 0x7fffffff;
for(int i = 0; i < m; i++) {//枚举新增的边
if(!vis2[i])
vval = min(vval, val - edmax[edge[i].x][edge[i].y] + edge[i].w);
}
if(vval == val) puts("Not Unique!");
else printf("%d\n", val);
}
}