Submission #1275718

#TimeUsernameProblemLanguageResultExecution timeMemory
1275718SinaPourkashaniBosses (BOI16_bosses)C++20
22 / 100
2 ms576 KiB
#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], 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);
        }
    }

    ll mx = 0, root = 1;
    for (ll i = 1; i <= n; i++) {
        if (adj[i].size() > mx) {
            mx = adj[i].size();
            root = i;
        }
    }

    bfs(root);

    for (ll i = 1; i <= n; i++) {
        adj2[par[i]].pb(i);
    }

    dfs(root);

    cout << ans << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...