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 <iostream>
#include <vector>
#include <array>
#include <bitset>
using namespace std;
constexpr int INF = 1e9;
constexpr int MAXN = 5e3;
int best = INF;
int N;
array<short, MAXN> dout;
array<array<short, MAXN>, MAXN> G;
array<short, MAXN> val, q;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> N;
for (int i = 0; i < N; i++) {
int K; cin >> K;
while (K--) {
int u; cin >> u; u--;
G[u][dout[u]++] = i;
}
}
for (int i = 0; i < N; i++) {
int l = 0, r = -1;
bitset<MAXN> vis;
q[++r] = i;
val[i] = 1;
vis[i] = true;
int done = 0, sum = 0;
while (l <= r) {
auto v = q[l++];
sum += val[v];
done++;
for (int j = 0; j < dout[v]; j++) {
int u = G[v][j];
if (!vis[u]) {
q[++r] = u;
val[u] = val[v]+1;
vis[u] = true;
}
}
}
if (done < N) continue;
best = min(best, sum);
}
cout << best << '\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... |