답안 #675066

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
675066 2022-12-27T01:38:16 Z benjaminkleyn Bosses (BOI16_bosses) C++17
100 / 100
638 ms 644 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n;
vector<int> children[5001];
bool vis[5001];
int dist[5001];

int main()
{
    cin >> n;
    for (int i = 1, k; i <= n; i++)
    {
        cin >> k;
        for (int j = 0, par; j < k; j++)
        {
            cin >> par;
            children[par].push_back(i);
        }
    }

    ll ans = LLONG_MAX;
    for (int root = 1; root <= n; root++)
    {
        queue<int> q;
        q.push(root);

        memset(vis, false, sizeof(vis));
        vis[root] = true;
        
        memset(dist, 0, sizeof(dist));

        while (!q.empty())
        {
            int u = q.front();
            q.pop();

            for (const int &v : children[u])
                if (!vis[v])
                {
                    q.push(v);
                    vis[v] = true;
                    dist[v] = dist[u] + 1;
                }
        }

        bool connected = true;
        for (int i = 1; i <= n; i++)
            connected &= vis[i];

        if (!connected)
            continue;

        ll res = 0;
        for (int i = 1; i <= n; i++)
            res += 1 + dist[i];

        ans = min(ans, res);
    }
    cout << (ans == LLONG_MAX ? -1 : ans) << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 428 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 428 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 420 KB Output is correct
12 Correct 6 ms 436 KB Output is correct
13 Correct 4 ms 468 KB Output is correct
14 Correct 128 ms 468 KB Output is correct
15 Correct 19 ms 468 KB Output is correct
16 Correct 466 ms 620 KB Output is correct
17 Correct 614 ms 644 KB Output is correct
18 Correct 638 ms 640 KB Output is correct