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 F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9 + 50;
const int N = 5e3 + 50;
int n, d[N];
vector<int> adj[N];
void solve(){
cin >> n;
for (int u = 1; u <= n; u++) {
int k;
cin >> k;
for (int i = 1; i <= k; i++) {
int v;
cin >> v;
adj[v].push_back(u);
}
}
ll ans = inf;
queue<int> q;
for (int s = 1; s <= n; s++) {
for (int u = 1; u <= n; u++) d[u] = inf;
d[s] = 1;
q.push(s);
while (q.size()) {
int u = q.front();
q.pop();
for (int v:adj[u]) {
if (d[u]+1 < d[v]) {
d[v] = d[u]+1;
q.push(v);
}
}
}
ll sm = 0;
for (int u = 1; u <= n; u++) {
sm += d[u];
}
ans = min(ans, sm);
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |