Your country has several army units. There are several other countries, each of which is considered to be either an enemy or neutral, and each of which has several army units of its own.
A war consists of a series of battles. Once you initiate war with a neighboring country, war will proceed until either country has no remaining army units. After defeating another country in war, you own their territory. That is, their territory is added to yours forming one new larger territory. You can only initiate war with a country that borders some part of the territory you already own.
In any single round of battle, exactly one of the two countries involved will lose an army unit. If country A attacks country B, where A and B have a and b army units, respectively, then the probability that B will lose an army unit during the battle is a2/(a2+a*b+b2), otherwise A will lose an army unit. Note that a and b correspond to the total number of units in countries A and B, respectively.
Your goal is to defeat all of the enemy nations. While it is not required that you defeat any of the neutral nations, doing so may be necessary in order to reach other enemy countries (see examples 2 and 3).
You are given a String armies, describing each country. Each element of armies is formatted as "type units borders" (quotes added for clarity). type is one of 'Y', 'E' or 'N', indicating if the country is yours, an enemy, or neutral. units is an integer indicating how many army units are held by that country. borders is a space delimited list of zero or more integers, indicating the zero-based indices of adjacent countries.
You are to return a double indicating the probability that you can defeat all of your enemy nations without all of your army units being lost, assuming that you choose your order of attack optimally.