The Unique MST

http://poj.org/problem?id=1679

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36744   Accepted: 13395

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!

Source

模板题

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define N 250500
#define MOD 1e9+7
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
struct sair{
int x,y,v;
}a[];
int fa[];
int n,m;
bool cmp(sair a,sair b){
return a.v<b.v;
} int Find(int x){
int r=x,y;
while(x!=fa[x]){
x=fa[x];
}
while(x!=r){
y=fa[r];
fa[r]=x;
r=y;
}
return x;
} int join(int x,int y){
int xx=Find(x);
int yy=Find(y);
if(xx==yy){
return ;
}
fa[xx]=yy;
return ;
} vector<int>v; int check(int xxx){
int ans=;
int xxxx=;
for(int i=;i<=n;i++){
fa[i]=i;
}
for(int i=;i<=m;i++){
if(i!=xxx){
if(join(a[i].x,a[i].y)){
ans+=a[i].v;
xxxx++;
}
}
}
if(xxxx==n)
return ans;
return -;
} int main(){
std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=;i<=n;i++){
fa[i]=i;
}
for(int i=;i<=m;i++){
cin>>a[i].x>>a[i].y>>a[i].v; }
int ans1=;
v.clear();
sort(a+,a+m+,cmp);
for(int i=;i<=m;i++){
if(join(a[i].x,a[i].y)){
ans1+=a[i].v;
v.push_back(i);
}
}
int flag=;
for(int i=;i<v.size();i++){
if(check(v[i])==ans1){
flag=;
break;
}
}
if(flag){
cout<<ans1<<endl;
}
else{
cout<<"Not Unique!"<<endl;
}
}
}

The Unique MST的更多相关文章

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

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

  2. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

  3. POJ1679The Unique MST(次小生成树)

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

  4. poj 1679 The Unique MST

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

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

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

  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(唯一的最小生成树)

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

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

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

  9. The Unique MST (判断是否存在多个最小生成树)

    The Unique MST                                                                        Time Limit: 10 ...

  10. POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

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

随机推荐

  1. document.write覆盖问题

    1.不会覆盖页面: 如果仅仅是一句js语句:document.writeln('<span id=\'span1\'>嗯,你好</span>'); ps:\'是转义字符,代表' ...

  2. BOM及改变this指向

    bom ( borwser object model 浏览器对象模型) 定义js操作浏览器的属性和方法 window.open(url way())    中有两个参数 url代表打开的网页地址 wa ...

  3. (转!)Netdata---Linux系统性能实时监控平台部署

    我一直以为人是慢慢变老的,其实不是,人其实是一瞬间变老的. -------村上春树<舞!舞!舞!> 转自https://www.cnblogs.com/kevingrace/p/73001 ...

  4. Spring MVC 底层原理

    参考博客:http://www.cnblogs.com/xiaoxi/p/6164383.html Spring MVC处理的流程: 具体执行步骤如下: 1 首先用户发送请求给前端控制器,前端控制器根 ...

  5. 【基础知识六】支持向量机SVM

    开发库: libsvm, liblinear      GitHub地址 SVM难点:核函数选择 一.基本问题 找到约束参数ω和b,支持向量到(分隔)超平面的距离最大:此时的分隔超平面称为“最优超平面 ...

  6. python之路05

    一  元组 对于元组我们可以把他看成一个不可变的列表# 元组:在()内用逗号分隔开的能够存多个值,对于元组来说列表有的一些功能它基本上都有,# 1.按索引取值(正向取+反向取):只能取# 2.切片(顾 ...

  7. Out 与 Ref 关键字的区别

    相同点:既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值,并保持该更改.若要通过引用传递参数, 可使用ref或out关键字.ref和out这两个关键字都能够提供相似的功效, ...

  8. python连接数据库(pymysql)及数据库加密

    内容: 1.pymysql介绍 2.pymysql基本使用 3.数据库加密 参考:http://www.cnblogs.com/wupeiqi/articles/5713330.html 1.pymy ...

  9. Win10交换Ctrl和大写键

    打开注册表 [HKEY_LOCAL_MacHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout] "Scancode Map" ...

  10. 【RabbitMQ】ubuntu下RabbitMQ安装与简单使用

    参考:https://blog.csdn.net/the_fool_/article/details/80570971 1.安装: https://blog.csdn.net/a295277302/a ...