Problem 2: Money Matters
https://open.kattis.com/problems/moneymatters
We are given that we are dealing with a splintered group of friends, so we can anticipate that this graph is not going to be connected. This is another connected components problem, but with a twist! Each node has a weight - the amount of money a person owes or is owed - and we want each person to be able to exchange this weight with people they are connected to so that everyone ends up with zero money owed.
In other words, the sum of weights over a connected component should equal zero!
So, to solve, you must traverse through all of the connected components, summing up the values at the nodes. If you complete your traversal through the connected component and the sum is anything but zero, their task is impossible.
How do I traverse through Connected Components?
Just use your favorite graph traversal method!
Start from some node, and if there are still unvisited nodes when your B/DFS terminates, there are still connected components for you to explore, so call B/DFS again (on an unvisited node).
Last updated