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. [转] Maven.pom.xml 配置示例

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  2. 1016 Phone Bills (25 分)

    1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following rul ...

  3. 共享存储(ISCSI网络存储)

    server端:(192.168.100.11) 分区sdb 分别为10G和5G #fdisk /dev/dab …… 同步分区表 #partprobe /dev/sdb # yum install ...

  4. [UE4]计算2点坐标附近的坐标:线性插值法

    float distance = FVector::Distance(SelfLocation, TargetLocation); .f / distance; DrawDebugPoint(GetW ...

  5. servlet的讲解

    https://www.ibm.com/developerworks/cn/java/j-lo-servlet/

  6. Solr Web增加Basic安全性验证

      Solr在5.0版本后,不再提供war包部署的方式,取而代之的是内置了jetty服务,   但是我们发现其中并没有内置任何安全性相关检查,任何人如果知道了我们的外网地址就能直接访问并修改其中的索引 ...

  7. ASP.NET基于Redis的Provider库

    因为session基于本地cache,以前我们自己写分布式缓存,或者数据库存储,或者cookie加密存储,来保存用户状态信息,但较少的直接通过创建一个继承 SessionStateStoreProvi ...

  8. 2018-2019-2 《网络对抗技术》Exp2 后门原理与实践 Week4 20165233

    Exp2 后门原理与实践 实验内容 一.基础问题回答 1.例举你能想到的一个后门进入到你系统中的可能方式? 答:通过访问钓鱼网站,无意下载了一些图片或是文件.而这个图片或文件中携带后门. 2.例举你知 ...

  9. uva-10344

    题意: 枚举23点,注意,数字也是可以枚举的,wa了一次 #include<stdio.h> #include<iostream> #include<sstream> ...

  10. VisualSVN: 只能修改自己提交日志

    上回讲过怎么修改日志信息,这次想提交怎么只能修改自己提交的. 现在演示用户111来修改libra的日志信息 这个公正的SVN出现了 用户111说小样,不让我改,那我修改自己提交的日志总行了吧!! 我改 ...