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 ll long long
ll visited[5002];
vector<ll> graph[5002];
ll iter=0;
queue<pair<ll,ll>> q;
ll bfs(ll v, ll n){
visited[v]=iter;
q.push({v,1});
ll ans=0;
while(q.size()){
auto [v,d]=q.front();
q.pop();
ans+=d;
for (auto u : graph[v])if (visited[u]!=iter){
visited[u]=iter;
q.push({u,d+1});
}
}
for (ll i = 1; i<=n; i++)if (visited[i]!=iter)return 1e18;
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,k,v;
cin >> n;
for (ll i = 1; i<=n; i++){
cin >> k;
for (ll j = 1; j<=k; j++){
cin >> v;
graph[v].push_back(i);
}
}
ll mn=1e18;
for (ll i = 1; i<=n; i++){
iter++;
mn=min(mn,bfs(i,n));
}
cout << mn << '\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... |