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 ll long long
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define left (h<<1),l,(l + r)/2
#define right ((h<<1)|1),(l + r)/2 + 1,r
#define int ll
using namespace std;
const int N = 5000 + 3;
int vis[N],dep[N],cnt[N];
vector <int> v[N];
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
for (int i = 1; i <= n; i++){
int k;
cin>>k;
for (int j= 1; j <= k; j++){
int x;
cin>>x;
v[x].pb(i);
}
}
int ans = 1e18;
for (int root = 1; root <= n; root++){
for (int i = 1;i <= n; i++)
dep[i] = 0,cnt[i] = 0;
queue <int> q;
q.push(root);
dep[root] = 1;
while(q.size()){
int x = q.front(); q.pop();
for (int to: v[x]){
if (!dep[to]) {
dep[to] = dep[x] + 1;
q.push(to);
}
}
}
for(int i=1;i<=n;i++)
cnt[dep[i]]++;
int sum = n,cur = n;
for (int i = 1; i <= n; i++){
cur -= cnt[i];
sum += cur;
}
ans=min(ans,sum);
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |