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. DB - RDMS - MySQL优化

    慢SQL会消耗打来难过的数据库CPU资源,特别是频繁执行的慢SQL语句,会造成大量任务的堆积,CPU瞬间增大.

  2. 用 js 写一个获取随机颜色的程序

    function getColor(){ var color="#"; for(var i=0;i<6;i++){ color+=(Math.random()*16 | 0) ...

  3. MyCat实现数据库与数据库之间的读写分离

    一.Mycat的安装准备: 1.jdk:要求jdk必须是1.7及以上版本 2.Mysql:推荐mysql是5.5以上版本 3.Mycat: Mycat的官方网站: http://www.mycat.o ...

  4. poj 2674 线性世界 弹性碰撞

    弹性碰撞的题目一般都是指碰到就会掉转方向的一类题目,这里我们可以忽略掉头,仅仅看成擦肩而过,交换名字等等 题意:一条线上N只蚂蚁,每只蚂蚁速度固定,方向和坐标不同,碰头后掉头,求最后掉下去那只蚂蚁的名 ...

  5. Echarts 背景渐变柱状图

    var dom = document.getElementById("container"); var myChart1 = echarts.init(dom); var app ...

  6. 【数据库】MySQL 从安装到命令

    一, MySQL 的安装于配置 我是通过百度云盘的方式下载的.建议登录百度云终端,然后点击下面的链接,选择要安装的版本,解压安装. http://www.h2ero.cn/pan/share/17cd ...

  7. BZOJ 3027: [Ceoi2004]Sweet

    容斥 #include<cstdio> using namespace std; int a,b,n,m[15]; long long ans=0,mod=2004; long long ...

  8. android 事件拦截 (Viewpager不可以左右滑动)

    以前没有做过真正的需求,所以从来没有觉得事件拦截分发处理有什么好懂的. 现在做需求了,真的是什么需求都有,你作为开发都要去研究实现.比如说,只能点不能滑动的viewpager.其实这都可以不用view ...

  9. java面试二

    技术交流群: 233513714 126.什么是ORM?答:对象关系映射(Object-Relational Mapping,简称ORM)是一种为了解决程序的面向对象模型与数据库的关系模型互不匹配问题 ...

  10. Asp.net Core发布到CentOS7

    第一步.安装CentOS 官网https://www.centos.org/下载CentOS,下载地址https://www.centos.org/download/,我选的“DVD ISO”,然后虚 ...