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>
#define pii pair<int,int>
#define f first
#define s second
#define ll long long
using namespace std;
const int N = 1e5+9;
vector<int> g[N];
int main(){
cin.tie(0)->sync_with_stdio(false);
int n,m;
cin>>n;
for(int i=1;i<=n;i++){
int k;
cin>>k;
for(int j=1;j<=k;j++){
cin>>m;
g[i].push_back(m);
}
}
ll sum = LLONG_MAX;
for(int i=1;i<=n;i++){
bool visit[n+1]={0};
visit[i] = true;
queue<pii> s;
s.push({i,1});
ll nub = 0;
int cnt = 1; //count node
while(!s.empty()){
int u = s.front().f;
int w = s.front().s;
// cout<<u<<","<<w<<"* ";
s.pop();
visit[u] = true;
for(auto v:g[u]){
if(!visit[v]) {
cnt++;
nub+=(w+1);
s.push({v,w+1});
}
}
}
// cout<<nub<<" "<<cnt<<endl;
if(cnt<n) continue;
sum = min(sum,nub);
}
cout<<sum;
return 0;
}
/*
4
1 4
3 1 3 4
2 1 2
1 3
4
1 2
0
1 4
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... |