-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11631.cpp
More file actions
76 lines (73 loc) · 1.42 KB
/
11631.cpp
File metadata and controls
76 lines (73 loc) · 1.42 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
71
72
73
74
75
76
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
struct edge
{
int x,y,w;
bool operator <(const edge &b) const{return b.w>w;}
};
int f(int *parent,int u)
{
while(parent[u]!=-1)
{
u=parent[u];
}
return u;
}
bool uni(int a,int b,int *parent,int *rak)
{
a=f(parent,a);
b=f(parent,b);
if(a==b)
return false;
if(rak[a]>rak[b])
{
parent[b]=a;
rak[a]+=rak[b];
}
else
{
parent[a]=b;
rak[b]+=rak[a];
}
return true;
}
int main()
{
int m,n,*parent,*rak,orig,tmp;
edge input;
vector<edge> graph;
while(cin>>m>>n&&(n||m))
{
orig=0;
tmp=0;
parent=new int[m];
rak=new int[m];
fill_n(parent,m,-1);
fill_n(rak,m,1);
for(int i=0;i<n;i++)
{
cin>>input.x>>input.y>>input.w;
orig+=input.w;
graph.push_back(input);
}
sort(graph.begin(),graph.end());
for(int i=0;i<n;i++)
{
if(uni(graph[i].x,graph[i].y,parent,rak))
{
orig-=graph[i].w;
tmp++;
}
if(tmp>=m-1)
break;
}
graph.clear();
cout<<orig<<endl;
delete []parent;
delete []rak;
}
return 0;
}