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 ...
随机推荐
- DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量
DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...
- ZXing.net 生成和解析二维码
nuget引用zxing.net包 public partial class Form1 : Form { public Form1() { InitializeComponent(); } priv ...
- Linux sed命令用法
概述 sed命令是一个面向字符流的非交互式编辑器,不允许用户与它进行交互操作.sed是以行为单位处理文本内容的.在shell中,可以批量修改文本内容. 用法 sed [选项] [动作] 选项与参数:- ...
- C# 后台Http访问 raw from 键值对
using RestSharp;using System;using System.Collections;using System.Collections.Generic;using System. ...
- IDEA中使用单元测试@Test等,提示没有 Junit.jar包
1.File-->Project Structure-->Modules-->右侧Dependencies-->+号-->JARs or directories... 2 ...
- 阿里云Windows远程连接出现身份验证错误,要求的函数不正确”的报错。
最近很多阿里云用户在远程Windows Server的云服务器ECS时出现“身份验证错误,要求的函数不受支持”的报错. 这个问题解决起来非常简单,修改组策略中的一个配置就可以了. 在运行中输入gped ...
- 使用Wamp搭建Php本地开发环境,HBuilder调试
初涉Php,此处做点笔记,希望下次不要能够轻松应对,至少不要在入同一个坑 本文摘要: wamp和HBuilder和Mysql5.7的安装包 Wamp的使用,包括80端口,443端口的占用问题 HBui ...
- 昊合数据整合平台HHDI常见问题
Q: HaoheDI和Informatica PowerCenter.IBM DataStage的区别在哪里? A: Informatica和DataStage是比较重量级的ETL平台,其自身就是比较 ...
- JAVA8 Stream API的使用
/** * @auther hhh * @date 2018/12/31 12:48 * @description Stream流:用来处理数组.集合的API * 1.不是数据结构,没有内部存储(只是 ...
- AtCoder Regular Contest 098 F.Donation
传送门 首先,对于一个点i,进入这个点前必须大于等于Ai,每个点必须捐赠Bi 那么我们可以在每个点最后一次经过的时候再捐赠,这样显然更优 现在我们假设每个点都是最后一次经过的时候捐赠.现在我们把捐赠的 ...