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
const int MX = 2e5 + 10;
int N, vis[MX], dep[MX], off = 0;
vector<int> adj[MX];
ll bfs(int u){
ll ans = 0;
queue<int> q; q.push(u);
dep[u] = 1;
vis[u] = off;
while(!q.empty()){
int cr = q.front();
ans += dep[cr];
q.pop();
for(int nxt : adj[cr]){
if(vis[nxt] == off) continue;
vis[nxt] = off;
dep[nxt] = dep[cr] + 1;
q.push(nxt);
}
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
for(int i = 1; i <= N; i++){
int k; cin >> k;
for(int j = 1; j <= k; j++){
int x; cin >> x;
adj[x].push_back(i);
}
}
ll ans = 1e18;
for(int i = 1; i <= N; i++){
off++;
ans = min(ans, bfs(i));
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |