#abc352e. E - Clique Connect
E - Clique Connect
Score: points
问题陈述
给定一个加权无向图 ,包含 个顶点,编号为 到 。最初, 没有边。
你将执行 次操作以向 添加边。第 次操作 如下:
- 给定一个顶点子集 ,包含 个顶点。对于每一对 ,满足 且 ,在顶点 和 之间添加一条权重为 的边。
执行完所有 次操作后,确定 是否连通。如果连通,找出 的最小生成树中边的总权重。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given a weighted undirected graph with vertices, numbered to . Initially, has no edges.
You will perform operations to add edges to . The -th operation is as follows:
- You are given a subset of vertices consisting of vertices. For every pair such that and , add an edge between vertices and with weight .
After performing all operations, determine whether is connected. If it is, find the total weight of the edges in a minimum spanning tree of .
Constraints
- $1 \leq A_{i,1} < A_{i,2} < \dots < A_{i,K_i} \leq N$
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If is not connected after all operations, print -1
. If is connected, print the total weight of the edges in a minimum spanning tree of .
Sample Input 1
4 3
3 3
1 2 3
2 2
1 2
3 4
1 3 4
Sample Output 1
9
The left diagram shows after all operations, and the right diagram shows a minimum spanning tree of (the numbers next to the edges indicate their weights).
The total weight of the edges in the minimum spanning tree is .
Sample Input 2
3 2
2 1
1 2
2 1
1 2
Sample Output 2
-1
is not connected even after all operations.
Sample Input 3
10 5
6 158260522
1 3 6 8 9 10
10 877914575
1 2 3 4 5 6 7 8 9 10
4 602436426
2 6 7 9
6 24979445
2 3 4 5 8 10
4 861648772
2 4 8 9
Sample Output 3
1202115217