ZOJ Problem Set - 3885
Bob lives in an ancient village, where transactions are done by one item exchange with another. Bob is very clever and he knows what items will become more valuable later on. So, Bob has decided to do some business with villagers.
At first, Bob has N kinds of items indexed from 1 to N, and each item has Ai. There are M ways to exchanges items. For the ith way (Xi, Yi), Bob can exchange one Xith item to one Yith item, vice versa. Now Bob wants that his ith item has exactly Bi, and he wonders what the minimal times of transactions is.
There are multiple test cases.
For each test case output the minimal times of transactions. If Bob could not reach his goal, output -1 instead.
2 1 1 2 2 1 1 2 4 2 1 3 2 1 3 2 2 3 1 2 3 4
Author: FENG, Jingyi
Source: ZOJ Monthly, July 2015