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 int int64_t
#define pb push_back
#define st first
#define nd second
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
vector<int> G[5009];
bool vis[5009];
int d[5009];
int sum;
int cnt;
void bfs(int x) {
queue<int> Q;
Q.push(x);
sum = cnt = 0;
d[x] = 1;
memset(vis, 0, sizeof(vis));
vis[x] = true;
while(sz(Q)) {
int y = Q.front();
Q.pop();
cnt++;
sum += d[y];
for(int z:G[y]) {
if(!vis[z]) {
d[z] = d[y]+1;
vis[z] = true;
Q.push(z);
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for(int i=1;i<=n;i++) {
int k;
cin >> k;
for(int j=0;j<k;j++) {
int x;
cin >> x;
G[x].pb(i);
}
}
int res = 1e18;
for(int i=1;i<=n;i++) {
bfs(i);
if(cnt==n) res = min(res, sum);
}
cout << res << "\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... |