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 pb push_back
#define ii pair<int, long long>
#define fi first
#define se second
using namespace std;
const int N = 5001;
const long long lim = 9223372036854775807;
const double pi = acos(-1);
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int n;
vector<int> adj[N];
void BFS(int s, long long &ans, int &num){
bool visited[n + 1];
memset(visited, false, sizeof(visited));
visited[s] = true;
queue<ii> q;
q.push({s, 1LL});
while(!q.empty()){
auto it = q.front();
q.pop();
int u = it.fi;
long long w = it.se;
ans += w;
++num;
for(int v : adj[u]){
if(visited[v])
continue;
visited[v] = true;
q.push({v, w + 1});
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
int k;
cin >> k;
for(int j = 1; j <= k; ++j){
int u;
cin >> u;
adj[u].pb(i);
}
}
long long res = lim;
for(int i = 1; i <= n; ++i){
int num = 0;
long long ans = 0;
BFS(i, ans, num);
if(num < n)
continue;
res = min(res, ans);
}
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... |