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 <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <math.h>
using namespace std;
const int maxn = ;
const int INF = 0x7fffffff;
struct node {
int u, v, w;
int used, num, flag;
} qu[ * maxn];
int fa[maxn], n, m;
int cmp(node a, node b) {
return a.w < b.w;
}
void init() {
for (int i = ; i <= n ; i++) fa[i] = i;
}
int Find(int x) {
return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
int combine(int x, int y) {
int nx = Find(x);
int ny = Find(y);
if (nx != ny) {
fa[nx] = ny;
return ;
}
return ;
}
int kruskal(int flag) {
init();
int sum = , cnt = ;
for (int i = ; i < m ; i++) {
if (qu[i].flag) continue;
if (combine(qu[i].v, qu[i].u)) {
if (!flag) qu[i].used = ;
sum += qu[i].w;
cnt++;
if (cnt == n - ) break;
}
}
if (cnt != n - ) return -;
return sum;
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
for (int i = ; i < m ; i++) {
scanf("%d%d%d", &qu[i].u, &qu[i].v, &qu[i].w);
qu[i].flag = , qu[i].used = ;
}
sort(qu, qu + m, cmp);
int sum = kruskal();
int flag = ;
for (int i = ; i < m ; i++) {
if (qu[i].used ) {
qu[i].flag = ;
int temp = kruskal();
qu[i].flag = ;
if (temp == sum) {
flag = ;
break;
}
}
}
if (flag) printf("Not Unique!\n");
else printf("%d\n", sum);
}
return ;
}

poj1679 次最小生成树 kruskal(暴力枚举)的更多相关文章

  1. poj1679(最小生成树)

    传送门:The Unique MST 题意:判断最小生成树是否唯一. 分析:先求出原图的最小生成树,然后枚举删掉最小生成树的边,重做kruskal,看新的值和原值是否一样,一样的话最小生成树不唯一. ...

  2. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  3. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  4. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  5. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

  6. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  7. bzoj 1028 暴力枚举判断

    昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...

  8. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

  9. hihoCoder #1179 : 永恒游戏 (暴力枚举)

    题意: 给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF, ...

随机推荐

  1. 使用JDBC操作数据库

    准备工作 1.创建一个java项目导入mysql驱动包 2.在src目录中创建一个新的Java类 JDBC查询: package com.ATedu.test; import java.sql.Con ...

  2. 解决php文字及图片显示乱码的问题

    我们在学习PHP的过程中,想必有不少新手朋友们都遇到过乱码的问题,解决乱码问题不仅是小白们必须掌握的基础知识点,也是最为常见的PHP面试题之一.下面就结合简单代码示例给大家总结介绍下,PHP遇到乱码时 ...

  3. ADSL_自动拨号源码(Delphi),已经测试通过

    下载地址: http://files.cnblogs.com/lwm8246/ADSL_%E8%87%AA%E5%8A%A8%E6%8B%A8%E5%8F%B7.rar

  4. Python基础-字符串的使用

    基础知识 字符串解释:字符串是不可变的,所有元素赋值和切片赋值操作都是非法的,属于序列一种(字符串.元组.列表). 一.格式化字符串 (1).format()方法==str.format() 作用:将 ...

  5. python flask豆瓣微信小程序案例

    项目步骤 定义首页模板index.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  6. c语言可变参数函数

    c语言支持可变参数函数.这里的可变指,函数的参数个数可变. 其原理是,一般情况下,函数参数传递时,其压栈顺序是从右向左,栈在虚拟内存中的增长方向是从上往下.所以,对于一个函数调用 func(int a ...

  7. 7 Django的模板层

    你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...

  8. 3 网格 landing page

    0.大框架 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  9. Android 支付宝H5 没有回调

    今天测试反馈问题,说,手机上没有安装支付宝的,调用支付宝支付之后,没有回调.不提示成功也不提示失败. 我自己试了半天也都是没有问题 .后来终于可以试出来了. 发现原来是,清单里面注册的Activity ...

  10. Python的类(一)

    类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义在类中且在函数体之外. ...