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 int long long
using namespace std;
const int inf = 1e18 + 18;
const int sz = 5e3 + 15;
vector<int> aj[sz];
bool vi[sz];
int n;
int bfs(int x) {
fill_n(vi, sz, false);
queue<pair<int,int>> q;
q.push(make_pair(1,x));
int r = 0;
int c = 0;
while (!q.empty()) {
auto tp = q.front();
q.pop();
int d = tp.first;
int v = tp.second;
if (vi[v])
continue;
vi[v] = true;
r += d;
c ++;
for (int u : aj[v])
if (!vi[u])
q.push(make_pair(d + 1, u));
}
return (c == n ? r : inf);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i ++) {
int k;
cin >> k;
for (int j = 1; j <= k; j ++) {
int v;
cin >> v;
aj[v].push_back(i);
}
}
int mn = inf;
for (int i = 1; i <= n; i ++) {
int r = bfs(i);
mn = min(mn, r);
}
cout << mn << '\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... |