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.
#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;
}
}
I need help in understanding what exactly is going on.
For instance when I type 3, enter, 5. It prints 3 5
When I type 3, enter, 9. It prints 3 9
What is happening when I type 5, enter, 9 ?
p = 5
q = 9
How does the program know there is a connection.

New Topic/Question
Reply



MultiQuote





|