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;
#define ll long long
#define f first
#define s second
#define pb push_back
#define pi pair<int,int>
#define pl pair<ll,int>
const int MAX = 5e5+1;
int color[MAX];
int dis[MAX];
vector<int>g[MAX];
int n;
ll bfs(int u){
dis[u] =1;
memset(color,0,sizeof(color));
queue<int>q;
q.push(u);
while(!q.empty()){
int from = q.front();
q.pop();
if(color[from]) continue;
color[from]=1;
for(int to: g[from]){
if(!color[to]){
dis[to]= dis[from]+1;
q.push(to);
}
}
}
ll sum = 0;
for(int i = 1; i <=n; i++) {
sum+= dis[i];
if(!dis[i]) return INT_MAX;
}
// cout << u << " "<< sum << endl;
return sum;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
int temp, k;
for(int i = 1; i <= n; i++){
cin >> temp;
while(temp--){
cin >> k;
g[k].pb(i);
}
}
ll ans= INT_MAX;
for(int i= 1; i <= n; i++){
ans = min(bfs(i),ans);
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |