当前位置:网站首页>Minimum spanning tree -- unblocked project hdu1863
Minimum spanning tree -- unblocked project hdu1863
2022-04-23 05:08:00 【FOWng_ lp】

Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
Sample Output
3
?
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
typedef long long LL;
LL ans,t;
int fa[105];
struct N{
int a,b,c;
};
bool cmp(N a, N b){
return a.c < b.c;
}
int find(int x){
if(x!=fa[x]) return find(fa[x]);
else return x;
}
void combine(int a,int b){
if(find(a)!=find(b))fa[find(a)] = find(b);
}
int main(){
int n,m;
N A[105];
while(cin >> n >> m){
if(!n)break;
ans=0,t=1;
for(int i = 1;i <= m;i++)fa[i]=i;
for(int i = 1;i <= n;i++){
cin >> A[i].a >> A[i].b >>A[i].c;
}
sort(A+1,A+1+n,cmp);
for(int i = 1;i <= n;i++){
if(find(A[i].a)!=find(A[i].b)){
combine(A[i].a,A[i].b);
ans+=A[i].c;
}
}
for(int i = 2;i <= m;i++){
if(find(i)!=find(i-1)){
t=0;
break;
}
}
if(t)cout <<ans <<endl;
else cout << "?" <<endl;
}
return 0;
}
版权声明
本文为[FOWng_ lp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220549345393.html
边栏推荐
- 数据安全问题已成隐患,看vivo如何让“用户数据”重新披甲
- Mac 进入mysql终端命令
- SCP command details
- Unique primary key ID of tidb sub table -- solution to failure of sequence and Gorm to obtain primary key
- The WebService interface writes and publishes calls to the WebService interface (2)
- 5 minutes to understand MySQL row column conversion
- Docker installation and mysql5 7 installation
- Discussion on flow restriction
- Uglifyjs compress JS
- 独立站运营 | FaceBook营销神器——聊天机器人ManyChat
猜你喜欢

独立站运营 | FaceBook营销神器——聊天机器人ManyChat

MySQL -- execution process and principle of a statement

Learning Android II from scratch - activity

AQS source code reading

Backup MySQL database with Navicat

DIY is an excel version of subnet calculator

多线程基本概念(并发与并行、线程与进程)和入门案例

#define 定义常量和宏,指针和结构体
![[database] MySQL single table query](/img/27/99d174219109ea7a455cfdf55e0996.png)
[database] MySQL single table query

Sword finger offer: the path with a certain value in the binary tree (backtracking)
随机推荐
Introduction to load balancing
机器学习---线性回归
[2021] Spatio-Temporal Graph Contrastive Learning
Define defines constants and macros, pointers and structures
Unique primary key ID of tidb sub table -- solution to failure of sequence and Gorm to obtain primary key
Agile practice | agile indicators to improve group predictability
Learning Android from scratch -- baseactivity and activitycollector
What are the redis data types
Barcode generation and decoding, QR code generation and decoding
Acid of MySQL transaction
Some experience in using MySQL / tidb database [slowly updating...]
持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
Innovation training (XI) airline ticket crawling company information
Summary of MySQL knowledge points
Redis lost key and bigkey
View, modify and delete [database] table
HRegionServer的详解
Unity C e-learning (IV)
2022/4/22
JS engine loop mechanism: synchronous, asynchronous, event loop