#P7221. [JSOI2010] 蔬菜庆典
[JSOI2010] 蔬菜庆典
Description
JYY found an island on Mars with buried treasure and took some of it away. Later, the Martians found that JYY had stolen the treasure and arrested him. The Martians plan to eat JYY unless he can win enough Martian coins in the games of the annual Vegetable Festival on Mars to pay for the treasure he took.
The game takes place in the Vegetable Square. First, a huge genetically modified pumpkin is placed in the square. Then various other huge vegetables are dragged in one after another. Together with the big pumpkin, there are vegetables in total. The -th vegetable that is placed will be connected by a rope to some previously placed vegetable. In the Martians' words, vegetable is a Dlihc of vegetable , and vegetable is the Tnerap of vegetable . JYY immediately realizes that the initial big pumpkin has no Tnerap, and every later vegetable has exactly one Tnerap; each vegetable may have one or more Dlihc, or none. After all vegetables are set up in the square, the Martians stick a note on each vegetable. On the note of vegetable is an integer , representing the price of this vegetable.
The games go on one by one. Near the end of the whole party, JYY finally gets to the one that suits him. (You cannot expect JYY, who is afraid of heights, to tightrope-walk among the vegetables, even though that would pay a lot.) The rules are: each time, the player (that is, JYY) may choose any vegetable that has both a Dlihc and a Tnerap, and change its price to , where is the index of vegetable 's Tnerap, and is the index of any one of vegetable 's Dlihc. The Martians give plenty of time, enough for JYY to perform any number of operations. When JYY decides to stop, the game ends. After that, all giant vegetables will be purchased by the Martian government according to the marked prices on them. The money earned from selling the vegetables belongs to JYY, to pay off his debt.
JYY wants to know the maximum total amount he can sell these vegetables for, or whether he can make the total price increase without limit through a sequence of operations. Please help JYY solve this problem.
Input Format
The input contains multiple test cases. Each test case is in the following format.
The first line contains an integer , the number of vegetables in the square.
The next lines each contain two integers . The integers on the -th line represent the index of the Tnerap of vegetable and the price of vegetable .
When , it indicates the end of input.
Output Format
For each test case, output one line. If the total price of the vegetables can be increased without limit, output +inf. Otherwise, output an integer, the maximum possible total price of all vegetables.
5
-1 3
1 2
1 1
3 2
3 2
5
-1 3
1 2
1 1
3 2
3 3
0
13
+inf
Hint
Sample Explanation 1
There are two test cases.
For the first test case, we can only operate on vegetable . Its value can only be or , so the answer is .
For the second test case, we can make the prices of all vegetables increase without limit in the following way:
$$\begin{matrix}1\to 3+3-1=5\\5\to 3+2-5=0\\0\to 3+3-0=6\\6\to 3+2-6=-1\\-1\to 3+3-(-1)=7\\\cdots\end{matrix}$$Constraints
For of the testdata, 。
Translated by ChatGPT 5
京公网安备 11011102002149号