-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11631.cpp
More file actions
executable file
·70 lines (69 loc) · 1.25 KB
/
11631.cpp
File metadata and controls
executable file
·70 lines (69 loc) · 1.25 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
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
int const N=2*1e5+9;
int parent[N],size[N];
int n,e;
pair<long long ,pair<int,int>> adj[N];
void make_set(int v)
{
parent[v]=v;
size[v]=1;
}
int find_set(int v)
{
if(parent[v]==v)return v;
else return parent[v]=find_set(parent[v]);
}
void unite(int x,int y)
{
int a=parent[x];
int b=parent[y];
if(a!=b)
{
if(size[a]<size[b])swap(a,b);
parent[b]=a;
size[a]+=size[b];
}
}
long long kruskal()
{
int x,y;
long long cost,ans=0;
for(int i=0; i<e; i++)
{
x=adj[i].second.first;
y=adj[i].second.second;
cost=adj[i].first;
if(find_set(x)!=find_set(y))
{
ans+=cost;
unite(x,y);
}
}
return ans;
}
int main()
{
vector<int> vec;
while(true)
{
cin>>n>>e;
if(n==0&&e==0)
{
for(int i=0; i<vec.size(); i++)cout<<vec[i]<<endl;
return 0;
}
for(int i=0; i<n; i++)make_set(i);
long long sum=0;
int aa,bb,ww;
for(int i=0; i<e; i++)
{
cin>>aa>>bb>>ww;
sum+=ww;
adj[i]=make_pair(ww,make_pair(aa,bb));
}
sort(adj,adj+e);
//cout<<sum-kruskal()<<endl;
vec.push_back(sum-kruskal());
}
}