CF1408E.Avoid Rainbow Cycles

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given mm sets of integers A1,A2,…,AmA_1, A_2, \ldots, A_m; elements of these sets are integers between 11 and nn, inclusive.

There are two arrays of positive integers a1,a2,…,ama_1, a_2, \ldots, a_m and b1,b2,…,bnb_1, b_2, \ldots, b_n.

In one operation you can delete an element jj from the set AiA_i and pay ai+bja_i + b_j coins for that.

You can make several (maybe none) operations (some sets can become empty).

After that, you will make an edge-colored undirected graph consisting of nn vertices. For each set AiA_i you will add an edge (x,y)(x, y) with color ii for all x,y∈Aix, y \in A_i and x<yx \lt y. Some pairs of vertices can be connected with more than one edge, but such edges have different colors.

You call a cycle i1→e1→i2→e2→…→ik→ek→i1i_1 \to e_1 \to i_2 \to e_2 \to \ldots \to i_k \to e_k \to i_1 (eje_j is some edge connecting vertices iji_j and ij+1i_{j+1} in this graph) rainbow if all edges on it have different colors.

Find the minimum number of coins you should pay to get a graph without rainbow cycles.

给你 mm 个整数集合 A1,A2,…,AmA_1, A_2, \ldots, A_m;这些集合中的元素均为介于 11 到 nn(含)之间的整数。

另有两个正整数数组 a1,a2,…,ama_1, a_2, \ldots, a_m 和 b1,b2,…,bnb_1, b_2, \ldots, b_n。

一次操作定义为:从集合 AiA_i 中删除一个元素 jj,并为此支付 ai+bja_i + b_j 枚金币。

你可以执行若干次(也可以不执行)此类操作(某些集合可能因此变为空集)。

之后,你将构造一个边染色的无向图,该图包含 nn 个顶点。对于每个集合 AiA_i,你将在所有满足 x,y∈Aix, y \in A_i 且 x<yx \lt y 的顶点对 (x,y)(x, y) 之间添加一条颜色为 ii 的边。某些顶点对之间可能被多条边连接,但这些边的颜色互不相同。

我们称一个环 i1→e1→i2→e2→…→ik→ek→i1i_1 \to e_1 \to i_2 \to e_2 \to \ldots \to i_k \to e_k \to i_1(其中每条边 eje_j 连接顶点 iji_j 和 ij+1i_{j+1},且下标按模 kk 计算)为彩虹环(rainbow cycle),当且仅当该环上所有边的颜色互不相同。

求使最终图中不存在彩虹环所需支付的金币数的最小值。

输入格式

The first line contains two integers mm and nn (1≤m,n≤1051 \leq m, n \leq 10^5), the number of sets and the number of vertices in the graph.

The second line contains mm integers a1,a2,…,ama_1, a_2, \ldots, a_m (1≤ai≤1091 \leq a_i \leq 10^9).

The third line contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤1091 \leq b_i \leq 10^9).

In the each of the next of mm lines there are descriptions of sets. In the ii-th line the first integer sis_i (1≤si≤n1 \leq s_i \leq n) is equal to the size of AiA_i. Then sis_i integers follow: the elements of the set AiA_i. These integers are from 11 to nn and distinct.

It is guaranteed that the sum of sis_i for all 1≤i≤m1 \leq i \leq m does not exceed 2⋅1052 \cdot 10^5.

第一行包含两个整数 mm 和 nn(1≤m,n≤1051 \leq m, n \leq 10^5),分别表示集合的数量和图中顶点的数量。

第二行包含 mm 个整数 a1,a2,…,ama_1, a_2, \ldots, a_m(1≤ai≤1091 \leq a_i \leq 10^9)。

第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤1091 \leq b_i \leq 10^9)。

接下来的 mm 行中,每行描述一个集合。第 ii 行的第一个整数 sis_i(1≤si≤n1 \leq s_i \leq n)表示集合 AiA_i 的大小;随后是 sis_i 个整数:集合 AiA_i 的元素。这些整数均在 11 到 nn 之间,且互不相同。

保证对所有 1≤i≤m1 \leq i \leq m,所有 sis_i 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

Print one integer: the minimum number of coins you should pay for operations to avoid rainbow cycles in the obtained graph.

输出一个整数:为避免所得图中出现彩虹环,你需要支付的最少硬币数量。

输入输出样例

  • 输入#1

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

    输出#1

    11
  • 输入#2

    7 8
    3 6 7 9 10 7 239
    8 1 9 7 10 2 6 239
    3 2 1 3
    2 4 1
    3 1 3 7
    2 4 3
    5 3 4 5 6 7
    2 5 7
    1 8

    输出#2

    66

说明/提示

In the first test, you can make such operations:

  • Delete element 11 from set 11. You should pay a1+b1=5a_1 + b_1 = 5 coins for that.
  • Delete element 11 from set 22. You should pay a2+b1=6a_2 + b_1 = 6 coins for that.

You pay 1111 coins in total. After these operations, the first and the second sets will be equal to 2{2} and the third set will be equal to 1,2{1, 2}.

So, the graph will consist of one edge (1,2)(1, 2) of color 33.

In the second test, you can make such operations:

  • Delete element 11 from set 11. You should pay a1+b1=11a_1 + b_1 = 11 coins for that.
  • Delete element 44 from set 22. You should pay a2+b4=13a_2 + b_4 = 13 coins for that.
  • Delete element 77 from set 33. You should pay a3+b7=13a_3 + b_7 = 13 coins for that.
  • Delete element 44 from set 44. You should pay a4+b4=16a_4 + b_4 = 16 coins for that.
  • Delete element 77 from set 66. You should pay a6+b7=13a_6 + b_7 = 13 coins for that.

You pay 6666 coins in total.

After these operations, the sets will be:

  • 2,3{2, 3};
  • 1{1};
  • 1,3{1, 3};
  • 3{3};
  • 3,4,5,6,7{3, 4, 5, 6, 7};
  • 5{5};
  • 8{8}.

We will get the graph:

There are no rainbow cycles in it.

在第一个测试用例中,你可以执行如下操作:

  • 从集合 11 中删除元素 11。为此你需要支付 a1+b1=5a_1 + b_1 = 5 枚金币。
  • 从集合 22 中删除元素 11。为此你需要支付 a2+b1=6a_2 + b_1 = 6 枚金币。

你总共支付了 1111 枚金币。执行这些操作后,第一个和第二个集合均变为 {2}\{2\},第三个集合变为 {1,2}\{1, 2\}。

因此,所得图包含一条颜色为 33 的边 (1,2)(1, 2)。

在第二个测试用例中,你可以执行如下操作:

  • 从集合 11 中删除元素 11。为此你需要支付 a1+b1=11a_1 + b_1 = 11 枚金币。
  • 从集合 22 中删除元素 44。为此你需要支付 a2+b4=13a_2 + b_4 = 13 枚金币。
  • 从集合 33 中删除元素 77。为此你需要支付 a3+b7=13a_3 + b_7 = 13 枚金币。
  • 从集合 44 中删除元素 44。为此你需要支付 a4+b4=16a_4 + b_4 = 16 枚金币。
  • 从集合 66 中删除元素 77。为此你需要支付 a6+b7=13a_6 + b_7 = 13 枚金币。

你总共支付了 6666 枚金币。

执行这些操作后,各集合变为:

  • {2,3}\{2, 3\};
  • {1}\{1\};
  • {1,3}\{1, 3\};
  • {3}\{3\};
  • {3,4,5,6,7}\{3, 4, 5, 6, 7\};
  • {5}\{5\};
  • {8}\{8\}。

我们得到如下图:

该图中不存在彩虹环。

输入解题思路,AI测评打分。不知道怎么写?

首页