Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26782   Accepted: 9598

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

先跑一遍最小生成树,把树上边都记录下来。

然后枚举不使用其中一条边而再跑最小生成树,若答案没变,说明最小生成树不止一条。

注意数组大小←至少有十道题死在这个问题上了

用n估算的话5000最保险,实际上3000可AC

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int mxn=;
int n,m;
struct edge{
int x,y;
int v;
}e[mxn];
int tot;
int mst[mxn],cnt;
int cmp(const edge a,const edge b){
return a.v<b.v;
}
int fa[mxn];
void init(int x){
for(int i=;i<=x;i++)fa[i]=i;return;
}
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void Kruskal(){ init(n);
int i,j;
cnt=;
int ans1=;
tot=;
for(i=;i<=m;i++){
int x=find(e[i].x);int y=find(e[i].y);
if(x!=y){
fa[x]=y;
ans1+=e[i].v;
mst[++cnt]=i;
tot++;
}
if(tot==n-)break;
}
//
int ans2;
for(int k=;k<=cnt;k++){
tot=; ans2=;
init(n);
//init
for(i=;i<=m;i++){
if(i==mst[k])continue;
int x=find(e[i].x);int y=find(e[i].y);
if(x!=y){
fa[x]=y;
ans2+=e[i].v;
// mst[++cnt]=i;//!!这步不能加! 偷懒从上面复制的结果就是WA记录喜+1
tot++;
}
if((tot==n-) && ans1==ans2){
printf("Not Unique!\n");
return;
}
}
}
printf("%d\n",ans1);
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].v);
sort(e+,e+m+,cmp);
Kruscal();
}
return ;
}

POJ1679 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. POJ1679 The Unique MST 【次小生成树】

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

  4. POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29902   Accepted: 10697 ...

  5. POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

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

  6. POJ1679 The Unique MST —— 次小生成树

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  7. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

  8. poj1679 The Unique MST(判定次小生成树)

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

  9. POJ-1679.The Unique MST.(Prim求次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39561   Accepted: 14444 ...

  10. POJ1679 The Unique MST(次小生成树)

    可以依次枚举MST上的各条边并删去再求最小生成树,如果结果和第一次求的一样,那就是最小生成树不唯一. 用prim算法,时间复杂度O(n^3). #include<cstdio> #incl ...

随机推荐

  1. java中substring()、charAt()、indexOf() (2013-05-05-bd 写的日志迁移

    substring 1. public String substring(int beginIndex)     返回一个新的字符串,它是此字符串的一个子字符串, 该子字符串始于指定索引处的字符,一直 ...

  2. DSP资源分享贴

    DSP资源分享 [2017.5.16 更新] 分享资源共同学习.以前的资源很多人都说用不了了,我会陆续补充,逐步完善.这里不单单分享DSP的,设计基础的,还有其他的电子相关的比较好的资源吧主都和您分享 ...

  3. Pandas 数据结构Series:基本概念及创建

    Series:"一维数组" 1. 和一维数组的区别 # Series 数据结构 # Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象 ...

  4. 一行代码将两个列表拼接出第三个列表(两个可迭代对象相加产生第三个可迭代对象)--map()方法

    map()方法 map(func, *iterables) --> map object lambda方法: lambda  参数 :返回值 a = map(',7]) print(list(a ...

  5. Azure Cloud Service - PaaS

    使用Azure Cloud Service有一段时间了,前阵子在公司内部做一个Cloud Service培训的时候就在想,能不能用一幅图把Cloud Service所涉及的概念都罗列出来.于是就有了下 ...

  6. 十二、mysql之视图,触发器,事务等

    一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...

  7. Altium Designer 快捷键使用整理

    Altium Designer 快捷键 一.原理图部分 1.原理图元件自动编号 原理图中快捷键 T+A 2.原理图与PCB交互设计查找 原理图中选中一个元件跳转到PCB中相应的位置T+S 3.原理图中 ...

  8. Hbase的安装与部署(集群版)

    HBase 部署与使用 部署 Zookeeper 正常部署 $ ~/modules/zookeeper-3.4.5/bin/zkServer.sh start 首先保证 Zookeeper 集群的正常 ...

  9. R语言中的机器学习包

    R语言中的机器学习包   Machine Learning & Statistical Learning (机器学习 & 统计学习)  网址:http://cran.r-project ...

  10. USACO Section1.3 Barn Repair 解题报告

    barn1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...