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 ...
随机推荐
- jquery 配合 ajax 完成 在线编辑 你值得拥有
思路分析: 将 table中的表格 改变成为 input表格框获得值 ajax配合修改 删除 <?php use yii\helpers\Url; $web = Url::base(); ?&g ...
- 华为模拟器ensp代码错误2,41,40问题的解决
win8+ensp320 ensp这是个神奇的软件,问题竟然出现的这么莫名其妙..前一秒还是好的时候,后一秒就立马出现了问题.不过不要慌...沉住气,把这篇文章看下去. 博主从昨天开始,ensp神奇的 ...
- 1、spring boot入门
1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,martin fowler 微服务: ...
- 散列--数据结构与算法JavaScript描述(8)
散列 散列是一种常用的数据存储技术,散列后的数据可以快速地插入或取用. 散列使用的数据结构叫做散列表. 在散列表上插入.删除和取用数据都非常快,但是对于查找操作来说却效率低下,比如查找一组数据中的最大 ...
- 菜鸟教程perl总结
数据类型有: 标量$, 数组@,哈希% 数组声明 : @hits = (25, 30, 40); 或者 @sites = qw/google taobao runoob/; 数组操作 pop, ...
- TouTiao开源项目 分析笔记14 段子评论
1.段子页面详情 1.1.先看看预览界面吧 左边的页面已经实现了,现在的目的就是要实现点击左侧的每一个item 然后跳转到右边相应的段子详情页面. 1.2.首先肯定有右侧这个活动==>JokeC ...
- ClassNotFountException 与 NoClassDefineError
一 知识准备 NoClassDefFoundError:正如它们的名字所说明的是一个错误 (Error),而ClassNotFoundException是一个异常.正如上一章节所说Exception和 ...
- ListView, GirldList 等setCurrentItem 不立即刷新
有时候,你调用了adapter的setCurrentItem,然后立马就去获得第一个view的坐标,那么你获取的不是最新的. 要知道程序的并行性.程序运行很快,所以,延迟200s去得到第一个view的 ...
- 【转】android makefile文件分析
Makefile的规则如下: target ... : prerequisites ... command ... ... target可以是一个目标文件,也可以是Object File(例如hell ...
- 是时候用Coffeescript了
CoffeeScirpt是什么? CoffeeScript是一门小巧的语言脚本语言,会编译为JavaScript,并且CoffeeScript产生的JavaScript是可以通过JavaScript ...