Isolated Island
Time limit8sMemory limit1024 MB
Given fence segments that split an island into regions, determine whether two neighboring regions need the same minimum number of fish to reach the ocean.
- Level
Medium7 of 10
- Topics
- Geometry, Shortest path, Graph
- Solved
- No attempts yet
Problem
On a small island far far away, a handful of old men live isolated from the rest of the world. The entire island is divided into plots of land by fences, and each old man owns a single plot of land bounded by fences on all sides. (The region outside all fences is the ocean.) Each of the inhabitants needs fish to survive, and the only place where they can fish is the ocean surrounding them. Since not every plot of land is connected to the ocean, some of the men might need to pass through the land of others before being able to fish. The men can cross a single fence at a time, but cannot go through fenceposts or locations where fences intersect.
Unfortunately, the old men are greedy. They demand one fish each time a person wants to enter their land. Since they do not want to lose too much fish to the others, every old man chooses a route that minimizes the number of fish he has to pay to get to the ocean.
Over the years, this has led to rivalry between the old men. Each man hates all other men who have to pay less than him to reach the ocean. Two men only like each other if they have to pay the same amount of fish to reach the ocean.

Figure I.1: Illustrations of the first three sample inputs. In sample input 1, every man has direct access to the ocean, so they all like each other. In sample input 2, no pair of neighbours like each other, because the man in the middle needs to pay one fish, whereas all of his neighbours do not have to pay any fish to reach the ocean. In sample input 3, there are six men, some of whom are friendly neighbours.
The natural question is: are there old men on this island who are neighbours (owning land on opposite sides of a single fence) and like each other? See Figure I.1 for two islands with opposite answers to this question.
Input
The first line contains an integer (), the number of fences. Each of the next lines contains four integers , , , and (, ), describing a straight fence between the fenceposts at and .
Fences may intersect internally, and three or more fences may meet at the same location. Any two fences intersect in at most one point. After crossing a single fence, one always ends up in a different region. All regions together form a single island, and any region can be reached from any other region.
Output
If there is a pair of neighbours who like each other, output "yes". Otherwise, output "no".