-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecial Tree.cpp
84 lines (82 loc) · 1.47 KB
/
Special Tree.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define re(i, n) for(int i = 0; i < n; i++)
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define ren(i,a,b) for(int i = (a); i >= (b); i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
vector<pair<int , pair<int,int> > > v;
int n;
bool checkcycl(vector<int>& cycl , int a , int b)
{
bool t = 0;
if(cycl[a] == -1 && cycl[b] == -1)
{
t = 1;
cycl[a] = a;
cycl[b] = a;
return t;
}
if(cycl[a] == -1 || cycl[b] == -1)
{
t = 1;
if(cycl[a] == -1)
cycl[a] = b;
else
cycl[b] = a;
return t;
}
int ah = a,bh = b;
while(ah != cycl[ah])
ah = cycl[ah];
while(bh != cycl[bh])
bh = cycl[bh];
if(ah == bh)
t = 0;
else
{
cycl[bh] = ah;
t = 1;
}
return t;
}
int spetree()
{
int key = 0;
sort(v.begin(), v.end());
std::vector<int> cycl(n ,-1);
std::vector<bool> visi(n);
int weight = 0;
for(auto node : v)
{
if(checkcycl(cycl ,node.second.first ,node.second.second))
{
key++;
visi[node.second.first] = 1;
visi[node.second.second] = 1;
weight += node.first;
}
if(key == n-1)
break;
}
return weight;
}
int main()
{
int m , a , b , w;
si(n); si(m);
re(i , m)
{
si(a); si(b); si(w);
v.push_back(make_pair(w , make_pair(a-1, b-1)));
}
cout << spetree() << "\n";
return 0;
}