#P2770. 航空路线问题

    ID: 1780 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>网络流Special JudgeO2优化网络流 24 题

航空路线问题

Description

Given an airline map in which vertices represent cities and edges represent direct routes between two cities, and no two cities lie on the same meridian. Find a travel route that satisfies the following constraints and visits the maximum number of cities.

  1. Start from the westernmost city, fly strictly from west to east through several cities to the easternmost city, then fly strictly from east to west back to the starting city (possibly via several cities).

  2. Except for the starting city, any city may be visited at most once.

For the given airline map, design an algorithm to find an optimal airline travel itinerary that meets the requirements.

Input Format

The first line contains two integers separated by a space, the number of vertices nn and the number of edges vv.

Lines 22 to n+1n + 1: each line contains a string. The (i+1)(i + 1)-th line gives the name sis_i of the ii-th city from west to east.

Lines n+2n + 2 to n+v+1n + v + 1: each line contains two strings x,yx, y, indicating that there is a direct route between city xx and city yy.

Output Format

This problem uses Special Judge.

First determine whether there exists a route that meets the requirements. If it exists, output one valid itinerary.

If a route exists, output:

  • On the first line, output an integer mm, the maximum number of cities visited.
  • Then output mm lines, one string per line. The ii-th line contains the name of the ii-th city in the itinerary. Note that the 11-st and the mm-th cities are necessarily the starting city.

Otherwise, output a single line containing the string No Solution!.

8 9
Vancouver
Yellowknife
Edmonton
Calgary
Winnipeg
Toronto
Montreal
Halifax
Vancouver Edmonton
Vancouver Calgary
Calgary Winnipeg
Winnipeg Toronto
Toronto Halifax
Montreal Halifax
Edmonton Montreal
Edmonton Yellowknife
Edmonton Calgary
7
Vancouver
Edmonton
Montreal
Halifax
Toronto
Winnipeg
Calgary
Vancouver 

Hint

Constraints and Notes

For 100%100\% of the testdata, it is guaranteed that 1n<1001 \leq n < 100, 1vn×(n1)21 \leq v \leq \frac{n \times (n - 1)}{2}, the length of sis_i does not exceed 1515 and contains only letters and digits, x,yx, y are city names given in the input, and no pair x,yx, y appears more than once.

Translated by ChatGPT 5