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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 5e3 + 5;
int n;
vi par[N];
vi adj[N];
int dist[N];
int ans = INT_MAX;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
cin >> n;
ForE(i, 1, n){
int k; cin >> k;
while (k--){
int x; cin >> x;
par[i].emplace_back(x);
adj[x].emplace_back(i);
}
}
ForE(i, 1, n){
queue <int> qu;
memset(dist, -1, sizeof(dist));
dist[i] = 1; qu.emplace(i);
while (!qu.empty()){
int u = qu.front(); qu.pop();
Fora(v, adj[u]){
if (dist[v] == -1){
dist[v] = dist[u] + 1;
qu.emplace(v);
}
}
}
int tans = 0;
ForE(j, 1, n){
if (dist[j] == -1){
tans = INT_MAX;
break;
}
tans += dist[j];
}
ans = min(ans, tans);
}
cout << ans << endl;
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |