Jurassic Remains
Time limit1sMemory limit128 MB
Each bone is a set of letters; choose the largest subset such that every letter appearing in it occurs in at least two of the chosen bones.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Brute force, Math, Combinatorics
- Solved
- No attempts yet
Problem
Paleontologists in Siberia recently unearthed many fragments of a Jurassic-period dinosaur skeleton and want to ship them to a paleontology museum. The dinosaur was so huge that no available box could hold the fragments, so the team detached the skeleton into separate bones to be reassembled at the museum. To make reassembly easier, every joint where two bones had been connected was tagged with a label.
While packing, several additional loose bones were discovered and added to the same package before it was shipped.
When the package arrived, two complications surfaced:
- The labels are not all distinct. Only the capital letters
AtoZare used. Two joints that must be connected always share the same letter, but several different pairs of joints may carry that same letter. - The same labels were also used to tag the extra loose bones, so some labeled joints do not need to be connected to anything. Fortunately, on any single bone each letter appears on at most one joint.
Help the museum reconstruct a skeleton fragment. Choose a set of bones that can be joined so that all of the following hold:
- Two joints may be connected only if they carry the same label.
- For every chosen bone, each of its labeled joints is connected to some other joint.
- The number of chosen bones is as large as possible.
Two bones may be joined through several joints at once.
Input
The first line contains an integer --- the number of bones (). Each of the next lines contains one bone: a non-empty string of distinct capital letters listing the labels on that bone's joints.
Output
Print a single integer --- the largest number of bones that can be used together to reassemble a skeleton fragment. If no bones can be used, print .