I.O.U.
시간 제한3초메모리 제한1024 MB
친구 사이에 발행된 채무 관계를 방향 그래프로 보고, 각 사이클의 최소 채무액만큼 사이클을 줄여 나가 더 이상 사이클이 남지 않게 만든 뒤 남은 I.O.U.를 출력한다.
문제
You are developing a new app intended to simplify expense-sharing among groups of friends. This app will allow them to keep track of who encountered an expense and how it should be shared with others through the form of I.O.U.s. For instance, if Alice pays for a meal shared with Bob and Carol, and Bob's and Carol's share were 5\, respectively, then Bob would issue an I.O.U. over 5\ to Alice.
Your app will maintain a ledger of who owes whom. Note that cycles can occur: For instance, if Bob initially owes Alice 10\ expense on behalf of Alice, Alice would issue an I.O.U. over 5\ to 52$ people.
Your app will be given a list of I.O.U.s issued and settle them as much as possible by considering all cycles and reducing each debt in a cycle by the minimum amount of debt occurring in the cycle. After all cycles are considered and canceled, your app should output who owes whom how much. If there are multiple ways in which cancelation can occur, you may choose any of them as long as there are no cycles left at the end. However, you may not introduce I.O.U.s between friends that never gave an I.O.U. to each other, e.g., if Alice owes Bob money, and Bob owes the same amount to Carol, you cannot remove Bob from the picture and declare that Alice now owes Carol.
입력
The input consists of a single test case. The first line contains two integers and (), where denotes the number of friends and denotes the number of I.O.U.s issued. Friends are numbered to . This is followed by lines containing three integers , , () denoting an I.O.U. given by friend to friend over dollars. Any friend holds at most one I.O.U. from any friend (), but friend may hold an I.O.U. from friend at the same time that friend holds an I.O.U from .
출력
First, output a single number , denoting the number of I.O.U.s left after canceling all cycles. Then, on the following lines, output the I.O.U.s that are left in the same form in which they appear in the input (e.g. using integers , , denoting that friend owes friend dollars). Do not include any I.O.U.s fully canceled, i.e., all the I.O.U.s you output must have .