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. MongoDB CPU 利用率高,分析慢请求

    Jemeter 压测过程,发现mongodb的CPU均达到100%,需要查看mongodb的执行情况,使用mongo自带的profiling功能. profiling将请求的执行情况记录在DB下的 s ...

  2. [UE4]C 语言动态数组

    在实际的编程中,往往会发生这种情况,即所需的内存空间取决于实际输入的数据,而无法预先确定.对于这种问题,用静态数组的办法很难解决.为了解决上述问题,C语言提供了一些内存管理函数,这些内存管理函数结合指 ...

  3. innerHTML与jquery里的html()区别介绍

    我原本一直以为innerHTML和jquery里的html其实是完全一样的,jquery是多此一举了,直到我遇到一次问题   看个示例: 复制代码 代码如下: var tbody=document.c ...

  4. Oracle跨库复制表结构

    1.首先建立远程连接 create public database link LINK_SJPSconnect to system identified by manager using '(DESC ...

  5. 提示ORA-01144: File size (13107200 blocks) exceeds maximum of 4194303 blocks 最大4194303 block(转)

    并不是100g的表空间,是100g的数据文件.一般情况下,单个数据文件的最大为32g.解决方法:1.创建多个数据文件,都不能超过32g2.创建大表空间.create bigfile tablespac ...

  6. Solr优化案例分析

    随着umc接入主机的数量越来越多,每天产生的syslog日志数量也在剧增, 之前一天产生的syslog数量才不 到1W,随着整个集团的网络设备不端接入,导致现在每天产生的syslog数量大概在180w ...

  7. 启动 node 文件时附带参数

    cmd: node app.js hello app.js var args = process.argv; console.log(args);//[ 'C:\\Program Files\\nod ...

  8. 中国标准时间改为formatTime格式

    1.toLocaleDateString (根据本地时间把Date 对象的日期部分转换为字符串): var time = new Date(); var formatTime = time.toLoc ...

  9. python学习中的第一个例子

    搭建python 先学习下当小白鼠 1 看下自己的python版本 python -v 2 然后,用pip安装开发Web App需要的第三方库: 异步框架aiohttp: pip3 install a ...

  10. J2SE 8的输入输出--读取/写入文本文件和读取/写入二进制数据

    读取/写入文本文件 // 1. 文本输入 // (1) 短小文本直接转入字符串 String string = new String(Files.readAllBytes(Paths.get(&quo ...