Submission #1293873

#TimeUsernameProblemLanguageResultExecution timeMemory
1293873hynmjBosses (BOI16_bosses)C++20
100 / 100
394 ms784 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const long long N = 5e3 + 5;
int a[N];
vector<int> graph[N];
bool vis[N];
int n, m, e;

int bfs(int k)
{
    vector<int> dis(n + 1, 1e18);
    queue<int> q;
    q.push(k);
    dis[k] = 1;
    while (q.size())
    {
        int t = q.front();
        q.pop();
        for (int i : graph[t])
        {
            if (dis[i] > dis[t] + 1)
            {
                dis[i] = dis[t] + 1;
                q.push(i);
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (dis[i] == 1e18)
            return dis[i];
        ans += dis[i];
    }

    return ans;
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> m;
        for (int j = 0; j < m; j++)
        {
            cin >> e;
            graph[e].push_back(i);
        }
    }
    int ans = 1e18;
    for (int i = 1; i <= n; i++)
    {
        ans = min(ans, bfs(i));
    }
    cout << ans << endl;
    // for (int i = 0; i < n + 1; i++)
    // {
    //     cout << "i = " << i << endl;
    //     for (auto j : graph[i])
    //         cout << j << ' ';
    //     cout << endl;
    // }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++)
    {
        // cout << "Case #" << i << ':' << ' ';
        solve();
        cout << endl;
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...