#P5234. [JSOI2012] 越狱老虎桥
[JSOI2012] 越狱老虎桥
Description
It was the winter of . A small team from an underground organization in Nanjing decided to raid Laohuqiao Prison to rescue several hundred trapped people. At that time, Laohuqiao Prison was surrounded by layers of power grids, numbered from inside to outside as . The st layer of the power grid was connected to high-voltage electricity. There were high-voltage wires connecting all the grids. The -th wire connected the -th and -th layers, and to destroy the -th wire, at least agents were needed. Facing so many layers of grids, the raiding team was troubled. They must destroy at least one layer of the power grid, otherwise the raid cannot succeed.
However, a cunning spy learned about this. To sabotage the raid plan, the enemy secretly added one more high-voltage wire without letting the team members discover it.
To ensure success, no matter which two layers this newly added secret high-voltage wire connects, the team must destroy exactly one high-voltage wire so that at least one layer of the power grid becomes unpowered. Note that for the newly added wire, we do not know how many agents are needed to destroy it successfully. The question is: what is the minimum number of agents the team must have?
The decisive battle is tonight!
Input Format
The first line contains integers, and , representing the number of grid layers and the number of high-voltage wires.
The next lines each contain integers: , , and .
Output Format
Output only one line containing one integer, which is the minimum number of agents needed.
If the plan is bound to fail, output .
3 2
1 2 1
2 3 2
-1
4 3
1 2 1
1 3 2
1 4 3
3
Hint
For of the testdata, , .
For of the testdata, , .
For of the testdata, , , .
For the second sample, the newly added high-voltage wire can only appear between and , between and , or between and .
If it appears between and , then the only wire that can be destroyed is the one between and . If it appears between and , then the only wire that can be destroyed is the one between and . If it appears between and , then the only wire that can be destroyed is the one between and .
Therefore, at least agents are needed to handle all possible cases.
Translated by ChatGPT 5
京公网安备 11011102002149号