Wormholes
InterviewTime limit1sMemory limit128 MB
Count the pairings of N wormholes into pairs so that walking right and teleporting through pairs can loop forever from some start.
- Level
Medium6 of 10
- Topics
- Backtracking, Graph, Simulation
- Solved
- No attempts yet
Problem
Farmer John's weekend hobby of running high-energy physics experiments has backfired, and wormholes (, even) have appeared on his farm. Each wormhole sits at a distinct point of the farm's 2D map.
By John's calculations the wormholes link up into pairs. If wormholes A and B form a pair, an object entering A leaves B moving in the same direction, and an object entering B leaves A moving in the same direction. The consequences can be unpleasant. Suppose wormhole A at and wormhole B at are paired, and Bessie the cow starts at walking in the direction. She enters B, comes out of A, enters B again, and repeats that forever, trapped in an infinite cycle.
John knows where every wormhole is, and he knows Bessie always walks in the direction, but he does not remember where she is right now. Count the pairings of the wormholes for which Bessie could get trapped in an infinite cycle after starting from some unlucky point. Two pairings are different when at least one pair differs.
Input
- The first line contains the number of wormholes, .
- Each of the next lines contains two space-separated integers, the and coordinates of one wormhole. Both coordinates are integers between 0 and 1,000,000,000.
Output
Print the number of pairings for which Bessie could get stuck in a cycle after starting from some point and walking in the direction.
Hint
Take four wormholes at the corners of a square, placed at , , , in that order and numbered 1 to 4. Pair 1 with 2 and 3 with 4, and Bessie gets stuck whenever she starts between and or between and . Pair 1 with 3 and 2 with 4, and the same starting points trap her again. Only when 1 is paired with 4 and 2 with 3 can Bessie walk in the direction from any point of the plane without cycling.