当前位置:网站首页>AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
2022-07-07 01:36:00 【Mr. Qiao Da】
AcWing 346. Corridor Water Splashing Festival
The key to solving the problem is to deduce the formula :**(Size[a] * Size[b] - 1) * (w + 1)** The meaning of this formula is mortgage : Traverse to two points belonging to two different connected blocks connected by a certain edge , Connect all points of the two connecting blocks to each other , Subtract an existing edge , After introducing the formula , Traverse all edges according to the algorithm of minimum spanning tree , Find out two points that are not in a connected block and accumulate the calculated value with a formula
#include<bits/stdc++.h>
using namespace std;
const int N = 6010;
struct Edge{
int a, b, w;
bool operator< (const Edge &t) const {
return w < t.w;
}
}e[N * 4];
int T, n, m;
int p[N];
int Size[N];
int find(int x){
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}
int main()
{
cin>>T;
while(T -- ){
cin>>n;
int res = 0;
for(int i = 0; i < n - 1; i ++ ){
int a, b, w;
cin>>a>>b>>w;
e[i] = {
a, b, w};
}
// Initialize and query array
for(int i = 1; i <= n; i ++ ){
p[i] = i;
Size[i] = 1;
}
sort(e, e + n - 1);
for(int i = 0; i < n - 1; i ++ ){
int a = find(e[i].a), b = find(e[i].b), w = e[i].w;
if(a != b){
res += (Size[a] * Size[b] - 1) * (w + 1); // The formula , Connect all points of the two connecting blocks to each other , Subtract an existing edge
p[a] = b;
Size[b] += Size[a];
}
}
cout<<res<<endl;
}
return 0;
}
边栏推荐
- Meet in the middle
- 设置Wordpress伪静态连接(无宝塔)
- AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
- Instructions for using the domain analysis tool bloodhound
- THREE. AxesHelper is not a constructor
- According to the analysis of the Internet industry in 2022, how to choose a suitable position?
- C language instance_ three
- What are the differences between Oracle Linux and CentOS?
- AcWing 1140. 最短网络 (最小生成树)
- Taro applet enables wxml code compression
猜你喜欢
Transformation transformation operator
Let's see through the network i/o model from beginning to end
AcWing 361. 观光奶牛 题解(spfa求正环)
Make Jar, Not War
LeetCode:1175. Prime permutation
鼠标右键 自定义
今日问题-2022/7/4 lambda体中修改String引用类型变量
Comparison of picture beds of free white whoring
go-zero微服务实战系列(九、极致优化秒杀性能)
子网划分、构造超网 典型题
随机推荐
dvajs的基础介绍及使用
Taro2.* applet configuration sharing wechat circle of friends
mysqlbackup 还原特定的表
Wood extraction in Halcon
从底层结构开始学习FPGA----FIFO IP的定制与测试
Install Firefox browser on raspberry pie /arm device
shell脚本快速统计项目代码行数
tansig和logsig的差异,为什么BP喜欢用tansig
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
Appium基础 — Appium Inspector定位工具(一)
Gazebo的安装&与ROS的连接
Spark TPCDS Data Gen
The difference between Tansig and logsig. Why does BP like to use Tansig
Neon Optimization: performance optimization FAQ QA
C语言实例_2
mongodb查看表是否导入成功
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
uva 1401 dp+Trie