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;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int INF = 1e18;
const int maxn = 1e5 + 5;
const int N = 1e9 + 7;
vector<int> adj[maxn];
int dep[maxn];
signed main(void){
fastio;
int n;
cin>>n;
for(int i = 0; i < n; i++){
int d;
cin>>d;
for(int j = 0; j < d; j++){
int x;
cin>>x;
x--;
adj[x].pb(i);
}
}
int ans = INF;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) dep[j] = 0;
dep[i] = 1;
queue<int> q;
q.push(i);
int cnt = 0, cur = 0;
while(!q.empty()){
int pos = q.front(); q.pop();
cnt++;
cur += dep[pos];
for(auto x : adj[pos]){
if(dep[x]) continue;
dep[x] = dep[pos] + 1;
q.push(x);
}
}
if(cnt < n) continue;
ans = min(ans, cur);
}
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... |