#include using namespace std; using bs = bitset<1000>; int main() { int n, k; cin >> n >> k; vector> partitions(k); for (int i = 0; i < k; i++) { int sz; cin >> sz; vector seen(n); for (int j = 0; j < sz; j++) { int x; cin >> x; seen[x - 1] = true; } for (int j = 1; j < n; j++) { if (seen[j] == seen[0]) { partitions[i].push_back(j); } } } sort(partitions.begin(), partitions.end(), [](const auto& x, const auto& y) { return x.size() < y.size(); }); vector nodes; vector has_parent, has_child; nodes.push_back(0); nodes.push_back(0); has_parent.push_back(false); has_parent.push_back(true); has_child.push_back(true); has_child.push_back(false); for (auto elem : partitions[0]) { nodes[0].set(elem, true); } for (int i = 1; i < n; i++) { if (!nodes[0].test(i)) { nodes[1].set(i, true); } } bool valid = true; for (int i = 1; i < k && valid; i++) { int partially_filled = -1; bs curr_state{0}; for (auto elem : partitions[i]) { curr_state.set(elem, true); } for (int j = 0; j < int(nodes.size()); j++) { if ((nodes[j] & curr_state) != nodes[j]) { if (partially_filled == -1) { partially_filled = j; } else { valid = false; break; } } } if (!valid) { break; } if (partially_filled != -1) { if (!has_child[partially_filled] || !has_parent[partially_filled]) { // get all nodes that are in partially_filled but not in curr_state curr_state = nodes[partially_filled] & (~curr_state); nodes[partially_filled] &= ~curr_state; has_child[partially_filled] = true; has_child.push_back(false); has_parent.push_back(true); nodes.push_back(curr_state); } else { if ((nodes[partially_filled] & (~curr_state)) == nodes[partially_filled]) { valid = false; break; } else { has_child.push_back(false); has_parent.push_back(true); curr_state = nodes[partially_filled] & (~curr_state); nodes[partially_filled] &= ~curr_state; nodes.push_back(curr_state); } } } else { // if we hit here, then there exists a partition such that every nodes in the tree belong to the same part, meaning there's an edge pointing to nothing valid = false; break; } } cout << valid << '\n'; }