#include <bits/stdc++.h>
#define int long long
using namespace std;
const int base = 1 << 20;
vector<int> rz[base];
vector<int> tree[base];
bool vis[base];
int DP[base];
void dfs(int v, int p){
vis[v] = true;
if(tree[v].size() == 0) DP[v] = 1;
for(auto x: tree[v]){
if(vis[x] == false){
dfs(x, v);
DP[v] += DP[x];
}
}
}
int32_t main(){
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
int k, n; cin >> k >> n;
int nr_vertex = 1;
for(int i = 1; i <= n; i++) {rz[1].push_back(nr_vertex); nr_vertex++;}
for(int i = 2; i <= k; i++){
int amount; cin >> amount;
for(int j = 1; j <= amount; j++){
int x; cin >> x;
if(x != 0) tree[rz[i - 1][x - 1]].push_back(nr_vertex);
rz[i].push_back(nr_vertex);
nr_vertex++;
}
}
for(int i = 1; i <= k; i++){
for(auto x: rz[i]){
if(vis[x] == false) dfs(x, 0);
}
}
int res = 0;
for(int i = 1; i <= k; i++){
int temp_res = 0;
for(auto x: rz[i]) temp_res += DP[x];
res = max(res, temp_res);
}
cout << res << '\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 | #include <bits/stdc++.h> #define int long long using namespace std; const int base = 1 << 20; vector<int> rz[base]; vector<int> tree[base]; bool vis[base]; int DP[base]; void dfs(int v, int p){ vis[v] = true; if(tree[v].size() == 0) DP[v] = 1; for(auto x: tree[v]){ if(vis[x] == false){ dfs(x, v); DP[v] += DP[x]; } } } int32_t main(){ std::ios_base::sync_with_stdio(0); std::cin.tie(0); int k, n; cin >> k >> n; int nr_vertex = 1; for(int i = 1; i <= n; i++) {rz[1].push_back(nr_vertex); nr_vertex++;} for(int i = 2; i <= k; i++){ int amount; cin >> amount; for(int j = 1; j <= amount; j++){ int x; cin >> x; if(x != 0) tree[rz[i - 1][x - 1]].push_back(nr_vertex); rz[i].push_back(nr_vertex); nr_vertex++; } } for(int i = 1; i <= k; i++){ for(auto x: rz[i]){ if(vis[x] == false) dfs(x, 0); } } int res = 0; for(int i = 1; i <= k; i++){ int temp_res = 0; for(auto x: rz[i]) temp_res += DP[x]; res = max(res, temp_res); } cout << res << '\n'; return 0; } |
English