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 ll long long
using namespace std;
const int MAXN = 5005;
const int INF = 1e9;
vector<int> adj[MAXN];
int N;
ll bfs(int src){
vector<int> depth(N+1,INF);
queue<int> q;
q.push(src); depth[src] = 1;
while(!q.empty()){
int u = q.front(), d = depth[u]; q.pop();
for(auto v : adj[u]){
if(d+1 < depth[v]){
depth[v] = d+1;
q.push(v);
}
}
}
ll ans = 0; bool good = true;
for(int i = 1;i<=N;++i){
if(depth[i] == INF){
good = false;
break;
}
ans += depth[i];
}
if(!good) return INF;
return ans;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N;
for(int i = 1;i<=N;++i){
int K; cin >> K;
for(int j = 1;j<=K;++j){
int a; cin >> a;
adj[a].push_back(i);
}
}
ll minn = INF;
for(int i = 1;i<=N;++i){
minn = min(minn,bfs(i));
}
cout << minn << "\n";
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... |