#include <iostream>
usingnamespace std;
staticconstint 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;
}
}
It's supposed to read a sequence of pairs and then print out the pairs that are not connected.
For instance, if I input : 2,9
it would output: 2 9
If I input afterward: 2,5
it would output: 2 5
however, if I input: 5 2
there would be no output because it is implied that 5 is connected to 9. Therefore no new connection is made.