COCI '23 Contest 3 #4 Restorani
View as PDF
Coming to Szeged, Mr. Malnar is, as usual, obliged to get acquainted with the local culture, and thus try all traditional meals, culinary specialties, and local drinks.
We can imagine Szeged as  interesting locations numbered from 
 to 
, connected
by 
 bidirectional roads in such a way that there is a path using roads between
every pair of interesting locations. Amazingly, Mr. Malnar needs exactly one
minute to walk across each road. Time spent walking in an interesting location is
negligible.
Mr. Malnar has a list of  restaurants he would like to visit. It consists of 
 positive integers where the
-th number represents an interesting location near which the 
-th restaurant is.
One problem is that Mr. Malnar must eat an ice cream in a pastry shop right after dining in a restaurant. Another problem is that he refuses to visit the same pastry shop twice.
Luckily, he came prepared as he is familiar with  pastry shops whose locations he remembers as a list of
 positive integers where the 
-th number represents an interesting location near which the 
-th pastry
shop is.
Mr. Malnar is tired from his travel and doesn't want to walk more than he has to, so he asks you to calculate how much will he have to walk and offer the order of visiting restaurants and pastry shops, as he is capable of navigating between them without help.
Mr. Malnar is currently at an interesting location number  and must return to it at the end of his walk.
Input Specification
The first line contains integers  and 
 
, the number of interesting locations and
the number of restaurants/pastry shops, respectively.
The second line contains  integers 
 
, the list of restaurants.
The third line contains  integers 
 
, the list of pastry shops.
In each of the next  lines there are two integers 
 and 
 
 - this means that there is
a road between interesting locations 
 and 
.
Output Specification
In the first line, output , the time in minutes that Mr. Malnar will have to walk to visit all restaurants
and pastry shops, returning to location 
 at the end.
In the second line output  integers 
, the order of visiting restaurants and pastry shops.
The numbers in odd positions represent restaurants and should form a permutation of the first  positive
integers. The numbers in even positions represent pastry shops and should also form a permutation of the
first 
 positive integers.
Visiting locations in given order and returning to the starting position by taking the shortest route between
each should take exactly  minutes.
If there are multiple optimal orders, output any.
Scoring
| Subtask | Points | Constraints | 
|---|---|---|
| 1 | 20 | |
| 2 | 20 | |
| 3 | 30 | |
| 4 | 40 | No additional constraints. | 
If your program, on some test, outputs the first line correct, but doesn't give the correct order in the
second line, it will receive  of points for that test.
The number of points in a subtask corresponds to the least number of points achieved by some test in that subtask.
Sample Input 1
3 1
2
3
1 2
1 3
Sample Output 1
4
1 1
Explanation for Sample 1
Mr. Malnar first has to walk  minute to the only restaurant on location 
, then 
 minutes to the
only pastry shop on location 
 and finally 
 minute back to location 
. Mr. Malnar will walk in total
 minutes.
Sample Input 2
9 4
2 3 4 6
4 5 8 9
1 2
1 3
3 4
3 5
5 6
1 7
7 8
7 9
Sample Output 2
18
3 1 4 2 2 4 1 3
Explanation for Sample 2
Mr. Malnar visits restaurants and pastry shops in this order: restaurant at location  (
 min), pastry
shop at location 
 (
 min), restaurant at location 
 (
 min), pastry shop at location 
 (
 min), restaurant
at location 
 (
 min), pastry shop at location 
 (
 min), restaurant at location 
 (
 min), pastry shop at
location 
 (
 min). After eating an ice cream at a pastry shop on location 
 he returns to location 
 (
 min).
Mr. Malnar walks in total 
 minutes.
Sample Input 3
10 5
3 5 6 7 8
1 2 4 9 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
Sample Output 3
24
4 4 5 5 3 3 2 2 1 1
Explanation for Sample 3
Mr. Malnar visits restaurants and pastry shops in this order: restaurant at location  (
 min), pastry
shop at location 
 (
 min), restaurant at location 
 (
 min), pastry shop at location 
 (
 min), restaurant
at location 
 (
 min), pastry shop at location 
 (
 min), restaurant at location 
 (
 min), pastry shop at
location 
 (
 min), restaurant at location 
 (
 min), pastry shop at location 
 (
 min). After eating his
last ice cream, he is already at location 
 so he doesn't move. Mr. Malnar walks in total 
 minutes.
Comments