-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1174.cpp
124 lines (122 loc) · 2.66 KB
/
1174.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdio>
#include <math.h>
#include <vector>
#include <bitset>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
#define PI 3.14159265
#define INF 9999999999
#define EPS 1e-16
#define pb push_back
#define mp make_pair
#define FOR(i,f_limit) for(long long int i=0;i<f_limit;i++)
#define FR(i,s_limit,f_limit) for(long long int i=s_limit;i<f_limit;i++)
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<vi> vv;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
vector<pair<ll,ii> > ed;
vi rnk,p,setsize;
ll numset;
ll unionfind(ll n)
{
n=200001;
rnk.assign(n,0);
setsize.assign(n,1);
p.assign(n,0);
numset=n;
FOR(i,n) p[i]=i;
}
ll findset(ll x)
{
if(p[x]==x) return x;
else return p[x]=findset(p[x]);
}
bool sameset(ll x, ll y)
{
return findset(x)== findset(y);
}
void unionset(ll a, ll b)
{
if(!sameset(a,b))
{
numset--;
ll x=findset(a);
ll y=findset(b);
if(rnk[x]>rnk[y])
{
p[y]=x;
setsize[x]+=setsize[y];
}
else
{
p[x]=y;
setsize[y]+=setsize[x];
if(rnk[x]==rnk[y]) rnk[y]++;
}
}
}
string s1[50001],s2[50001];
ll w[50001];
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t,f=0;
cin>>t;
while(t--)
{
if(f) cout<<endl;
f=1;
ll m;
cin>>m;
ll n;
cin>>n;
map<string,ll> map1;
set<string> set1;
FOR(i,n)
{
cin>>s1[i]>>s2[i]>>w[i];
set1.insert(s1[i]);
set1.insert(s2[i]);
}
ll cnt=0;
for(set<string>::iterator it=set1.begin();it!=set1.end();it++)
{
map1[(*it)]=cnt++;
}
ed.clear();
FOR(i,n)
{
ed.pb(mp(w[i],ii(map1[s1[i]],map1[s2[i]])));
}
sort(ed.begin(),ed.end());
unionfind(m);
ll mst=0;
FOR(i,n)
{
pair<ll,ii> xy=ed[i];
if(!sameset(xy.second.first,xy.second.second))
{
mst+=xy.first;
unionset(xy.second.first,xy.second.second);
}
}
cout<<mst<<endl;
}
return 0;
}