|
| 1 | +/*"Success isn't permanent, failure isn't fatal, |
| 2 | + it's the courage to continue that counts" |
| 3 | +⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ |
| 4 | +⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀ |
| 5 | +⠀ ⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀ |
| 6 | +⠀ ⠀⠀⠀⠀⠀⠀⣸⣿⠁ HIMANSHU⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀ |
| 7 | +⠀ ⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀ |
| 8 | +⠀ ⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀ |
| 9 | +⠀ ⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀ |
| 10 | +⠀ ⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀ |
| 11 | +⠀ ⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀ |
| 12 | +⠀ ⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀ |
| 13 | +⠀ ⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀ |
| 14 | +⠀ ⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀ |
| 15 | +⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ |
| 16 | +⠀ ⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀ |
| 17 | +⠀ ⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀⠀ |
| 18 | +*/ |
| 19 | +#include<bits/stdc++.h> |
| 20 | +using namespace std; |
| 21 | +#define A 1000000007ll |
| 22 | +#define D 100000000000000ll |
| 23 | +#define B 998244353ll |
| 24 | +#define C 1000000000000000000ll |
| 25 | +#define pfin(a) printf("%d\n",a); |
| 26 | +#define pfln(a) printf("%lld\n",a); |
| 27 | +#define pfis(a) printf("%d ",a); |
| 28 | +#define pfls(a) printf("%lld ",a); |
| 29 | +#define sfi(a) scanf("%d",&a); |
| 30 | +#define sfl(a) scanf("%lld",&a); |
| 31 | +#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) |
| 32 | +#define f(i,a,b) for(int i=a;i<b;i++) |
| 33 | +#define pb(a) push_back(a); |
| 34 | +#define mp(a,b) make_pair(a,b) |
| 35 | +#define mxe(v) *max_element(v.begin(),v.end()) |
| 36 | +#define mne(v) *min_element(v.begin(),v.end()) |
| 37 | +#define rev(v) reverse(v.begin(),v.end()) |
| 38 | +#define sor(v) sort(v.begin(),v.end()) |
| 39 | +#define sorrev(v) sort(v.begin(),v.end(),greater<ll>()) |
| 40 | +#define bs binary_search |
| 41 | +#define lb(v,x) lower_bound(v.begin(),v.end(),x)-v.begin() |
| 42 | +#define ub(v,x) upper_bound(v.begin(),v.end(),x)-v.begin() |
| 43 | +#define ll long long |
| 44 | +#define F first |
| 45 | +#define S second |
| 46 | +#define vi vector<ll> |
| 47 | +#define vc vector<char> |
| 48 | +#define endl "\n" |
| 49 | +#define deci fixed<<setprecision(9) |
| 50 | +const ll mod = 1e9 + 9; |
| 51 | +#define N 100005 |
| 52 | +pair<ll, pair<ll, ll>> v1[100005]; |
| 53 | +ll arr[1000005]; |
| 54 | +ll mx1[1000005]; |
| 55 | +ll mn1[1000005]; |
| 56 | +ll size[1000005]; |
| 57 | +void init(ll n) |
| 58 | +{ |
| 59 | + ll i; |
| 60 | + f(i, 1, n + 1) |
| 61 | + { |
| 62 | + arr[i] = i; |
| 63 | + size[i] = 1; |
| 64 | + mx1[i] = i; |
| 65 | + mn1[i] = i; |
| 66 | + } |
| 67 | +} |
| 68 | +ll root(ll x) |
| 69 | +{ |
| 70 | + while (arr[x] != x) |
| 71 | + { |
| 72 | + arr[x] = arr[arr[x]]; |
| 73 | + x = arr[x]; |
| 74 | + } |
| 75 | + return x; |
| 76 | +} |
| 77 | +void uni(ll x, ll y) |
| 78 | +{ |
| 79 | + ll root_x = root(x); |
| 80 | + ll root_y = root(y); |
| 81 | + if (size[root_x] < size[root_y]) |
| 82 | + { |
| 83 | + mx1[root_y] = max(mx1[root_x], mx1[root_y]); |
| 84 | + mn1[root_y] = min(mn1[root_x], mn1[root_y]); |
| 85 | + arr[root_x] = root_y; |
| 86 | + size[root_y] += size[root_x]; |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + mx1[root_x] = max(mx1[root_x], mx1[root_y]); |
| 91 | + mn1[root_x] = min(mn1[root_x], mn1[root_y]); |
| 92 | + arr[root_y] = root_x; |
| 93 | + size[root_x] += size[root_y]; |
| 94 | + } |
| 95 | +} |
| 96 | +bool find(ll x, ll y) |
| 97 | +{ |
| 98 | + if (root(x) == root(y)) |
| 99 | + { |
| 100 | + return true; |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + return false; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +int main() |
| 109 | +{ |
| 110 | +#ifndef ONLINE_JUDGE |
| 111 | + // for getting input from input.txt |
| 112 | + freopen("input.txt", "r", stdin); |
| 113 | + // for writing output to output.txt |
| 114 | + freopen("output.txt", "w", stdout); |
| 115 | +#endif |
| 116 | + ll t; |
| 117 | + sfl(t); |
| 118 | + while (t--) |
| 119 | + { |
| 120 | + ll n, m, i; |
| 121 | + sfl(n); |
| 122 | + sfl(m); |
| 123 | + init(n); |
| 124 | + f(i, 1, m + 1) |
| 125 | + { |
| 126 | + ll x, y, z; |
| 127 | + sfl(x); |
| 128 | + sfl(y); |
| 129 | + sfl(z); |
| 130 | + v1[i] = mp(z, mp(x, y)); |
| 131 | + } |
| 132 | + sort(v1 + 1, v1 + (m + 1)); |
| 133 | + ll max_cost = 0; |
| 134 | + for (i = m; i >= 1; i--) |
| 135 | + { |
| 136 | + ll x1 = v1[i].second.first; |
| 137 | + ll x2 = v1[i].second.second; |
| 138 | + ll wei = v1[i].first; |
| 139 | + if (root(x1) != root(x2)) |
| 140 | + { |
| 141 | + uni(x1, x2); |
| 142 | + max_cost += wei; |
| 143 | + } |
| 144 | + } |
| 145 | + cout << max_cost << endl; |
| 146 | + |
| 147 | + } |
| 148 | +} |
0 commit comments