#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector <pair<ll, ll>> vp;
typedef pair<ll, ll> pll;
typedef map <ll, ll> mll;
typedef set <ll> sll;
#define pb push_back
#define ff first
#define ss second
#define str to_string
#define all(x) (x).begin(), (x).end()
#define print(x) for (auto i : x) cout << i << ' '; cout << '\n';
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
const ll maxn = 1e5+5;
const ll mod = 1e9+7;
const ll inf = 1e18;
ll pw(ll a, ll b, ll M = mod) {ll ans = 1; for (; b; a = ((a * a) % M), b >>= 1) if (b & 1) ans = (ans * a) % M; return ans;}
ll n, dist[maxn], par[maxn], s[maxn], d[maxn], ans = 0;
vll adj[maxn], adj2[maxn];
void bfs(ll root) {
fill(dist+1, dist+n+1, inf);
dist[root] = 0;
queue <ll> q;
q.push(root);
par[root] = 0;
while (!q.empty()) {
ll v = q.front();
q.pop();
for (ll u : adj[v]) {
if (dist[u] > dist[v] + 1) {
par[u] = v;
dist[u] = dist[v] + 1;
q.push(u);
}
}
}
}
void dfs(ll v) {
ll sum = 0;
for (ll u : adj2[v]) {
dfs(u);
sum += s[u];
}
s[v] = sum + 1;
ans += s[v];
}
int main() {
FastIO
cin >> n;
for (ll i = 1; i <= n; i++) {
ll k; cin >> k;
for (ll j = 1; j <= k; j++) {
ll x; cin >> x;
adj[x].pb(i);
}
d[i] = k;
}
ll mx = 0, root = 1;
for (ll i = 1; i <= n; i++) {
if (adj[i].size() > mx) {
mx = adj[i].size();
root = i;
}
}
ll mn = inf;
for (ll i = 1; i <= n; i++) {
bfs(i);
for (ll j = 1; j <= n; j++) {
adj2[par[j]].pb(j);
}
ans = 0;
dfs(i);
bool flag = true;
for (ll j = 1; j <= n; j++) {
adj2[j] = {};
if (dist[j] == inf) flag = false;
}
if (flag) mn = min(mn, ans);
}
cout << mn << '\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... |