fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define IOS cin.tie(nullptr)->sync_with_stdio(0),cin.exceptions(cin.failbit);
  4. #define lb(x) (x)&-(x)
  5. #define all(x) (x).begin(),(x).end()
  6. #define ll long long
  7.  
  8. constexpr int maxN=3e2+5;
  9.  
  10. int n,m,val[maxN][maxN],ptr[maxN];
  11.  
  12. bitset<maxN> vis;
  13.  
  14. vector<int> adj[maxN],adj2[maxN];
  15.  
  16. inline bool chk(int guess){
  17. fill(ptr,ptr+n+1,0);
  18. vis.reset();
  19. for(int i = 1;i<=m;i++)adj[i].clear(),adj2[i].clear();
  20. for(int i = 1;i<=n;i++)adj[val[i][0]].emplace_back(i);
  21. for(int flag = 1;flag;){
  22. flag = 0;
  23. for(int j = 1;j<=m;j++)if(adj[j].size()>guess){
  24. flag = 1;
  25. vis[j]=1;
  26. for(int k:adj[j]){
  27. while(ptr[k]<m&&vis[val[k][ptr[k]]])ptr[k]++;
  28. if(ptr[k]==m)return 0;
  29. adj[val[k][ptr[k]]].emplace_back(k);
  30. }
  31. adj[j].clear();
  32. }
  33. }
  34. return 1;
  35. }
  36.  
  37. int main(){
  38. IOS
  39. cin>>n>>m;
  40. for(int i = 1;i<=n;i++)for(int j = 0;j<m;j++)cin>>val[i][j];
  41. for(int i = 1;i<=n;i++)if(chk(i))return cout<<i<<'\n',0;
  42. }
Success #stdin #stdout 0.01s 5280KB
stdin
4 5
5 1 3 4 2
2 5 3 1 4
2 3 1 4 5
2 5 4 3 1
stdout
2