This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
vector <int> graf[5005];
int cost;
bool visited[5005];
int mk;
void bfs(int v){
visited[v] = true;
queue <pair <int, int>> q;
q.push({v, 1});
while(!q.empty()){
mk++;
int x = q.front().first;
int y = q.front().second;
cost += y;
q.pop();
for(auto c : graf[x]){
if(!visited[c]){
visited[c] = 1;
q.push({c, y+1});
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cout.precision(10);
cout<<fixed;
int n;
cin >> n;
for(int i=1; i<=n; i++){
int x;
cin >> x;
for(int j=1; j<=x; j++){
int a;
cin >> a;
graf[a].push_back(i);
}
}
int mincost = 1000000000;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
visited[j] = 0;
}
cost = 0;
mk = 0;
bfs(i);
if(mk != n) continue;
mincost = min(mincost, cost);
}
cout << mincost << "\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... |