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;
const int MAXN = 5005;
int n, ans;
int H[MAXN];
vector <int> Adj[MAXN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ans = MAXN * MAXN;
cin >> n;
for(int i = 1; i <= n; i++)
{
int s;
cin >> s;
while(s--)
{
int a;
cin >> a;
Adj[a].push_back(i);
}
}
for(int i = 1; i <= n; i++)
{
int tmp = 0, cnt = 0;
queue <int> BFS;
for(int j = 1; j <= n; j++)
{
H[j] = -1;
}
H[i] = 0;
BFS.push(i);
while(BFS.empty() == false)
{
int node = BFS.front();
tmp += (H[node] + 1);
cnt++;
BFS.pop();
for(auto x : Adj[node])
{
if(H[x] == -1)
{
H[x] = H[node] + 1;
BFS.push(x);
}
}
}
if(cnt == n)
{
ans = min(ans, tmp);
}
}
cout << ans << '\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... |