提交时间:2022-07-19 12:04:50
运行 ID: 52385
#include <bits/stdc++.h> using namespace std; const int MXV = 12405; const int MXE = 376805; const int INF = 0x3f3f3f3f; int depth[MXV]; int N, M, S, T; int head[MXV], cap[MXE], to[MXE], nxt[MXE], egsz; void addedge(int f, int t, int c) { cap[egsz] = c, to[egsz] = t, nxt[egsz] = head[f], head[f] = egsz++; cap[egsz] = 0, to[egsz] = f, nxt[egsz] = head[t], head[t] = egsz++; } bool bfs() { fill(depth, depth + N + M + 2, INF); queue<int> q; depth[S] = 0; q.push(S); while (!q.empty()) { int c(q.front()); q.pop(); for (int i(head[c]); ~i; i = nxt[i]) { if (cap[i] && depth[to[i]] == INF) { depth[to[i]] = depth[c] + 1; q.push(to[i]); } } } return depth[T] != INF; } int dfs(int p, int f) { if (p == T) return f; if (!f) return 0; int u(0); for (int i(head[p]); ~i && f; i = nxt[i]) { if (!cap[i] || depth[to[i]] != depth[p] + 1) continue; int t(dfs(to[i], min(f, cap[i]))); cap[i] -= t, cap[i ^ 1] += t; u += t, f -= t; } return u; } int main() { // freopen("subnet.in", "r", stdin); // freopen("subnet.out", "w", stdout); cin >> N >> M; S = 0, T = N + M + 1; fill(head, head + N + M + 2, -1); for (int i(1); i <= M; ++i) addedge(S, i, 2); for (int i(M + 1); i <= M + N; ++i) addedge(i, T, 1); for (int i(1), k(0); i <= M; ++i) { cin >> k; for (int j(0), x(0); j != k; ++j) { cin >> x; if (k != 1) addedge(i, M + x, 1); } } int mxf(0); while (bfs()) mxf += dfs(S, INF); int cnt(0); for (int i(0); i != (M << 1); i += 2) { if (!cap[i]) ++cnt; } cout << cnt << endl; return 0; }