POJ - 1679_The Unique MST
The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
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:
- V' = V.
- 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!
题意:判断最小生成树是不是唯一。
题解:首先判断能不能形成最小生成树,然后寻找次小生成树,判断是否等于最小生成树,如果不相等,则说明最小生成树唯一。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const int maxn = 105;
const int INF = 1e9+7;
int s[maxn][maxn],n,m;
int prim()
{
int i,j,MIN,k,sum = 0;
int dis[maxn],f[maxn],pre[maxn],maxx[maxn][maxn];/*maxx用来存储最小生成树的 i 到 j 的最大边权值*/
int use[maxn][maxn];
for(i=1;i<=n;i++)
{
dis[i] = s[1][i];
pre[i] = 1;
f[i] = 0;
}
memset(maxx,0,sizeof(maxx));
memset(use,0,sizeof(use));
f[1] = 1;
for(i=1;i<n;i++)
{
MIN = INF;
k = -1;
for(j=1;j<=n;j++)
if(!f[j]&&MIN>dis[j])
{
MIN = dis[j];
k = j;
}
if(MIN==INF)
return -1;
//printf("MIN %d\n",MIN);
f[k] = 1;
sum += MIN;
for(j=1;j<=n;j++)
{
if(f[j])
maxx[k][j] = maxx[j][k] = max(maxx[pre[k]][j],dis[k]);/*(发现直接等于dis[k]也行)*/
else
{
if(dis[j]>s[k][j])
{
dis[j] = s[k][j];
pre[j] = k;
}
}
}
}
//printf("%d\n",sum);
MIN = INF;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(i!=pre[j]&&j!=pre[i]&&s[i][j]!=INF)
MIN = min(MIN,sum-maxx[i][j]+s[i][j]);
}
}
//printf("%d\n",MIN);
if(MIN==sum)
return -1;
else
return sum;
}
int main()
{
int t,i,j,a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
s[i][j] = i==j?0:INF;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(s[a][b]>c)
s[a][b] = s[b][a] = c;
}
a = prim();
if(a==-1)
printf("Not Unique!\n");
else
printf("%d\n",a);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现