
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 A_{i}. There are M ways to exchanges items. For the ith way (X_{i}, Y_{i}), Bob can exchange one X_{i}th item to one Y_{i}th item, vice versa. Now Bob wants that his ith item has exactly B_{i}, and he wonders what the minimal times of transactions is. Input
There are multiple test cases. OutputFor each test case output the minimal times of transactions. If Bob could not reach his goal, output 1 instead. Sample Input2 1 1 2 2 1 1 2 4 2 1 3 2 1 3 2 2 3 1 2 3 4 Sample Output1 1 Author: FENG, Jingyi Source: ZOJ Monthly, July 2015 