「CH6201」走廊泼水节

传送门

考虑 \(\text{Kruskal}\) 的过程以及用到一个最小生成树的性质即可。

在联通两个联通块时,我们肯定会选择最小的一条边来连接这两个联通块,那么这两个联通块之间的其他边都必须比这条边长,不然最小生成树就不唯一。

又为了让答案最小化,我们就只需要让这些其他边比当前边多 \(1\) 就好了。

参考代码:

#include <algorithm>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
} const int _ = 6010;
typedef long long LL; int n, Fa[_], siz[_];
struct node { int x, y, z; } t[_];
inline bool cmp(const node& a, const node& b) { return a.z < b.z; } inline int Find(int x) { return Fa[x] == x ? x : Fa[x] = Find(Fa[x]); } inline void solve() {
read(n);
for (rg int i = 1; i <= n; ++i) Fa[i] = i, siz[i] = 1;
for (rg int x, y, z, i = 1; i < n; ++i)
read(x), read(y), read(z), t[i] = (node) { x, y, z };
sort(t + 1, t + n, cmp);
LL ans = 0;
for (rg int i = 1; i < n; ++i) {
int x = Find(t[i].x), y = Find(t[i].y);
if (x == y) continue;
ans += 1ll * (t[i].z + 1) * (siz[x] * siz[y] - 1);
Fa[x] = y, siz[y] += siz[x];
}
printf("%lld\n", ans);
} int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
int T; read(T);
while (T--) solve();
return 0;
}

「CH6201」走廊泼水节的更多相关文章

  1. 【CH6201】走廊泼水节

    题目大意:给定一棵树,要求增加若干条边,将其转化为完全图,且该完全图以该树为唯一的最小生成树,求增加的边权最小是多少. 题解:完全图的问题一般要考虑组合计数.重新跑一遍克鲁斯卡尔算法,每次并查集在合并 ...

  2. CH6201 走廊泼水节【最小生成树】

    6201 走廊泼水节 0x60「图论」例题 描述 [简化版题意]给定一棵N个节点的树,要求增加若干条边,把这棵树扩充为完全图,并满足图的唯一最小生成树仍然是这棵树.求增加的边的权值总和最小是多少. 我 ...

  3. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  4. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  5. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  6. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  7. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  8. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  9. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

随机推荐

  1. Django框架之ORM常用字段

    一.ORM介绍 1.ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过 ...

  2. 【转载】巴塞尔问题(Basel Problem)的多种解法

    如何计算 \(\displaystyle \zeta \left ( 2 \right )=\frac{1}{1^{2}}+\frac{1}{2^{2}}+\frac{1}{3^{2}}+\cdots ...

  3. 吴裕雄 PYTHON 人工智能——基于MASK_RCNN目标检测(5)

    import os import sys import numpy as np import tensorflow as tf import matplotlib import matplotlib. ...

  4. Jmeter_请求原件之参数化CSV

    1.用途:注册10个账户 2.用CSV 制造数据相对比TEXT更方便 3.创建CSV 文件,注册账户和密码如下 4.Jmeter设置如下 因为是注册10个账户,要运行10次 5.线程组->添加- ...

  5. mysql学习笔记(三):unsigned理解以及特殊情况

    UNSIGNED UNSIGNED属性就是将数字类型无符号化,与C.C++这些程序语言中的unsigned含义相同.例如,INT的类型范围是-2 147 483 648 - 2 147 483 647 ...

  6. java比较时间的方法

    一.通过compareTo Date date = new Date(1576118709574L); Date date1 = new Date(1576118709574L); Date date ...

  7. list的使用-Hdu 1276

    士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  8. 操作系统OS - 进程的状态切换

  9. Plastic Bottle Manufacturer - Consumer Demand For Plastic Bottles Becomes Higher

    Since transparent containers enable consumers to clearly see the contents, consumers are increasingl ...

  10. 【SSM】Log4j 日志配置

    1.log4j.properties ### 配置根 ### # log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROL ...