-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE.cpp
54 lines (52 loc) · 1.01 KB
/
E.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct edge {
int l, r, cost;
edge (int l, int r, int cost) : l(l), r(r), cost(cost) {}
edge () {}
bool operator < (edge e) const {
return cost > e.cost;
}
};
int par[maxn * 2];
int a[maxn], b[maxn];
int root(int x) {
if(par[x] == x) return x;
return par[x] = root(par[x]);
}
void join(int x, int y) {
x = root(x);
y = root(y);
if(x != y) {
par[x] = y;
}
}
int main() {
int m, n;
scanf("%d %d", &m, &n);
vector <edge> v;
for(int i = 0; i < m; i++) scanf("%d", &a[i]);
for(int i = 0; i < n; i++) scanf("%d", &b[i]);
for(int i = 0; i < m; i++) {
int s;
scanf("%d", &s);
while(s--) {
int x;
scanf("%d", &x);
x -= 1;
v.emplace_back(x, i + n, a[i] + b[x]);
}
}
sort(v.begin(), v.end());
long long ans = 0;
iota(par, par + n + m, 0);
for(auto e : v) {
if(root(e.l) != root(e.r)) {
join(e.l, e.r);
} else {
ans += e.cost;
}
}
printf("%lld\n", ans);
}