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>
#define MOD 1000000007
#define INF 1061109567
#define INFLL 4557430888798830399
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s,"r",stdin);
#define out(s) freopen(s,"w",stdout);
#define fi first
#define se second
#define bw(i,r,l) for (int i=r-1;i>=l;i--)
#define fw(i,l,r) for (int i=l;i<r;i++)
#define fa(i,x) for (auto i:x)
using namespace std;
const int N = 5005;
int n, sz[N], hei[N];
vector<int> g[N];
bool used[N];
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
fw (i, 0, n) {
int k;
cin >> k;
while (k--) {
int x;
cin >> x;
x--;
g[x].pb(i);
}
}
/*
Salary of person u is equal to the number of nodes inside the subtree
Fix root as r. At each step just BFS into all possible choices.
*/
int res = INF;
fw (r, 0, n) {
queue<int> q;
q.push(r);
int lft = n;
memset(used, 0, sizeof used);
used[r] = 1;
hei[r] = 0;
while (!q.empty()) {
lft--;
int u = q.front(); q.pop();
fa (v, g[u]) if (!used[v]) {
used[v] = 1;
q.push(v);
hei[v] = hei[u] + 1;
}
}
int ans = n;
if (!lft) {
fw (i, 0, n) ans += hei[i];
res = min(res, ans);
}
}
cout << res;
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... |