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/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long
#define F first
#define S second
#define pb push_back
#define lc (ind << 1)
#define rc (lc | 1)
#define md ((b + e) >> 1)
const ll N = 5000 + 5;
const ll M = 1e6 + 5;
const ll LOG = 30;
const ll INF = 1e9 + 5;
const ll MOD = 1e9 + 7;
int n;
vector <int> g[N];
ll ans = INF * INF;
ll h[N];
queue <int> q;
inline ll bfs(int r){
fill(h + 1, h + n + 1, INF);
h[r] = 0;
q.push(r);
while (q.size()){
int v = q.front();
q.pop();
for (int u : g[v]){
if (h[v] + 1 < h[u]){
h[u] = h[v] + 1;
q.push(u);
}
}
}
ll sum = 0;
for (int i = 1; i <= n; i ++){
sum += h[i];
}
return sum + n;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int v = 1; v <= n; v ++){
int m;
cin >> m;
for (int i = 0; i < m; i ++){
int u;
cin >> u;
g[u].pb(v);
}
}
for (int v = 1; v <= n; v ++){
ans = min(ans, bfs(v));
}
cout << ans << '\n';
return 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... |