当前位置:网站首页>AcWing 1142. 繁忙的都市 题解(最小生成树)
AcWing 1142. 繁忙的都市 题解(最小生成树)
2022-07-06 18:00:00 【乔大先生】
AcWing 1142. 繁忙的都市
克鲁斯卡尔算法求最小生成树里最大的边,记录最后加入最小生成树内的边即可
#include<bits/stdc++.h>
using namespace std;
const int N = 8010;
struct Edge{
int a, b, w;
bool operator < (const Edge &t) const{
return w < t.w;
}
}e[N];
int p[N];
int n, m;
int find(int x){
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}
int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i ++ ) p[i] = i;
for(int i = 0; i < m; i ++ ){
int a, b, c;
cin>>a>>b>>c;
e[i] = {
a, b, c};
}
sort(e, e + m);
int res = 0;
for(int i = 0; i < m; i ++ ){
int a = find(e[i].a), b = find(e[i].b), w = e[i].w;
if(a != b){
p[a] = b;
res = w;
}
}
cout<<n - 1<<' '<<res<<endl;
return 0;
}
边栏推荐
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- Transplant DAC chip mcp4725 to nuc980
- Make Jar, Not War
- Docker method to install MySQL
- NEON优化:log10函数的优化案例
- 接收用户输入,身高BMI体重指数检测小业务入门案例
- [case sharing] basic function configuration of network loop detection
- docker 方法安装mysql
- taro3.*中使用 dva 入门级别的哦
- THREE.AxesHelper is not a constructor
猜你喜欢

Comparison of picture beds of free white whoring

Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period

Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP

对C语言数组的再认识

2022 Google CTF SEGFAULT LABYRINTH wp

1123. 最深叶节点的最近公共祖先

从底层结构开始学习FPGA----FIFO IP的定制与测试

The MySQL database in Alibaba cloud was attacked, and finally the data was found

黑马笔记---异常处理

黑马笔记---创建不可变集合与Stream流
随机推荐
docker 方法安装mysql
NEON优化:矩阵转置的指令优化案例
Meet in the middle
LeetCode:1175. 质数排列
让我们,从头到尾,通透网络I/O模型
2022 Google CTF segfault Labyrinth WP
搭建【Redis in CentOS7.x】
Implementation principle of waitgroup in golang
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
黑马笔记---创建不可变集合与Stream流
THREE.AxesHelper is not a constructor
c语言—数组
从底层结构开始学习FPGA----FIFO IP的定制与测试
Dark horse notes - exception handling
Neon Optimization: summary of performance optimization experience
736. Lisp 语法解析 : DFS 模拟题
How to manage distributed teams?
Match VIM from zero (0) -- Introduction to vimscript
What does security capability mean? What are the protection capabilities of different levels of ISO?
HMM 笔记