fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. vector<int> adj[1000];
  5. int dp[1000];
  6. int rec(int i) {
  7. if (dp[i] != -1) return dp[i];
  8. int cur = 1;
  9. for (int j : adj[i]) {
  10. cur = max(cur, 1 + rec(j));
  11. }
  12. return dp[i] = cur;
  13. }
  14. signed main() {
  15. ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  16. int n, k; cin >> n >> k;
  17. vector<vector<int>> a(k, vector<int>(n)), pos(k, vector<int>(n));
  18. for (int i = 0; i < k; i++) {
  19. for (int j = 0; j < n; j++) {
  20. cin >> a[i][j];
  21. a[i][j]--;
  22. pos[i][a[i][j]] = j;
  23. }
  24. }
  25. for (int i = 0; i < n; i++) {
  26. for (int j = 0; j < n; j++) {
  27. int ok = 1;
  28. for (int x = 0; x < k; x++) {
  29. ok &= (pos[x][i] < pos[x][j]);
  30. }
  31. if (ok) {
  32. adj[i].push_back(j);
  33. }
  34. }
  35. }
  36. for (int i = 0; i < n; i++) dp[i] = -1;
  37. int ans = 0;
  38. for (int i = 0; i < n; i++) {
  39. ans = max(ans, rec(i));
  40. }
  41. cout << ans << '\n';
  42. }
Success #stdin #stdout 0.01s 5440KB
stdin
4 
3
1 3 2 4
1 3 2 4
1 4 3 2
stdout
3