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. BeanFactory和IOC控制反转

    之前在看spring,看IOC实在是云里雾里,包括看AOP也是云里雾里的,后来重新学习Java Web,做了一个简单的web项目,再之后看了崔希凡老师的视频,Day27和Day28两天的内容,真的很有 ...

  2. google云函数实现BigQuery数据操作

    Google Cloud Function操作BigQuery数据库. 1.部署云函数时在配置文件中(package.json)添加一项 "@google-cloud/bigquery&qu ...

  3. 用php读取xml数据

    parser是php内置的一个用来处理xml的解析器,它的工作由三个事件组成:起始标签. 读取数据.结束标签. 也就是说在对xml进行处理的时候每当遇到起始标签.数据和结束标签的时候函数会做相应的动作 ...

  4. html5的canvas绘制线条,moveTo和lineTo详解

    今天在看html5,里面新增的属性有一个canvas,它相当于一个画布你可以用js在里面画你想要的效果!我在w3c的手册里面看到用moveTo和lineTo绘制线条讲的不是很清楚,尤其是moveTo和 ...

  5. Aizu:0009- Prime Number

    Prime Number Time limit 1000 ms Memory limit 131072 kB Problem Description Write a program which rea ...

  6. 9,K-近邻算法(KNN)

    导引: 如何进行电影分类 众所周知,电影可以按照题材分类,然而题材本身是如何定义的?由谁来判定某部电影属于哪 个题材?也就是说同一题材的电影具有哪些公共特征?这些都是在进行电影分类时必须要考虑的问 题 ...

  7. 5,版本控制git --标签管理

    打标签 像其他版本控制系统(VCS)一样,Git 可以给历史中的某一个提交打上标签,以示重要. 比较有代表性的是人们会使用这个功能来标记发布结点(v1.0 等等). 在本节中,你将会学习如何列出已有的 ...

  8. ThinkPad 触控板双指不可以滑动

    我一直在想为什么,今天我想禁用触摸板的时候,我找到原因了. 是因为没有装驱动. http://think.lenovo.com.cn/support/driver/newdriversdownlist ...

  9. Linux下单机安装部署kafka及代码实现

    技术交流群:233513714 这几天研究了kafka的安装及使用,在网上找了很多教程但是均以失败告终,直到最后想起网络方面的问题最终才安装部署成功,下面就介绍一下kafka的安装部署及代码实现 一. ...

  10. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

    花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用oracle用 ...