quick-find solution of connectivity problem
👍 g++ program11.cpp && ./a.out
3 4
3 4
4 9
4 9
8 0
8 0
2 3
2 3
5 6
5 6
2 9
5 9
5 9
7 3
7 3
4 8
4 8
5 6
0 2
6 1
6 1
👍 cat program11.in
3 4
4 9
8 0
2 3
5 6
2 9
5 9
7 3
4 8
5 6
0 2
6 1
👍 ./a.out < program11.in
3 4
4 9
8 0
2 3
5 6
5 9
7 3
4 8
6 1
👍 cat program11.cpp
#include <iostream>
using namespace std;
static const int N = 10000;
int main() {
int i, p, q, id[N];
for (i = 0; i < N; i++) id[i] = i;
while (cin >> p >> q) {
int t = id[p];
if (t == id[q]) continue;
for (i = 0; i < N; i++)
if (id[i] == t) id[i] = id[q];
cout << " " << p
<< " " << q << endl;
}
}
ch 1.3 Union-Find Algorithms p11 of