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;
typedef long long ll;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; i ++)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; i --)
const int MAX=5003;
int vis[MAX],n,cnt=0;
ll cost=0;
vector< vector<int> >adj(MAX) ;
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 : adj[node])
{
if(vis[child]==1) continue;
vis[child]=1;
q.push({child,p.second+1}) ;
}
}
}
int main()
{
// freopen("BOSSES.INP","r",stdin);
// freopen("BOSSES.OUT","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
FOR(i,1,n)
{
int m;
cin>>m;
while(m--)
{
int x;
cin>>x;
adj[x].push_back(i);
}
}
ll ans=1e18;
FOR(i,1,n)
{
memset(vis,0,sizeof(vis));
cnt=0,cost=0;
bfs(i);
if(cnt!=n) continue;
ans=min(ans,cost);
}
cout<<ans;
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... |