Countries
Time limit1sMemory limit128 MB
For each city, compute which other city exerts the strongest gravity-like influence on it, then label it king, democracy, or trace the chain of surrender to find its ultimate capital.
- Level
Medium4 of 10
- Topics
- Simulation, Math, Implementation
- Solved
- No attempts yet
Problem
This problem explores how mighty countries arise from humble beginnings. Consider a two-dimensional map. On the map there are cities. Each city sits at a distinct location with integer coordinates and holds a garrison of soldiers commanded by a general.
The influence that city exerts on a location equals divided by the squared distance between and — as if the mass of soldiers in city pulled on every surrounding location like gravity.
City is threatened by another city when the influence city exerts on city 's own location is strictly greater than ; then city could send enough soldiers to overpower every defender of city .
- If no other city threatens city , its grateful citizens crown its general as king and make the city the capital of his kingdom.
- If exactly one threatening city exerts strictly more influence on than every other city, city has no choice but to surrender. From then on city obeys the same capital that city obeys (its soldiers do not join anyone's army).
- Otherwise city is saved by the mutual distrust of two or more equally strongest threateners: whichever attacked first would in turn be overpowered by the others. But because its general failed to keep the city safe, the citizens cannot crown him; instead the city becomes the capital of a democracy.
For every city, report which of the three outcomes applies.
Input
The first line contains one integer (), the number of cities. Each of the next lines describes one city: line contains three integers , , () separated by single spaces. All cities occupy distinct locations.
Output
Print lines. Line gives the outcome for city :
Kif city is the capital of a kingdom.Dif city is the capital of a democracy.- Otherwise the index () of the city that city ultimately obeys as its capital, following the chain of surrenders.
Note
Consider the map below, where each dot is a city and its number of soldiers is written above it:

Number the five cities from top to bottom, then left to right. City 3 at is the capital of a kingdom that also contains city 4 at and city 5 at . City 1 at forms a kingdom by itself, and city 2 at forms a democracy by itself.