Primitivus
Time limit3sMemory limit128 MB
Given a set of ordered pairs, find the shortest sequence in which every pair appears consecutively at least once.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
The genetic code of the abstract organism Primitivus recurencis is a sequence of natural numbers . A feature of a primitivus is any ordered pair that appears consecutively in the genetic code, that is, there exists an index such that and . A genetic code never contains a feature of the form : no value is ever immediately followed by itself.
You are given a set of features. Write a program that:
- reads the list of features from standard input,
- computes the length of the shortest genetic code that contains all of the given features,
- writes that length to standard output.
Input
The first line contains one positive integer , the number of distinct features. Each of the next lines contains two natural numbers and separated by a single space (, , ), describing one feature . No feature appears more than once.
Output
Print a single integer: the length of the shortest genetic code that contains every feature from the input.
Hint
For the example input, all features appear in this genetic code of length 15:
.