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<cstring>
#include<vector>
#include<queue>
using namespace std;
int vis[5007];
long long cost = 0;
vector<int> vec[5007];
int n,cnt=0;
void bfs(int src)
{
queue< pair<int , int> >q ;
q.push({src , 1}) ;
vis[src] = 1 ;
while(!q.empty())
{
pair<int , int>p = q.front() ;
q.pop() ;
cnt++ ;
int node = p.first ;
cost += (p.second * 1ll) ;
for(auto &child : vec[node])
{
if(vis[child] == 1)
continue ;
vis[child] = 1 ;
q.push({child , p.second+1}) ;
}
}
return ;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
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(vis , 0 , sizeof(vis)) ;
cnt = 0 , cost = 0;
bfs(i) ;
if(cnt != n)
continue;
ans = min(ans , cost) ;
}
return printf("%lld" , ans) , 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... |