fork download
  1. #include <bits/stdc++.h>
  2. #define C make_pair
  3. #define ll long long
  4. #define all(a) a.begin(),a.end()
  5. #define name "task"
  6. #define ln "\n"
  7. using namespace std;
  8. ll n,m;
  9. void solve(){
  10. cin>>n>>m;
  11. ll a[m+3][n+9];
  12. ll pos[m+3][n+9];
  13. ll dp[n+9];
  14. for(int i=1;i<=m;++i){
  15. for(int j=1;j<=n;++j){
  16. cin>>a[i][j];
  17. pos[i][a[i][j]]=j;
  18. }
  19. }
  20. for(int i=1;i<=n;++i)
  21. dp[i]=1;
  22. for(int i=1;i<=n;++i){
  23. for(int j=i+1;j<=n;++j){
  24. bool check=true;
  25. for(int x=1;x<=m;++x){
  26. if(pos[x][a[1][i]]>pos[x][a[1][j]]){
  27. check=false;
  28. break;
  29. }
  30. }
  31. if(check==true)
  32. dp[j]=max(dp[i]+1,dp[j]);
  33. }
  34. }
  35. ll res=0;
  36. for(int i=1;i<=n;++i)
  37. res=max(res,dp[i]);
  38. cout<<res;
  39. }
  40. int main(){
  41. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  42. if(fopen(name".inp","r")){
  43. freopen(name".inp","r",stdin);
  44. freopen(name".out","w",stdout);
  45. }
  46. solve();
  47. }
  48.  
Success #stdin #stdout 0s 5312KB
stdin
5 3
1 5 3 4 2
1 3 4 2 5
3 1 5 4 2
stdout
3