-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-types-HE.cpp
63 lines (57 loc) · 1.33 KB
/
3-types-HE.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
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long ll;
typedef vector<int> vi;
typedef pair<int , int> ii;
typedef vector< pair<int, ii> > vii;
vii edges;
int n;
int pSet[100010];
ll sum=1;
int findSet(int i){
return pSet[i]==i?i:(pSet[i]=findSet(pSet[i]));
}
void unionSet(int i,int j){
int pari=findSet(i);
int parj=findSet(j);
if(pari!=parj){
pSet[pari]=parj;
}
}
bool isSameSet(int i,int j){
return findSet(i)==findSet(j);
}
bool compare(const pair<int,ii> &a, const pair<int,ii> &b){
return a.second.second<b.second.second;
}
int main(){
int t,m,u,v,w;
sum=0;
edges.clear();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
pSet[i]=i;
}
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d",&u,&v,&w);
edges.push_back(make_pair(u, make_pair(v,w)));
// unionSet(u,w);
// unionSet(v,w);
}
sort(edges.begin(),edges.end(),compare);
for (vector< pair<int,ii> >::iterator i = edges.begin(); i != edges.end(); ++i)
{
printf("Before %d %d %d\n", i->first,i->second.first,i->second.second);
if(!isSameSet(i->first,i->second.first)){
unionSet(i->first,i->second.first);
}
else{
sum++;
printf("deleted %d %d %d\n", i->first,i->second.first,i->second.second);
}
}
printf("%lld\n",sum);
return 0;
}