loj#P6860. 「ICPC World Finals 2021」蛛网漫游
「ICPC World Finals 2021」蛛网漫游
Description
Charlotte the spider sits at the center of her spiderweb, which consists of a series of silken straight strands that go from the center to the outer boundary of the web. Charlotte's web also has bridges, each of which connects two adjacent strands. The two endpoints of a bridge always have the same distance to the center of the spiderweb.
When Charlotte has finished a late-night feasting in the center and wants to retreat to some corner, she walks to the edge on autopilot. To do this, she picks a starting strand, and walks along it until she meets the first bridge on that strand. She will cross the bridge and go to the other strand, and then keeps walking outwards until she meets another bridge. Then she will cross that bridge, and repeat this process, until there are no more bridges on the current strand, and then she will walk to the end of the current strand. Note that Charlotte must cross all the bridges that she meets. Figure I.1 illustrates one path Charlotte could take.
Charlotte's favorite corner to sleep in during the daytime is at the end of strand . For each possible starting strand, she wants to know the minimum number of bridges to add to the original web in order to end at . Charlotte can add a bridge at any point along the strand, as long as the added bridge does not touch any other bridge. The two endpoints of any added bridge must have the same distance to the center of the spiderweb, and the bridge must connect two adjacent strands.
Input
The first line of input has three integers , , and , where () is the number of strands, () is the number of bridges, and () is Charlotte's favorite strand. Strands are labeled from to in counterclockwise order. Each of the remaining lines contains two integers and describing a bridge, where () is the bridge's distance from the center of the spiderweb and () is the first strand of the bridge in counterclockwise order. Specifically, if , then the bridge connects strands and . If , then the bridge connects strands and . All bridge distances are distinct.
Output
Output lines, where the line is the minimum number of bridges Charlotte needs to add in order to end at strand after walking on autopilot from strand .
7 5 6
2 1
4 3
6 3
8 7
10 5
2
1
1
1
0
1
2
4 4 2
1 1
2 2
3 3
4 4
1
1
0
1