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 <utility>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define ii pair<int, int>
#define ll long long
#define MP(x, y) make_pair(x, y)
const int N = 5e3 + 5;
const ll INF = 1e18;
int n;
vector<int> g[N];
bool vsd[N];
ll dp[N];
vector<int> lst;
ll bfs(int u) {
ll res = 0;
queue<int> q; q.push(u);
memset(vsd, false, sizeof vsd); vsd[u] = true;
memset(dp, 0ll, sizeof dp); dp[u] = 1;
while (q.size()) {
int u = q.front(); q.pop();
res += dp[u];
for (int v : g[u]) {
if (!vsd[v]) {
vsd[v] = true;
q.push(v);
dp[v] = dp[u] + 1;
}
}
}
for (int i = 1; i <= n; i++) if (!vsd[i]) return INF;
return res;
}
int main() {
#ifdef CHUNGDINH
freopen("main.inp", "r", stdin);
#endif // CHUNGDINH
cin >> n;
for (int i = 1; i <= n; i++) {
int k; cin >> k;
while (k--) {
int u; cin >> u;
g[u].push_back(i);
}
}
ll res = INF;
for (int i = 1; i <= n; i++) res = min(res, bfs(i));
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |