#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <deque>
#include <map>
#include <queue>
#include <climits>
using namespace std;
using ll=long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int K, N;
cin >> K >> N;
vector<vector<int>> V(K);
vector<map<int, int>> mapa(K);
V[0].resize(N);
for(int i=1; i<K; i++){
int k;
cin >> k;
V[i].resize(k);
for(int j=0; j<k; j++){
cin>>V[i][j];
}
}
int wynik = V[K-1].size();
int dodatki = 0;
for(int i=0; i<V[K-1].size(); i++){
if(V[K-1][i]!=0){
if(mapa[K-1].count(V[K-1][i])){
dodatki++;
mapa[K-1][V[K-1][i]]++;
}
else{
mapa[K-1][V[K-1][i]] = 1;
}
}
}
for(int i=K-2; i>=0; i--){
//cout<<dodatki<<'\n';
wynik=max(wynik, int(dodatki+V[i].size()));
dodatki=0;
for(int j=0; j<V[i].size(); j++){
if(V[i][j]!=0){
if(mapa[i].count(V[i][j])){
dodatki+=max(1, mapa[i+1][j+1]);
mapa[i][V[i][j]]+=max(1, mapa[i+1][j+1]);
}
else{
mapa[i][V[i][j]] = max(1, mapa[i+1][j+1]);
dodatki+=max(0, mapa[i+1][j+1]-1);
}
}
}
}
cout<<wynik<<'\n';
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include <iostream> #include <vector> #include <string> #include <algorithm> #include <numeric> #include <cmath> #include <deque> #include <map> #include <queue> #include <climits> using namespace std; using ll=long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); int K, N; cin >> K >> N; vector<vector<int>> V(K); vector<map<int, int>> mapa(K); V[0].resize(N); for(int i=1; i<K; i++){ int k; cin >> k; V[i].resize(k); for(int j=0; j<k; j++){ cin>>V[i][j]; } } int wynik = V[K-1].size(); int dodatki = 0; for(int i=0; i<V[K-1].size(); i++){ if(V[K-1][i]!=0){ if(mapa[K-1].count(V[K-1][i])){ dodatki++; mapa[K-1][V[K-1][i]]++; } else{ mapa[K-1][V[K-1][i]] = 1; } } } for(int i=K-2; i>=0; i--){ //cout<<dodatki<<'\n'; wynik=max(wynik, int(dodatki+V[i].size())); dodatki=0; for(int j=0; j<V[i].size(); j++){ if(V[i][j]!=0){ if(mapa[i].count(V[i][j])){ dodatki+=max(1, mapa[i+1][j+1]); mapa[i][V[i][j]]+=max(1, mapa[i+1][j+1]); } else{ mapa[i][V[i][j]] = max(1, mapa[i+1][j+1]); dodatki+=max(0, mapa[i+1][j+1]-1); } } } } cout<<wynik<<'\n'; return 0; } |
English