-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtoi11_place.cpp
51 lines (41 loc) · 832 Bytes
/
toi11_place.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define MOD 1e9 + 7
#define pii pair<int,pair<int,int> >
int n,m;
vector<int> pa;
priority_queue<pii> q;
int root(int x)
{
if(pa[x]==x) return x;
else return pa[x] = root(pa[x]);
}
void kruskal()
{
long long ans = 0;
pa.resize(n);
for(int i = 0;i < n;i++) pa[i] = i;
while(!q.empty())
{
int w = q.top().first;
int x = q.top().second.first,y = q.top().second.second;
q.pop();
if(root(x)==root(y)) continue;
ans+=w;
pa[root(x)] = pa[root(y)];
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i = 0;i < m;i++)
{
int a,b,d;
cin >> a >> b >> d;
q.push({d-1,{a-1,b-1}});
}
kruskal();
}