题目收集
题目列表题单
返回列表

Waypoints

CodeforcesP0066· 8000ms· 1024 MB· ZJU Summer 2026 Contest 6原题链接
dp多项式随机算法

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

You are given a connected, undirected, unweighted simple graph with nnn vertices and mmm edges (no self-loops, no multiple edges). You are also given kkk distinct waypoint vertices.

A simple cycle is a sequence of distinct vertices in which consecutive vertices are joined by an edge, including the first vertex and the last vertex; its length is its number of vertices. Moreover, we require that every cycle has length at least 3.

Find the minimum possible length of a simple cycle that passes through all kkk waypoints, or report that no such path exists.

输入格式

The first line contains an integer TTT — the number of test cases.

Each test case: the first line has nnn, mmm, kkk (1≤n≤501 \le n \le 501≤n≤50, n−1≤m≤(n2)n-1 \le m \le \binom{n}{2}n−1≤m≤(2n​), 1≤k≤min⁡(n,12)1 \le k \le \min(n,12)1≤k≤min(n,12)). Each of the next mmm lines has an edge uuu vvv (1≤u,v≤n1 \le u,v \le n1≤u,v≤n, u≠vu \ne vu=v); the graph is simple and connected. The last line lists the kkk distinct waypoints.

It is guaranteed that the sum of 2k2^k2k over all test cases does not exceed 2122^{12}212.

输出格式

For each test case, print one line: the minimum number of edges of a simple cycle through all kkk waypoints, or −1-1−1 if none exists.

样例输入 #1

2
4 4 2
1 2
2 3
3 4
1 4
1 4
4 3 3
1 2
1 3
1 4
2 3 4

样例输出 #1

4
-1