Codeforces Beta Round 83 (Div. 2 Only)
C
Dorm Water Supply
Codeforces
未评级
原题
← BD →
翻译中…

The German University in Cairo (GUC) dorm houses are numbered from $1$ to $*n*$. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).

翻译中…

For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.

翻译中…

In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.

翻译中…

Input

翻译中…

The first line contains two space-separated integers $*n*$ and $*p*$ $(1 ≤ *n* ≤ 1000, 0 ≤ *p* ≤ *n*)$ — the number of houses and the number of pipes correspondingly.

翻译中…

Then $*p*$ lines follow — the description of $*p*$ pipes. The $*i*$-th line contains three integers $*a*_{*i*}$ $*b*_{*i*}$ $*d*_{*i*}$, indicating a pipe of diameter $*d*_{*i*}$ going from house $*a*_{*i*}$ to house $*b*_{*i*}$ ($1 ≤ *a*_{*i*}, *b*_{*i*} ≤ *n*, *a*_{*i*} ≠ *b*_{*i*}, 1 ≤ *d*_{*i*} ≤ 10^{6}$).

翻译中…

It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.

翻译中…

Output

翻译中…

Print integer $*t*$ in the first line — the number of tank-tap pairs of houses.

翻译中…

For the next $*t*$ lines, print 3 integers per line, separated by spaces: $*tank*_{*i*}$, $*tap*_{*i*}$, and $*diameter*_{*i*}$, where $*tank*_{*i*} ≠ *tap*_{*i*}$ ($1 ≤ *i* ≤ *t*$). Here $*tank*_{*i*}$ and $*tap*_{*i*}$ are indexes of tank and tap houses respectively, and $*diameter*_{*i*}$ is the maximum amount of water that can be conveyed. All the $*t*$ lines should be ordered (increasingly) by $*tank*_{*i*}$.

翻译中…

样例

Input #1
3 2
1 2 10
2 3 20
Output #1
1
1 3 10
InputCopy OutputCopy
Input #2
3 3
1 2 20
2 3 10
3 1 5
Output #2
0
InputCopy OutputCopy
Input #3
4 2
1 2 60
3 4 50
Output #3
2
1 2 60
3 4 50
InputCopy OutputCopy
题目信息
题号
108C
时间限制
1 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。