Explanation needed

Hi, I would really appreciate it if someone could explain this source code to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
	}
}


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.
Topic archived. No new replies allowed.