#abc210e. E - Ring MST
E - Ring MST
Score : points
问题描述
我们有一个含有 个顶点且无边的无向图。我们将这 个顶点依次称为顶点 , 顶点 , 顶点 , , 顶点 。
考虑对这个图进行 种操作。 对于每一种 ,第 种操作是选择一个整数 使得 ,并添加一条连接顶点 和顶点 的无向边。这里, 表示将 除以 后的余数。执行一次第 种操作需要花费 日元。
你可以以任意顺序执行这 种操作任意次(包括零次)。例如,如果有三种操作可选,你可以选择执行第一种操作两次,第二种操作零次,以及执行第三次操作一次。
确定是否可能使该图变得连通。如果可能的话,找出实现这一目标所需的最小总成本。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
We have an undirected graph with vertices and edges. Let us call the vertices Vertex , Vertex , Vertex , , Vertex .
Consider kinds of operations on this graph.
For each , the operation of the -th kind is to choose an integer such that and add an undirected edge connecting Vertex and Vertex . Here, denotes the remainder when dividing by . It costs yen to do the operation of the -th kind once.
You can do these kinds of operations any number of times (possibly zero) in any order. For example, if three kinds of operations are available, you can choose to do the first operation twice, the second zero times, and the third once.
Determine whether it is possible to make the graph connected. If it is possible, find the minimum total cost needed to do so.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
If it is possible to make the graph connected, print the minimum total cost needed to do so.
If it is impossible to make the graph connected, print .
Sample Input 1
4 2
2 3
3 5
Sample Output 1
11
If we first do the operation of the first kind to connect Vertices and , then do it again to connect Vertices and , and finally the operation of the second kind to connect Vertices and , the graph will be connected. The total cost incurred here is yen, which is the minimum possible.
Sample Input 2
6 1
3 4
Sample Output 2
-1
There is no way to make the graph connected, so we should print .
update @ 2024/3/10 09:25:09