#include #include #include using namespace std; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N, X, Y; cin >> N >> X >> Y; vector> P(X), Q(Y); for (auto &vec : P) { int len; cin >> len; vec.resize(len + 1); for (auto &x: vec) cin >> x, --x; vec.pop_back(); vec.erase(vec.begin()); } for (auto &vec : Q) { int len; cin >> len; vec.resize(len + 1); for (auto &x : vec) cin >> x, --x; vec.pop_back(); vec.erase(vec.begin()); } vector belong_to(N, -1), pos(N, -1); for (int x = 0; x < X; ++x) { const auto &seq = P.at(x); for (int d = 0; d < int(seq.size()); ++d) { belong_to.at(seq.at(d)) = x; pos.at(seq.at(d)) = d; } } vector next_red(N, -1); vector pending; for (const auto &seq : Q) { int pr = -1; for (int x : seq) { if (belong_to.at(x) >= 0) next_red.at(x) = pr, pr = x; } if (pr >= 0) pending.push_back(pr); } vector heights; for (const auto &v : P) heights.push_back(v.size()); while (pending.size()) { int now = pending.back(); pending.pop_back(); if (const int l = belong_to.at(now), p = pos.at(now); heights.at(l) > p) { if (heights.at(l) < int(P.at(l).size())) { int old = P.at(l).at(heights.at(l)); int pr = next_red.at(old); if (pr >= 0) pending.push_back(pr); } heights.at(l) = p; } else { int pr = next_red.at(now); if (pr >= 0) pending.push_back(pr); } } cout << accumulate(heights.cbegin(), heights.cend(), X) << endl; }