Geeksforgeeks

Timer is Running

Influenced by Social Networking sites, Rahul launches his own site Friendsbook. Each user in Friendsbook is given a unique number, first user being numbered 1. There are N users in Friendsbook numbered from 1 to N. In Friendsbook, i th user can make j th user his friend without becoming his friend himself, i.e. in Friendsbook, there is a one-way link rather than a two-way link as in Facebook. Moreover i th user can make j th user his friend iff i>j. Also one user should have no more and no less than one friend except user 1 who will have no friend. Rahul wants to modify Friendsbook and find out whether one user is somehow linked to some other user. Help Rahul do so.

**Input: **

The first line of input takes the number of test cases, T. Then T test cases follow.Each test case has 2 input lines. The first line of each test case takes the number of users, N. The second line of each test case consists of N - 1 space separated integers. Here, i th integer denotes the friend of (i+1)th user.

**Output:**

Print all possible connections between the users in the following format:

4 2 2 means 4 is linked to 2 via 2 connections.

5 2 3 means 5 is linked to 2 via 3 connections, and so on.

The order of display should be as follows:

Print all possible connections starting from user 2 to user N with other users starting from 1 to The Current User Number - 1. In case one user is not connected at all with another user, that connection should not be printed.

**Constraints:**

1<=T<=200

1<=N<=500

**Example:**

**Input:**

3

3

1 2

3

1 1

4

1 2 2

**Output:**

2 1 1 3 1 2 3 2 1

2 1 1 3 1 1

2 1 1 3 1 2 3 2 1 4 1 2 4 2 1

**Explanation:**

In first test case, 2 is directly linked to 1 and hence 2 is linked to 1 via 1 connection. 3 is directly linked to 2 which in turn is directly linked to 1. Hence 3 is linked to 1 via 2 connections and to 2 via 1 connection.

In the second test case, both 2 and 3 are directly linked to 1. Hence both 2 and 3 are linked to 1 via 1 connection.

In the third test case, 2 is directly linked to 1 and both 3 and 4 are directly linked to 2 and hece connected to 1 via 2 connections. Therefore 2 is connected to 1 via 1 connection, 3 is connected to 1 via 2 connections, 3 is connected to 2 via 1 connection, 4 is connected to 1 via 2 connections and 4 is connected to 2 via 1 connection.

Author: akundusaltlake

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

jainshubham52535 | 529 |

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

jots62 | 351 |

kirtidee18 | 1057 |

ykaushik | 752 |

aggarwalvatsal5 | 741 |

harbilassingh | 576 |

pmr_3689 | 570 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4827 |

Login to report an issue on this page.