luogu#P5234. [JSOI2012] 越狱老虎桥
[JSOI2012] 越狱老虎桥
Background
This is beautiful Nanjing. This is the graceful Jinxiang River. This is the cozy Laohuqiao.
If we say the beauty of the Jinxiang River lies in its pleasant scenery, it is better to say it lies in the relaxed, old-classical-alley style of life in Nanjing. If we say the Jinxiang River is charming because of its simple folk customs, it is better to say it is the secrets buried by history that attract people’s curiosity.
Perhaps many people still remember Laohuqiao Prison, the largest prison in Jiangnan during the Beiyang period. Over nearly a century, through the rise and fall of several eras such as the Late Qing, Beiyang, the Republic of China, and New China, its name changed again and again, showing all its vicissitudes.
People today may find it hard to believe how many thrilling events once took place right here.
Problem 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