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!'.
 
题目大意:判断最小生成树是否唯一(或者说判断次小生成树与最小生成树是否具有同样的权值)
思路:用Kruskal加边的时候,每次判断是否有其他边和当前边具有同样的功能(同样的边权,连接的集合相同),有则输出Not Unique!
 
 #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXE = ;
const int MAXN = ; struct Edge {
int from, to, val;
bool operator < (const Edge &rhs) const {
return val < rhs.val;
}
} edge[MAXE]; int fa[MAXN], deg[MAXN];
int n, ecnt; void init() {
ecnt = ;
for(int i = ; i <= n; ++i) {
fa[i] = i;
deg[i] = ;
}
} void add_edge(int u, int v, int c) {
edge[ecnt].from = u;
edge[ecnt].to = v;
edge[ecnt++].val = c;
} int getfather(int x) {
return fa[x] == x ? x : getfather(fa[x]);
} void union_set(int x, int y) {
int a = getfather(x);
int b = getfather(y);
if(a == b) return ;
if(deg[a] <= deg[b]) swap(a, b);
++deg[a]; fa[b] = a;
} int kruskal() {
int sum = ;
int xa, ya;
sort(edge, edge + ecnt);
for(int i = ; i < ecnt; ++i) {
xa = getfather(edge[i].from);
ya = getfather(edge[i].to);
if(xa == ya) continue;
for(int j = i + ; j < ecnt; ++j) {
if(edge[j].val != edge[i].val) break;
if(xa == getfather(edge[j].from) && ya == getfather(edge[j].to)) {
return -;
break;
}
}
union_set(edge[i].from, edge[i].to);
sum += edge[i].val;
}
return sum;
} int main() {
int T, m, a, b, c;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
init();
for(int i = ; i < m; ++i) {
scanf("%d%d%d", &a, &b, &c);
if(a > b) add_edge(b, a, c);
else add_edge(a, b, c);
}
int ans = kruskal();
if(ans < ) printf("Not Unique!\n");
else printf("%d\n", ans);
}
}

POJ 1679 The Unique MST(最小生成树)的更多相关文章

  1. POJ 1679 The Unique MST (最小生成树)

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

  2. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  3. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  4. poj 1679 The Unique MST (判定最小生成树是否唯一)

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

  5. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

  6. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

  7. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

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

  8. POJ 1679 The Unique MST 推断最小生成树是否唯一

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

  9. poj 1679 The Unique MST【次小生成树】

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

  10. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

随机推荐

  1. shutil.rmtree()

    shutil.rmtree(path, ignore_errors=False, onerror=None)   #递归地删除文件 def rmtree(path, ignore_errors=Fal ...

  2. 分布式时间同步ntp安装

    直接执行:sudo yum install ntp或者sudo -y install ntp

  3. Spark运行模式_基于YARN的Resource Manager的Custer模式(集群)

    使用如下命令执行应用程序: 和"基于YARN的Resource Manager的Client模式(集群)"运行模式,区别如下: 在Resource Manager端提交应用程序,会 ...

  4. linux内核中的IS_ERR()、PTR_ERR()、ERR_PTR()

    IS_ERR宏定义在include/linux/err.h,如下所示: #define MAX_ERRNO 4095 //判断x是不是在(0xfffff000,0xffffffff)之间,注意这里用u ...

  5. Java基础之进制转换

    1.十进制与二进制之间的转换 (1)十进制转二进制的方法:使用十进制的数据不断除以2,直到商为0为止,从下往上取余就是对应的二进制. (2)二进制转十进制:使用二进制的每一位乘以2的n次方,n从0开始 ...

  6. ProtoBuffer由.proto文件生成.cc/.h

    ProtoBuffer由.proto文件生成.cc/.h 一:编译源码下载地址:http://code.google.com/p/protobuf/downloads/list 下载后,根据编译说明进 ...

  7. 手写ORM第一版

    ORM第一版: #Author = __rianley cheng__ #ORM 简易版 from mysql_ import Mysql class Fileld: def __init__(sel ...

  8. AOSP 设置编译输出目录

    export OUT_DIR=/media/caoxinyu/TomasYu/out 注意:export OUT_DIR= OUT_DIR 后面直接跟= ,不要有空格.否则报错.

  9. ORB-SLAM(七)ORBextractor 特征提取

    该类中主要调用OpenCV中的函数,提取图像中特征点(关键点及其描述,描述子,以及图像金字塔) 参考TUM1.yaml文件中的参数,每一帧图像共提取1000个特征点,分布在金字塔8层中,层间尺度比例1 ...

  10. EWS3-24S05电源转换芯片DC-DC

    1. EWS3-24S05是24V转5V的DC-DC电源,输入和输出都是直流电. 2. 典型应用 3. 引脚图 4. 使用注意事项: 输入电源的要求 输入电源的要求产品的输入端必需接一个低阻抗的电压源 ...