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<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<utility>
using namespace std;
vector<int> vec[5007];
bool visited[5007];
long long cost;
int cnt;
void bfs(int x){
queue<pair<int,int>> q;
q.push({x,1});
visited[x]=true;
while(!q.empty()){
int node=q.front().first;
int cs=q.front().second;
++cnt;
q.pop();
cost+=cs;
for(auto i:vec[node]){
if(visited[i]) continue;
visited[i]=true;
q.push({i,cs+1});
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n; cin >> n;
for(int i=1;i<=n;++i){
int m; cin >> m;
while(m--){
int x; cin >> x;
vec[x].push_back(i);
}
}
long long ans=1e18;
for(int i=1;i<=n;++i){
memset(visited,false,sizeof(visited));
cost=0; cnt=0;
bfs(i);
if(cnt!=n) continue;
ans=min(ans,cost);
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |