K - The Unique MST (最小生成树的唯一性)
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
Output
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:首先算出最小生成树的权值和ans,然后枚举删除最小生成树中的每一条边,若还可以达到相同的效果,就说明最小生成树不唯一,
因为两个不同的最小生成树至少有一条边不同,所以我们才可以枚举删除每一条边.
方法2:判断最小生成树和次小生成树的权值是否相同.
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=;
int f[maxn];
struct node
{
int u,v,w;
bool operator < (const node &r)const{
return w<r.w;
}
}q[maxn];
int Find(int x)
{
return f[x]==x?x:f[x]=Find(f[x]);
}
int Merge(int u,int v)
{
u=Find(u);
v=Find(v);
if(u!=v)return f[u]=v,;
return ;
}
vector<int>v;
int main()
{
int T;
cin>>T;
while(T--){
v.clear();
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++)f[i]=i;
for(int i=;i<=m;i++){
cin>>q[i].u>>q[i].v>>q[i].w;
}
sort(q+,q++m);
int ans=;
for(int i=;i<=m;i++){
int x=Merge(q[i].u,q[i].v);
if(x){
v.push_back(i);
ans+=q[i].w;
}
}
int flag=;
for(int i=;i<v.size();i++){
int sum=,cnt=;
for(int j=;j<=n;j++)f[j]=j;
for(int j=;j<=m;j++){
if(j==v[i])continue;
int x=Merge(q[j].u,q[j].v);
if(x){
sum+=q[j].w;
cnt++;
}
}
if(cnt==n-&&ans==sum){
flag=;
break;
}
}
if(flag)cout<<ans<<endl;
else printf("Not Unique!\n"); }
return ;
}
#include<iostream>
#include<cstring> using namespace std;
typedef long long ll;
const int maxn=;
const int INF=0x3f3f3f3f;
int Maxlen[maxn][maxn];
int dis[maxn],vis[maxn];
int pre[maxn],MAP[maxn][maxn];
int used[maxn][maxn];
int n,m; int Prim(int x)
{
memset(Maxlen,,sizeof(Maxlen));
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
memset(pre,,sizeof(pre));
memset(used,,sizeof(used));
for(int i=;i<=n;i++){
dis[i]=MAP[x][i];
pre[i]=x;
}
dis[x]=;
vis[x]=;
pre[x]=;
int ans=;
for(int i=;i<=n;i++){
int u=,minn=INF;
for(int j=;j<=n;j++){
if(!vis[j]&&dis[j]<minn){
u=j;
minn=dis[j];
}
}
vis[u]=;
ans+=minn;
used[u][pre[u]]=used[pre[u]][u]=;
for(int v=;v<=n;v++){
if(vis[v]){
Maxlen[u][v]=Maxlen[v][u]=max(Maxlen[v][pre[u]],dis[u]);
}
else{
if(dis[v]>MAP[u][v]){
dis[v]=MAP[u][v];
pre[v]=u;
}
}
}
}
return ans;
}
void sst(int ans)
{
int sum=INF;
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(!used[i][j]&&MAP[i][j]!=INF){
sum=min(sum,ans+MAP[i][j]-Maxlen[i][j]);
}
}
}
if(sum==ans)cout<<"Not Unique!"<<endl;
else cout<<ans<<endl;
}
int main()
{
int T;
cin>>T;
while(T--){
cin>>n>>m;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j)MAP[i][j]=;
else MAP[i][j]=INF;
}
}
for(int i=;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
MAP[u][v]=MAP[v][u]=min(MAP[u][v],w);
}
int ans=Prim();
sst(ans);
}
return ;
}
K - The Unique MST (最小生成树的唯一性)的更多相关文章
- The Unique MST(最小生成树的唯一性判断)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- K - The Unique MST
K - The Unique MST #include<iostream> #include<cstdio> #include<cstring> #include& ...
- [poj1679]The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28207 Accepted: 10073 ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- K - The Unique MST - poj 1679
题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...
- POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27141 Accepted: 9712 D ...
- poj1679 The Unique MST(最小生成树唯一性)
最小生成树的唯一性,部分参考了oi-wiki 如果一条不在最小生成树边集内的边,它可以替换一条在最小生成树边集内,且权值相等的边,那么最小生成树不是唯一的 同过kruskal来判断 考虑权值相等的边, ...
- (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- The Unique MST (判断是否存在多个最小生成树)
The Unique MST Time Limit: 10 ...
随机推荐
- 第一章:MySQL架构和历史
1.MySQL逻辑架构 MySQL存储引擎架构:将查询处理以及其他任务系统和数据的存储处理分离开来,这样做的好处在于可以根据需要灵活选择存储引擎. 第一层架构 -- 连接处理.授权认证.安全等. 第二 ...
- 【转】pip升级不成功怎么办
python -m pip install --upgrade pip -i https://pypi.douban.com/simple
- mysql锁探究和实验
如何保证数据并发访问的一致性.有效性是所有数据库必须解决的一个问题,锁冲突也是影响数据库并发访问性能的一个重要因素.从这个角度来说,锁对数据库而言显得尤其重要,也更加复杂. 表锁和行锁 mysql最显 ...
- sed使用案例
简介: sed是一种流编辑器,它是文本处理中非常重要的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用 ...
- 文献阅读报告 - Social LSTM:Human Trajectory Prediction in Crowded Spaces
概览 简述 文献所提出的模型旨在解决交通中行人的轨迹预测(pedestrian trajectory prediction)问题,特别是在拥挤环境中--人与人交互(interaction)行为常有发生 ...
- php里parent,::和self的分别
01.php里parent,::和self的分别/*self的特点*/class a{ public static $a1="我是类a"; function ca() { echo ...
- [转载]matlab视频读取函数VideoReader
看到以前matlab中读取视频多 使用mmreader等(参考<matlab读取/播放视频的函数>),而现在matlab有一个专门的视频读取类VideoReader完成视频读取的功能. 相 ...
- 创建DateFrame的常用四种方式
import pandas as pd %pylab 一.使用numpy创建 df = pd.DataFrame(np.arange(16).reshape((4,4)), index=list('a ...
- 将元素平分成差值最小的两个集合(DP)
现有若干物品,要分成较为平均的两部分,分的规则是这样的: 1)两部分物品的个数最多只能差一个. 2)每部分物品的权值总和必须要尽可能接近. 现在请你编写一个程序,给定现在有的物品的个数以及每个物品的权 ...
- 探讨 Git 代码托管平台的若干问题 - 2019 版
关于 Git 版本控制软件种类繁多,维基百科收录的最早的版本控制系统是 1972 年贝尔实验室开发的 Source Code Control System.1986 年 Concurrent Vers ...