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 ...
随机推荐
- MySQL学习之用户管理
用户权限管理 用户权限管理:在不同的项目中给不同的角色(开发者)不同的操作权限,为了保证数据库数据的安全. 简单点说:有的用户可以访问并修改这个数据,而有些用户只能去查看数据,而不能修改数据.就如同博 ...
- multiprocessing中进程池,线程池的使用
multiprocessing 多进程基本使用 示例代码1 import time import random from multiprocessing import Process def run( ...
- Flask之Flask实例有哪些参数
常用的参数应用实例 from flask import Flask, render_template, url_for, session, request, redirect app = Flask( ...
- php 将富文本编辑后的内容取出
背景:项目中用了富文本编辑器,讲写完的内容存入了数据库,但是取出的时候因为有些展示地方并不需要样式,只想获取到内容,所以需要将带了html编码的信息解析出来. 原始信息如下 [task_desc] = ...
- seleniun 爬取淘宝网
import re from selenium import webdriver from selenium.common.exceptions import TimeoutException fro ...
- Facebook 被指收集用户数据:通过照片和文本
北京时间5月25日消息,在加利福尼亚州进行的对Facebook泄露用户信息一案中,法院对Facebook提起一项新的诉讼,指控该公司通过App收集了用户及他们朋友的信息. 上周向加利福尼亚州圣马特奥市 ...
- 清华大学《C++语言程序设计基础》线上课程笔记04---指针
指针 static int i; static int* ptr = &i; 此处的*表示ptr是指针类型(地址类型),用来存放目标数据的地址 其本身也有地址,所以又指向指针的指针; *前面的 ...
- LeetCode初级算法的Python实现--动态规划
动态规划的本质是递归:所以做题之前一定要会递归:递归式就是状态转移方程:这里将会介绍使用动态规划做题的思维方式. 统一的做题步骤: 1.用递归的方式写出代码:(此方法写的代码在leetcode中一定会 ...
- 宁波Uber优步司机奖励政策(1月11日~1月17日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 6 线程threading
1.第1种方式:threading模块 1)单线程执行 #-*- coding:utf-8 -*- import time def main(): print("我错了...") ...