Every house in the colony has at most one pipe going into it and at most one pipe going out of it. Tanks and taps are to be installed in a manner such that every house with one outgoing pipe but no incoming pipe gets a tank installed on its roof and every house with only an incoming pipe and no outgoing pipe gets a tap. Find the efficient way for the construction of the network of pipes.

**Input:**

The first line contains an integer T denoting the number of test cases. For each test case, the first line contains two integer **n** & **p **denoting the number of houses and number of pipes respectively. Next, **p **lines contain 3 integer inputs **a, b **&** d, d **denoting the diameter of the pipe from the house **a** to house **b.**

**Output:**

For each test case, the output is the number of pairs of tanks and taps installed i.e **n **followed by **n** lines that contain three integers: house number of tank, house number of tap and the minimum diameter of pipe between them.

**Constraints:**

1<=T<=50

1<=n<=20

1<=p<=50

1<=a,b<=20

1<=d<=100

**Example:
Input:**

1

9 6

7 4 98

5 9 72

4 6 10

2 8 22

9 7 17

3 1 66

3

2 8 22

3 1 66

5 6 10

**Explanation:**

Connected components are ** 3->1, 5->9->7->4->6 and 2->8**.

Therefore, our answer is

Author: Vanshika_pec

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.

cs_abhi | 62 |

kya_bolti_public | 56 |

Shubhankar Sharma | 44 |

vermaankush14291 | 42 |

coderquill | 40 |

PiyushPandey4 | 856 |

john_wick | 757 |

ASWATHAMA | 565 |

akhyasharma01 | 547 |

UsfShilpa | 536 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.