You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.3 KiB

#include <bits/stdc++.h>
using namespace std;
const int N = 2010, M = 1000010;
int n, m;
int d[N];
int st[N];
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
int dfs(int u) {
if (~d[u]) return d[u]; // 记忆化搜索
d[u] = 1; // 最少是一级
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
d[u] = max(d[u], dfs(j) + w[i]);
}
return d[u];
}
int main() {
#ifndef ONLINE_JUDGE
freopen("456.in", "r", stdin);
#endif
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 1; i <= m; i++) {
memset(st, 0, sizeof st);
int k;
cin >> k;
int S = n, T = 1;
for (int j = 1; j <= k; j++) {
int x;
cin >> x;
S = min(S, x);
T = max(T, x);
st[x] = 1;
}
for (int j = S; j <= T; j++)
if (st[j])
add(n + i, j, 1);
else
add(j, n + i, 0);
}
int res = 0;
memset(d, -1, sizeof d);
for (int i = 1; i <= n; i++) res = max(res, dfs(i));
printf("%d\n", res);
return 0;
}