POJ1679 The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 26782 | Accepted: 9598 |
Description
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
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
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的更多相关文章
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- [poj1679]The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28207 Accepted: 10073 ...
- POJ1679 The Unique MST 【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20421 Accepted: 7183 D ...
- 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 ...
- POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27141 Accepted: 9712 D ...
- POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ-1679 The Unique MST,次小生成树模板题
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirec ...
- poj1679 The Unique MST(判定次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23180 Accepted: 8235 D ...
- POJ-1679.The Unique MST.(Prim求次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39561 Accepted: 14444 ...
- POJ1679 The Unique MST(次小生成树)
可以依次枚举MST上的各条边并删去再求最小生成树,如果结果和第一次求的一样,那就是最小生成树不唯一. 用prim算法,时间复杂度O(n^3). #include<cstdio> #incl ...
随机推荐
- php图片压缩-高清晰度
php高清晰度无损压缩 经常会用到把上传的大图片压缩,特别是体积,在微信等APP应用上,也默认都是有压缩的,那么,怎么样对图片大幅度压缩却仍能保持较高的清晰度呢? 压缩通常是有按比例缩放,和指定宽度压 ...
- php 图片操作类,支持生成缩略图,添加水印,上传缩略图
<?php class Image { //类开始 public $originimage = ""; //源图片文件地址 public $image ...
- JS简写
本文来源于多年的 JavaScript 编码技术经验,适合所有正在使用 JavaScript 编程的开发人员阅读. 本文的目的在于帮助大家更加熟练的运用 JavaScript 语言来进行开发工作. 文 ...
- java实时监听日志写入kafka
目的 实时监听某目录下的日志文件,如有新文件切换到新文件,并同步写入kafka,同时记录日志文件的行位置,以应对进程异常退出,能从上次的文件位置开始读取(考虑到效率,这里是每100条记一次,可调整) ...
- Spring---bean的实例化
Spring IoC容器如何实例化Bean呢?传统应用程序可以通过new和反射方式进行实例化Bean.而Spring IoC 容器则需要根据Bean定义里的配置元数据使用反射机制来创建Bean.在Sp ...
- Quartus 11生成pof文件在AS烧写之后,程序无法启动
1. 首先配置成AS,生成.pof文件,选择上面的图标Device 2. 选择Device and Pin Options... 3. 进入配置界面,选择如下 4. 进入下载界面,烧写.pof文件,开 ...
- TerminateProcess
Remarks The TerminateProcess function is used to unconditionally cause a process to exit. The state ...
- PowerShell技巧:使用XPath语法查询XML文件
[TechTarget中国原创] XML是存储结构化数据的一个很好的途径,但是想要让数据在其中发挥作用又会有些困难.每一种语言都有其特定方式来查询XML文件中的命名空间.元素及属性.PowerShel ...
- Jenkins拾遗--第四篇-适当的让构建失败
问题的引出: 有一段我们的前端构建总会现git上分支名称中的版本号和工程里的版本号不一致的问题:这样会导致构一个问题:构建后的产品名称叫做1.1,但是进入app的关于页面,看到的版本还是1.0.这会让 ...
- Pascal数据结构与算法
第一章 数据结构与算法的引入 1.1 数据结构的基本概念 一. 学习数据结构的意义 程序设计 = 数据结构 + 算法 目前,80%的待处理的数据具有“算法简单”(四则运算.检索.排序等),“对象复杂” ...