-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1234.cpp
More file actions
63 lines (59 loc) · 1.36 KB
/
1234.cpp
File metadata and controls
63 lines (59 loc) · 1.36 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define inf 10000000
#define mem(a, b) memset(a, b, sizeof(a))
#define NN 10010
int pr[NN + 7];
struct edge {
int u, v, w;
};
vector<edge> e;
bool comp(edge n, edge m) { return n.w > m.w; }
int root(int n) {
if (pr[n] == n) return n;
return root(pr[n]);
}
int mst(int n) {
int i, j, k;
for (i = 0; i <= n; i++) pr[i] = i;
sort(e.begin(), e.end(), comp);
int count = 0, sum = 0;
for (i = 0; i < e.size(); i++) {
int u = root(e[i].u);
int v = root(e[i].v);
if (u != v) {
pr[u] = v;
} else
sum += e[i].w;
}
return sum;
}
int main() {
// freopen("C:\\Users\\Maruf Tuhin\\Desktop\\in.txt","r",stdin);
ios_base::sync_with_stdio(false);
int i, j, k, l, n, r, c, u, v, w;
edge ed;
int tc, t = 1, x = -1, m;
cin >> tc;
while (tc--) {
cin >> n >> m;
while (m--) {
cin >> ed.u >> ed.v >> ed.w;
e.push_back(ed);
}
int sum = mst(n);
cout << sum << "\n";
e.clear();
}
cin >> n;
return 0;
}