当前位置:网站首页>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;
}
边栏推荐
- AI 从代码中自动生成注释文档
- Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
- Today's question -2022/7/4 modify string reference type variables in lambda body
- shell脚本快速统计项目代码行数
- AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
- 7.6模拟赛总结
- C语言实例_2
- 域分析工具BloodHound的使用说明
- THREE. AxesHelper is not a constructor
- Match VIM from zero (0) -- Introduction to vimscript
猜你喜欢
go-zero微服务实战系列(九、极致优化秒杀性能)
系统休眠文件可以删除吗 系统休眠文件怎么删除
405 method not allowed appears when the third party jumps to the website
[advanced C language] 8 written questions of pointer
第三方跳转网站 出现 405 Method Not Allowed
今日问题-2022/7/4 lambda体中修改String引用类型变量
Instructions for using the domain analysis tool bloodhound
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
c语言—数组
How to manage distributed teams?
随机推荐
Yunna | work order management measures, how to carry out work order management
AcWing 361. 观光奶牛 题解(spfa求正环)
域分析工具BloodHound的使用说明
1123. 最深叶节点的最近公共祖先
Add the applet "lazycodeloading": "requiredcomponents" in taro,
Wood extraction in Halcon
Taro 小程序开启wxml代码压缩
[advanced C language] 8 written questions of pointer
c语言—数组
736. LISP syntax parsing: DFS simulation questions
黑马笔记---创建不可变集合与Stream流
使用nodejs完成判断哪些项目打包+发版
swiper组件中使用video导致全屏错位
长按按钮执行函数
Taro2.* 小程序配置分享微信朋友圈
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
HMM notes
table表格设置圆角
交叉验证如何防止过拟合
What are the differences between Oracle Linux and CentOS?