POJ 1679 The Unique MST(最小生成树)
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
Output
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXE = ;
const int MAXN = ; struct Edge {
int from, to, val;
bool operator < (const Edge &rhs) const {
return val < rhs.val;
}
} edge[MAXE]; int fa[MAXN], deg[MAXN];
int n, ecnt; void init() {
ecnt = ;
for(int i = ; i <= n; ++i) {
fa[i] = i;
deg[i] = ;
}
} void add_edge(int u, int v, int c) {
edge[ecnt].from = u;
edge[ecnt].to = v;
edge[ecnt++].val = c;
} int getfather(int x) {
return fa[x] == x ? x : getfather(fa[x]);
} void union_set(int x, int y) {
int a = getfather(x);
int b = getfather(y);
if(a == b) return ;
if(deg[a] <= deg[b]) swap(a, b);
++deg[a]; fa[b] = a;
} int kruskal() {
int sum = ;
int xa, ya;
sort(edge, edge + ecnt);
for(int i = ; i < ecnt; ++i) {
xa = getfather(edge[i].from);
ya = getfather(edge[i].to);
if(xa == ya) continue;
for(int j = i + ; j < ecnt; ++j) {
if(edge[j].val != edge[i].val) break;
if(xa == getfather(edge[j].from) && ya == getfather(edge[j].to)) {
return -;
break;
}
}
union_set(edge[i].from, edge[i].to);
sum += edge[i].val;
}
return sum;
} int main() {
int T, m, a, b, c;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
init();
for(int i = ; i < m; ++i) {
scanf("%d%d%d", &a, &b, &c);
if(a > b) add_edge(b, a, c);
else add_edge(a, b, c);
}
int ans = kruskal();
if(ans < ) printf("Not Unique!\n");
else printf("%d\n", ans);
}
}
POJ 1679 The Unique MST(最小生成树)的更多相关文章
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 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
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ 1679 The Unique MST 推断最小生成树是否唯一
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22715 Accepted: 8055 D ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST 时间限制: 10 Sec 内存限制: 128 MB提交: 25 解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...
随机推荐
- mui的openWindowWithTitle()参数及说明
mui.openWindowWithTitle({ url: 'xxx.html', //String类型,要打开的界面的地址 id: 'id', //String类型,要打开的界面的id style ...
- 正则表达式-Regular expression学习笔记
正则表达式 正则表达式(Regular expression)是一种符号表示法,被用来识别文本模式. 最近在学习正则表达式,今天整理一下其中的一些知识点 grep - 打印匹配行 grep 是个很强大 ...
- linux命令sed与awk是干什么用的,怎么用?
非常强大的文本操纵工具,sed,awk,grep 这个三个命令 都是操作文本文件的unix系统有几个非常命令的特点:1. 对于内核而言,unix文件都是字节序列.io设备也是文件.2. 至于文件的含义 ...
- CSS动画实例
上一篇讲过css动画transform transition的语法,这一节展示自己做的几个小例子加深印象 1. 线条动画效果 代码:最外层div包含2个小的div : a和b. a有左右边框(高度 ...
- vue服务端渲染缓存应用
vue缓存分为页面缓存.组建缓存.接口缓存,这里我主要说到了页面缓存和组建缓存 页面缓存: 在server.js中设置 const LRU = require('lru-cache') const m ...
- Flask中那些特殊的装饰器
模板相关的装饰器 @app.template_global() 用法: @app.template_global() # 记得加括号 def jiafa(a, b): # 这个方法每调用一次就需要传一 ...
- 分清clientY pageY screenY layerY offsetY的区别
分清clientY pageY screenY layerY offsetY的区别 在我们想要做出拖拽这个效果的时候,我们需要分清这几个属性的区别,这几个属性都是计算鼠标点击的偏移值,我们需要对其进行 ...
- HDU 5212 莫比乌斯反演
Code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- R语言爬虫:Rvest包函数介绍(表格)
Rvest 包中常用函数一览: 函数 作用 read_html() 读取 html 页面 html_nodes() 提取所有符合条件的节点 html_node() 返回一个变量长度相等的list,相当 ...
- Python 入门(一)
IDE 个人推荐 Pycharm : 比较好用,虽然没有中文,但是练练英语也不错,毕竟大同小异 基础语法 行与缩进 python最具特色的就是使用缩进来表示代码块,不需要使用大括号 {} . 缩进的 ...