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

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!
题意:判断是否存在唯一的最小生成树。
思路:先求出一个最小生成树。在这颗树上先加入(x,y),加入后一定会成环,如果删除(x,y)以外最大的一条边,会得到加入(x,y)时权值最小的一棵树。如果加入的边和删除的边相等,则最小生成不是唯一的。
收获:了解了次小生成树。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500
int n, m;
int a[maxn][maxn];
int used[maxn][maxn];
int mmax[maxn][maxn];
int vis[maxn];
int dis[maxn];
int pre[maxn];
int prim()
{
int ans = ;
memset(vis, , sizeof vis);
memset(used, , sizeof used);
memset(mmax, ,sizeof mmax);
for(int i = ; i <= n; i++)
{
dis[i] = a[][i];
pre[i] = ;
}
vis[] = ;
dis[] = ;
for(int i = ; i < n; i++)
{
int temp = INF;
int k = -;
for(int j = ; j <= n; j++)
{
if(!vis[j] && temp > dis[j])
{
temp = dis[j];
k = j;
}
}
if(k == -) return ans;
ans += temp;
vis[k] = ;
used[k][pre[k]] = used[pre[k]][k] = ;
mmax[pre[k]][k] = temp;
for(int j = ; j <= n; j++)
mmax[j][k] = max(mmax[j][pre[k]],mmax[pre[k]][k] );//找最大的边权的边。
for(int j = ; j <= n; j++)
{
if(!vis[j] && dis[j] > a[k][j])
{
dis[j] = a[k][j];
pre[j] = k;
}
}
}
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
int u, v, w;
memset(a, INF, sizeof a);
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
a[u][v] = a[v][u] = w;
}
int mst = prim();
int flag = ;
for(int i = ; i <= n; i++)
for(int j = i+; j <= n; j++)
{
if(a[i][j] == INF || used[i][j] == )
continue;
if(a[i][j] == mmax[i][j])//判断加入的边和删除的边是否相等。
{
flag = ;
break;
}
}
if(flag)
printf("Not Unique!\n");
else
printf("%d\n",mst);
}
return ;
}

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

  1. poj1679次小生成树入门题

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

  2. poj1679 次小生成树

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

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

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

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

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

  5. POJ1679(次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36692   Accepted: 13368 ...

  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. Hive 5、Hive 的数据类型 和 DDL Data Definition Language)

    官方帮助文档:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL Hive的数据类型 -- 扩展数据类型data_t ...

  2. poj 3320 Jessica's Reading Problem(尺取法)

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  3. qt坐标系统

    #说明:坐标系统是由 QPainter控制的QPaintDevice是那些能够让 QPainter 进行绘制的“东西”(准确的术语叫做,二维空间)# 的抽象层(其子类有QWidget. QPixmap ...

  4. 分享一个3D球面标签云

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 在Qt中用QAxObject来操作Excel

        目录(?)[+]   下一篇:用dumpcpp工具生成的excel.h/excel.cpp来操纵Excel 最近写程序中需要将数据输出保存到Excel文件中.翻看<C++ GUI Pro ...

  6. JQuery.validate在ie8下不支持解决方案

    一.在ie8下回有问题的代码 1.JQuery.validate验证框架是通过页面form表单提交验证<input/>标签中输入是否符合自己的规则的 <form id="c ...

  7. struts2 标签

    一.逻辑控制标签 用于进行逻辑控制输出.主要分以下几类: 1)条件标签:用于执行基本的条件流转 <s:if>:拥有一个test属性,其表达式的值用来决定标签里内容是否显示.<s:if ...

  8. M - Candy Sharing Game

    Description A number of students sit in a circle facing their teacher in the center. Each student in ...

  9. uva11630 or hdu2987 Cyclic antimonotonic permutations(构造水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Cyclic antimonotonic permutations Time Li ...

  10. (原+转)简明 Python 教程:总结

     简明 Python 教程 说明:本文只是对<简明Python教程>的一个总结.请搜索该书查看真正的教程. 第3章 最初的步骤 1. Python是大小写敏感的. 2. 在#符号右面的内容 ...