-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisjoint_set_test.cpp
90 lines (83 loc) · 1.75 KB
/
disjoint_set_test.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
#include <algorithm>
#include <numeric>
#include <iterator>
#include <iostream>
#include <cstring>
#include <bitset>
#include <deque>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <unordered_set>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int,int> vii;
typedef vector<long long,long long> vll;
typedef unordered_set<int> uset;
int p[100001],r[100001];
int findU(int m) {
if (p[m] != m){
p[m] = findU(p[m]);
}
return p[m];
}
void link(int x, int y){
if(r[x] > r[y]) {
p[y] = x;
}
else{
p[x] = y;
if(r[x] == r[y]){
r[x]++;
}
}
}
void un(int x, int y) {
link(findU(x),findU(y));
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N,M;
cin >> N >> M;
for(int i = 0; i < N; i++) {
p[i] = i;
r[i] = 0;
}
vector<vector<int>> graph;
for(int i = 0; i < M; i++) {
int a,b;
cin >> a >> b;
vector<int> input;
input.push_back(i+1);
input.push_back(a);
input.push_back(b);
graph.push_back(input);
}
sort(graph.begin(),graph.end());
// for(auto i: graph) {
// cout << i[0] << " " << i[1] << " " << i[2] << endl;
// }
int cnt = 0;
vector<int> out;
for(auto i: graph) {
if(cnt >= N - 1) break;
if(findU(i[1]) != findU(i[2])) {
un(i[1],i[2]);
cnt++;
// cout << i[0] << " " << i[1] << " " << i[2] << endl;
out.push_back(i[0]);
}
}
if(cnt == N-1) {
for(auto i: out) {
cout << i << endl;
}
} else {
cout << "Disconnected Graph" << endl;
}
}