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.

SashaV | 220 |

VivSisMMM | 216 |

GustavoMéndez | 210 |

GHJKL | 196 |

d_Coder00 | 189 |

Pulkit__Sharma__ | 742 |

Anirban166 | 677 |

Found_me | 598 |

abducodes | 590 |

Sathvik Avasarala | 518 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.