TheCitiesAndRoadsDivOne
SRM 460 · 2009-11-12 · by Vasyl[alphacom]
Problem Statement
John and Brus have become very famous people all over the world, especially in Bolivia. Once they decided to visit their fan club in Bolivia. John has an old map of Bolivia which shows all of its cities and the roads connecting them. All roads are bidirectional, meaning they can be traversed in both directions. Since the map is old, it's possible that some additional roads have been built since the map was produced. However, roads are never destroyed in Bolivia, so all the roads on the map still exist.
Brus has discovered on the Internet that each pair of Bolivian cities now has at least one and at most two simple paths connecting them. A path between cities A and B is a sequence of cities starting with A and ending with B such that there's a road between each pair of consecutive cities in the sequence. The path is considered simple if it consists of distinct cities.
John and Brus have decided to add some new roads to the old map in such a way that the resulting map satisfies this condition. They can only add a road between a pair of cities if that road did not already exist in the old map. They can't add more than one road between the same pair of cities, and they can't add a road that leads from a city to itself. All added roads must be bidirectional.
You are given a
Constraints
- map will contain between 1 and 20 elements, inclusive.
- Each element of map will contain exactly n characters, where n is the number of elements in map.
- Each character of map will be either 'Y' of 'N'.
- The j-th character of the i-th element of map will be equal to the j-th character of the i-th element of map.
- The i-th character of the i-th element of map will be 'N'.
- There will be at least one way for John and Brus to add new roads to the old map.
{"NN",
"NN"}
Returns: 1
There is only one way - connect the cities with a single road.
{"NNY",
"NNN",
"YNN"}
Returns: 3
There are three possible ways - connect the second city with the first one, with the third one or with both.
{"NY",
"YN"}
Returns: 1
Adding no roads is a valid possible way.
{"NYNN",
"YNNN",
"NNNY",
"NNYN"}
Returns: 10
{"NNNNNNNNNN", "NNNNNNNNNN", "NNNNNNNYNN", "NNNNNNNYNN", "NNNNNNNNNN", "NNNNNNNNNN", "NNNNNNNNNN", "NNYYNNNNNN", "NNNNNNNNNY", "NNNNNNNNYN"}
Returns: 8127840
Submissions are judged against all 61 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TheCitiesAndRoadsDivOne with a public method int find(vector<string> map) · 61 test cases · 2 s / 256 MB per case