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!
题意:求其最小生成树,如果最小生成树的长度与次小生成树的长度相等(这里只要判断最小差值是否为0即可),则不唯一。否则唯一。
#include<iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int M=1005;
const int inf=9999999;
int n,m,ans;
int map[M][M],vis[M],dis[M];
int maxtree[M][M],father[M],stack[M];
void init()
{
int i,j;
for (i = 0; i < M; ++i)
{
for (j = 0; j < M; ++j)
{
if (i == j)
map[i][j] = 0;
else
map[i][j] = inf;
maxtree[i][j] = -1;
}
}
}
void prim()
{
int i,j,temp;
ans=0;
for(i=1;i<=n;i++)
{
father[i]=1;
dis[i]=map[1][i];
vis[i]=0;
}
vis[1]=1;
int t=0;
stack[t++]=1;
for(i=1;i<n;i++)
{
int MIN=inf;
for(j=2;j<=n;j++)
{
if(dis[j]<MIN&&!vis[j])
{
temp=j;
MIN=dis[j];
}
}
vis[temp]=1;
ans+=MIN;
//运用DP求任意两点的最大边权
for(int k=0;k<t;k++)
{
maxtree[temp][stack[k]]=maxtree[stack[k]][temp]=max(MIN,maxtree[stack[k]][father[temp]]);
}
stack[t++]=temp;
for(int k=2;k<=n;k++)
{
if(!vis[k]&&dis[k]>map[k][temp]&&map[k][temp]!=inf)
{
dis[k]=map[k][temp];
father[k]=temp;
}
}
}
}
int main()
{
int i,j,t;
cin>>t;
int a,b,len;
while(t--)
{
init();
cin>>n>>m;
for(i=0;i<m;i++)
{
cin>>a>>b>>len;
map[a][b]=map[b][a]=len;
}
prim();
int c=inf;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i!=j&&father[j]!=i&&j!=father[i]&&map[i][j]!=inf)
{
c=min(map[i][j]-maxtree[i][j],c);
}
}
}
if(c==0)
cout<<"Not Unique!"<<endl;
else
cout<<ans<<endl;
}
return 0;
}
法二:
#include <cstdio>
#include <cstring>
const int INF=0x7fffffff;
int map[105][105],dis[105];
int m,n;
bool visit[105];
void init ()
{
int i;
for (i=1; i<=n; i++)
for (int j=1; j<=n; j++)
map[i][j]=INF;
for (i=1; i<=n; i++)
map[i][i]=0;
memset(visit,false,sizeof(visit));
}
int Prim ()
{
int i,j,k;
int temp,ans=0;
for (i=1; i<=n; i++)
dis[i]=map[1][i];
visit[1]=true;
for (i=1; i<n; i++)
{
temp=INF;
for (j=1; j<=n; j++)
{
if (dis[j]<temp && visit[j]==false)
{
temp=dis[j];
k=j;
}
}
ans+=temp;
visit[k]=true;
for (j=1; j<=n; j++)
{
if (visit[j]==false && dis[j]==temp)
return -1;
}
for (j=1; j<=n; j++)
{
if (visit[j]==false)
if (dis[j]>map[k][j])
dis[j]=map[k][j];
else if (dis[j]==map[k][j] && dis[j]!=INF)
return -1;
}
}
return ans;
}
int main ()
{
int T;
scanf("%d",&T);
while (T--)
{
int x,y,w;
scanf("%d%d",&n,&m);
init ();
for (int i=0; i<m; i++)
{
scanf("%d%d%d",&x,&y,&w);
map[x][y]=w;
map[y][x]=w;
}
int ans=Prim();
if (ans == -1)
printf("Not Unique!\n");
else
printf("%d\n",ans);
}
return 0;
}