#include <bits/stdc++.h>
#define int long long
using namespace std;
struct edge
{
int u,v,w;
bool operator<(const edge& x)const
{
return w<x.w;
}
};
vector<edge> graph;
vector<int> f;
int n,m,ans=0;
int find(int u)
{
if (f[u]==u)
{
return u;
}
return f[u]=find(f[u]);
}
void merge(int u,int v)
{
f[u]=find(u);
f[v]=find(v);
if (f[u]!=f[v])
{
f[f[u]]=f[v];
}
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
f.resize(n+1);
for (int i=1;i<=n;i++)
{
f[i]=i;
}
for (int i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
graph.push_back({u,v,w});
}
sort(graph.begin(),graph.end());
int i=1;
for (int j=0;i<n&&j<graph.size();j++)
{
if (find(graph[j].u)==find(graph[j].v))
{
continue;
}
ans+=graph[j].w;
merge(graph[j].u,graph[j].v);
i++;
}
if (i!=n)
{
cout<<"orz";
return 0;
}
cout<<ans;
return 0;
}剪贴板
剪贴板 4rj4ujkr
剪贴板信息
剪贴板信息
- 收录
- 更新