-
Notifications
You must be signed in to change notification settings - Fork 481
/
1135.cpp
32 lines (31 loc) · 837 Bytes
/
1135.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
class Solution
{
public:
int minimumCost(int N, vector<vector<int>>& connections)
{
if (connections.size() < N - 1) return -1;
for (int i = 0; i < N; ++i) parent.push_back(i);
sort(connections.begin(), connections.end(), [](const vector<int>& a, const vector<int>& b) {
return a[2] < b[2];
});
int e = 0, res = 0, k = 0;
while (e < N - 1)
{
auto pre = connections[k++];
int x = find(pre[0] - 1), y = find(pre[1] - 1);
if (x != y)
{
parent[x] = y;
e++;
res += pre[2];
}
}
return res;
}
vector<int> parent;
int find(int x)
{
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
};