This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
const int maxn = 5e3 + 5;
bool mark[maxn];
int cnt[maxn], H_cnt[maxn], n;
vector<pii > P_pars;
int calc(int root) {
vector<int> height[maxn];
memset(H_cnt, 0, sizeof H_cnt);
memset(mark, false, sizeof mark);
height[0].pb(root);
H_cnt[0] = 1;
mark[root] = true;
ll sum = n, dec = 1;
for (int i = 1; i < n; i++) {
for (int x: height[i - 1]) {
for (pii u: P_pars) {
int v = u.first, par = u.second;
if (par == x and v != x and !mark[v]) {
height[i].pb(v);
H_cnt[i]++;
mark[v] = true;
}
}
}
sum += n - dec;
dec += H_cnt[i];
}
return sum;
}
inline int minn(ll a, ll b) {
if(a <= b)
return a;
return b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int mx = 0;
vector<int> roots;
cin >> n;
for (int i = 1; i <= n; i++) {
int c;
cin >> c;
for (int j = 1; j <= c; j++) {
int x;
cin >> x;
P_pars.pb({i, x});
cnt[x]++;
if (cnt[x] == mx) {
roots.pb(x);
} else if (cnt[x] > mx) {
mx = cnt[x];
roots.clear();
roots.pb(x);
}
}
}
ll ans = 1e18;
for (int r: roots) {
ans = minn(ans, calc(r));
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |