题目:
The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31234 | Accepted: 11241 |
Description
Given a connected undirected graph, tell if its minimum spanning tree is unique.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.
Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
Output
For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.
Sample Input
2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2
Sample Output
3 Not Unique!
判最小生成树是否唯一 裸次小生成树
代码:
1.kruskal不是很好用的板子 2831过不了
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1010;
const int maxm = 100010;
const int inf=0x7f7f7f7f;
int n,m,t;
struct edge{
int from,to,w;
bool vis;
}E[maxm];
bool cmp(edge a,edge b){
return a.w < b.w;
}
struct edge1{
int to;
int next;
}Vertex[maxn];//边数组,表示结点连向的边
int head[maxn]; //邻接表头结点位置
int End[maxn]; //邻接表尾结点位置,方便合并
int Len[maxn][maxn];//图中两点之间在最小生成树上路径最长的边
int fa[maxn];
int find(int x){
if(x!=fa[x]) fa[x]=find(fa[x]);
return fa[x];
}
void Kruskal(){
int x,y,k=0,ans=0;
//初始化邻接表,每个节点初始的时候添加一条指向自己的边,表示结点i各自为一个集合
memset(head,-1,sizeof(head));
memset(End,-1,sizeof(End));
for(int i=1;i<=n;++i){
Vertex[i].to=i;
Vertex[i].next=head[i];
End[i]=i;
head[i]=i;
}
sort(E,E+m,cmp);
for(int i=0;i<m;++i){
if(k==n-1) break;
if(E[i].w<0) continue;
x=find(E[i].from);
y=find(E[i].to);
if(x!=y){
for(int w=head[x];w != -1;w=Vertex[w].next){//遍历两个节点所在的集合
for(int v=head[y];v != -1;v=Vertex[v].next){
Len[Vertex[w].to][Vertex[v].to]=Len[Vertex[v].to][Vertex[w].to]=E[i].w;
//当前加入的边一定是加(x,y)边成环后删去的除(x,y)外长度最大的边
}
}
Vertex[End[y]].next=head[x];//合并两个邻接表,表示两点已连边连在一个集合中,最终连成一个最小生成树
head[x]=head[y];
End[y]=End[x];
fa[y]=x;
k++;
E[i].vis=true;
}
}
}
int main(){//4804K 16MS
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) fa[i]=i;
memset(Len,inf,sizeof(Len));
for(int i=0;i<m;++i){
scanf("%d%d%d",&E[i].from,&E[i].to,&E[i].w);
E[i].vis=false;
}
int mst1,mst2;//最小和次小
Kruskal();
mst1=0;
for(int i=0;i<m;++i){
if(E[i].vis) mst1+=E[i].w;
}
mst2=inf;
for(int i=0;i<m;++i){
if(!E[i].vis)//加边,并删去最小生成树上的边
mst2=min(mst2,mst1+E[i].w-Len[E[i].from][E[i].to]);
}
if(mst2==mst1) puts("Not Unique!");
else printf("%d\n",mst1);
}
return 0;
}
2.邻接矩阵+prim 无堆优化,本题效率和kruskal差不多
算法思想:
先跑一遍最小生成树,然后枚举不在生成树里的边(u,v),把它加入到生成树中,并删除u->v这条环路里的边中除了(u,v)外的最大边,得到次小生成树。
因此需要预处理生成树中任意两点间的最大边权dp[i][j](在树中i->j的路径是唯一的)。Prim的思想是每次向树中加入一个顶点i,若i加入生成树之后与树中的j相连,则有转移方程:dp[i][j] = dp[j][i] = max(dp[j][pre[i]],low[i])
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1010;
const int maxm=100010;
const int inf=0x7f7f7f7f;
struct Edge{
int u,v,c;
}E[maxm];///只参与输入输出处理
int t,n,m;
int G[maxn][maxn],path[maxn][maxn];///图 生成树中两点间最大边权
int pre[maxn],low[maxn];
bool vis[maxn],used[maxn][maxn];///点 边使用与否
int Prim(){///返回最小生成树值 若无法生成一棵树,返回-1
int ans = 0;
memset(path,0,sizeof(path));
memset(vis,false,sizeof(vis));
memset(used,false,sizeof(used));
vis[1] = true;
int k;
pre[1] = -1; low[1] = 0;
for(int i = 2; i <= n; i++){///点的编号从1开始
low[i] = G[1][i];
pre[i] = 1;
}
for(int i = 2; i <= n; i++){
int MIN = inf;
k = -1;
for(int j = 1; j <= n; j++){
if(!vis[j] && low[j] < MIN){
MIN = low[j];
k = j;
}
}
if(MIN == inf) return -1;
ans += MIN;
vis[k] = true;
used[k][pre[k]] = used[pre[k]][k] = true;
for(int j = 1; j <= n; j++){
if(vis[j]) path[j][k] = path[k][j] = max(path[j][pre[k]],low[k]);
if(!vis[j] && low[j] > G[k][j]){
low[j] = G[k][j];
pre[j] = k;
}
}
}
return ans;
}
int main(){///9772K 32MS
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
memset(G,inf,sizeof(G));
for(int i = 1; i <= m; ++i){
scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].c);
G[E[i].u][E[i].v] = G[E[i].v][E[i].u] = E[i].c;
}
int mst1 = Prim();
int mst2=inf;
for(int i = 1; i <= m; ++i){
if(!used[E[i].u][E[i].v])
mst2 = min(mst2,mst1 + E[i].c - path[E[i].u][E[i].v]);
}
///cout<<mst1<<" "<<mst2<<endl;
if(mst1 != mst2) printf("%d\n",mst1);
else puts("Not Unique!");
}
return 0;
}