This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
int n, ans = 1e9, t = 0;
vvi g;
int d[5050], q[5050], fi, en;
int go(int v) {
memset(d, -1, sizeof d);
fi = 0;
en = 1;
q[0] = v;
d[v]=1;
while(fi < en) {
int u = q[fi++];
for(auto v : g[u]) {
if(d[v]!=-1) continue;
d[v] = d[u]+1;
q[en++] = v;
}
}
ll ans = 0;
for(int i = 1; i <= n; i++)
if(d[i]==-1) return 1e9;
else ans += d[i];
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
g.resize(n+1);
for(int k, t, i = 1; i <= n; i++) {
cin >> k;
while(k--) cin >> t, g[t].pb(i);
}
for(int i = 1; i <= n; i++) {
ans = min(ans, go(i));
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |