The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36692   Accepted: 13368

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'.

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 <stdio.h>
#include <algorithm>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
#define line cout<<"------------------"<<endl;
const int MAXN=1e4+10;
const int INF=0x3f3f3f3f;
int n,m;
struct node{
int x,y;
int v;
bool vis;
}Edge[MAXN];
bool cmp(node a,node b)
{
return a.v<b.v;
}
int pre[MAXN];
int Find(int a)
{
if(pre[a]==a)
return a;
return Find(pre[a]);
}
vector<int >G[110]; int maxd[110][110];//并查集划到一个树上后,树上任意两点之间的距离 void init()
{
for (int i = 1; i <=n; ++i) {
G[i].clear();
pre[i] = i;
G[i].push_back(i);
} }
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&Edge[i].x,&Edge[i].y,&Edge[i].v);
Edge[i].vis=false;
}
sort(Edge+1,Edge+1+m,cmp);
int sum=0;
for (int i = 1; i <=m ; ++i) {
int x=Find(Edge[i].x);
int y=Find(Edge[i].y);
if(x!=y)
{
pre[x]=y;
sum+=Edge[i].v;
int len1=G[x].size();
int len2=G[y].size();
for (int j = 0; j <len1 ; ++j) {
for (int k = 0; k <len2 ; ++k) {
maxd[G[x][j]][G[y][k]]=maxd[G[y][k]][G[x][j]]=Edge[i].v;//构建两点间最小距离
}
}
int tem[110];
for (int j = 0; j <len2 ; ++j) {
tem[j]=G[y][j];
}
for (int j = 0; j <len1 ; ++j) {
G[y].push_back(G[x][j]);
}
for (int j = 0; j <len2 ; ++j) {
G[x].push_back(tem[j]);
}
Edge[i].vis=true;
}
}
int cis=INF;
for (int i = 1; i <=m ; ++i) {//从不是最小生成树上的边,遍历向上加。找到次小生成树
if(!Edge[i].vis)
cis=min(cis,sum+Edge[i].v-maxd[Edge[i].x][Edge[i].y]);
}
if(cis>sum)
printf("%d\n",sum);
else
printf("Not Unique!\n");
} return 0;
}
//poj1679

  

POJ1679(次小生成树)的更多相关文章

  1. POJ1679(次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24201   Accepted: 8596 D ...

  2. poj1679次小生成树入门题

    次小生成树求法:例如求最小生成树用到了 1.2.4这三条边,总共5条边,那循环3次的时候,每次分别不用1.2.4求得最小生成树的MST,最小的MST即为次小生成树 如下代码maxx即求最小生成树时求得 ...

  3. poj1679 次小生成树

    prim方法:先求过一遍prim,同时标记使用过得边.然后同时记录任意2点间的最大值. 每次加入一条新的边,会产生环,删去环中的最大值即可. #include<stdio.h> #incl ...

  4. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  5. 次小生成树(POJ1679/CDOJ1959)

    POJ1679 首先求出最小生成树,记录权值之和为MinST.然后枚举添加边(u,v),加上后必形成一个环,找到环上非(u,v)边的权值最大的边,把它删除,计算当前生成树的权值之和,取所有枚举加边后生 ...

  6. POJ1679 The Unique MST【次小生成树】

    题意: 判断最小生成树是否唯一. 思路: 首先求出最小生成树,记录现在这个最小生成树上所有的边,然后通过取消其中一条边,找到这两点上其他的边形成一棵新的生成树,求其权值,通过枚举所有可能,通过这些权值 ...

  7. POJ1679 The Unique MST 【次小生成树】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20421   Accepted: 7183 D ...

  8. 次小生成树(poj1679)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20737   Accepted: 7281 D ...

  9. POJ1679 The Unique MST —— 次小生成树

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

随机推荐

  1. python 学习 (迟到的2017小结)

    2017过去了,从前端开发转测试,入行一年多,写了web的UI自动化测试框架,看了python基础教程,核心编程,算是入门python了吧.目前呢,想开发个接口的自动化测试平台.           ...

  2. OpenGL中的数据——Buffer

    OpenGL中主要包括了两种数据——Buffer和Texture. Buffer用于储存线性数无类型据块,可以看成普通的内存块,而Texture则用于储存多维数据,一般储存图像或者其他数据. Buff ...

  3. SQA冲刺

    下学期的第三次冲刺 一 SQA是什么 软件质量保证的目的是使软件过程对于管理人员来说是可见的.它通过对软件产品和活动进行评审和审计来验证软件是合乎标准的.软件质量保证组在项目开始时就一起参与建立计划. ...

  4. Cloud Foundry Session Affinity(Sticky Session)的实现

    会话保持(Session Affinity),有时又称粘滞会话(Sticky Sessions), 是负载均衡领域设计需要着力解决的重要问题之一,也是一个相对比较复杂的问题. 会话保持是指在负载均衡器 ...

  5. SAPGUI里实现自定义的语法检查

    需求:在SAPGUI里点击这个语法检查的小图标或者直接按快捷键Ctrl+F2可以执行ABAP标准的语法检查. 如果需要实现SAPGUI里自定义的语法检查,比如,某团队强制要求应用程序类的每个方法的实现 ...

  6. Android——Intent,Bundle

    Intent——切换activity intent.setClass(first.this,second.class); startActivity(intent); Bundle——切换时数据传递 ...

  7. 整个ssd的网络和multibox_loss_layer

    总结说来prior_box层只完成了一个提取anchor的过程,其他与gt的match,筛选正负样本比例都是在multibox_loss_layer完成的 http://www.360doc.com/ ...

  8. putty 启动 linux 下的oracle

    没搞过linux ,仅作记录: 1 打开putty.exe 程序 ,选择 连接 2 输入linux 的用户名和密码后,按下图操作: 3  启动监听 4 命令总结: 1.  sudo su - orac ...

  9. oracle-sql脚本导出EXCEL数据

    在数据库中,经常有业务人员提出需求导出数据库中的业务数据,而且是每天.每周或每月定时导出.为了方便,可将sql查询的脚本 通过下面脚本来导出EXCEL数据. 1.将查询sql脚本(AAA.sql)放到 ...

  10. Windows无法安装到这个磁盘 选中的磁盘具有MBR分区表解决方法

    在安装 win10的时候,会出现这种提示:Windows 无法安装到这个磁盘.选中的磁 盘具有 MBR 分区表.在 EFI 系统上, Windows 只能安装到 GPT 磁盘.出现这种 情况主要是因为 ...