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;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define END "\n"
#define rall(v) (v).rbegin(), (v).rend()
#define all(v) (v).begin(), (v).end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void solve(){
int n;
cin >> n;
vector<vector<int>> adj(n + 1);
for (int i = 1; i <= n; ++i) {
int k;
cin >> k;
while (k--) {
int a;
cin >> a;
adj[a].push_back(i);
}
}
ll ans = LLONG_MAX;
for (int s = 1; s <= n; ++s) {
queue<int> q;
vector<bool> vis(n + 1);
vector<int> dist(n + 1);
dist[s] = 1;
vis[s] = 1;
q.push(s);
int tot = 0;
ll sum = 0;
while (!q.empty()) {
++tot;
int v = q.front();
sum += dist[v];
q.pop();
for (auto u: adj[v]) {
if (!vis[u]) vis[u] = 1, dist[u] = dist[v] + 1, q.push(u);
}
}
if (tot == n) ans = min(ans, sum);
}
cout << ans << END;
}
int main()
{
fastio
int t = 1;
// cin>> t;
while(t--) solve();
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... |