답안 #374021

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
374021 2021-03-06T12:41:39 Z vishesh312 Bosses (BOI16_bosses) C++17
100 / 100
873 ms 748 KB
#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/

#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()

using ll = long long;
const int mod = 1e9+7;

vector<vector<int>> adj;
vector<bool> vis;
ll cost;
int n, cur;

void dfs(int u, ll c) {
    vis[u] = true;
    ++cur;
    cost += c;
    for (auto v : adj[u]) {
        if (!vis[v]) {
            dfs(v, c+1);
        }
    }
}

void bfs(int s) {
    queue<array<ll, 2>> q;
    q.push({s, 1});
    vis[s] = true;
    while (!q.empty()) {
        int u = q.front()[0], d = q.front()[1];
        q.pop();
        ++cur;
        cost += d;
        for (auto v : adj[u]) {
            if (!vis[v]) {
                vis[v] = true;
                q.push({v, d+1});
            }
        }
    }
}

void solve(int tc) {
    cin >> n;
    adj.resize(n);
    vis.resize(n);
    for (int i = 0; i < n; ++i) {
        int m;
        cin >> m;
        while (m--) {
            int u;
            cin >> u;
            --u;
            adj[u].push_back(i);
        }
    }
    ll ans = 1e16;
    for (int i = 0; i < n; ++i) {
        fill(all(vis), false);
        cost = 0, cur = 0;
        bfs(i);
        if (cur == n) ans = min(ans, cost);
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int tc = 1;
    //cin >> tc;
    for (int i = 1; i <= tc; ++i) solve(i);
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 544 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 544 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 544 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 6 ms 492 KB Output is correct
13 Correct 4 ms 492 KB Output is correct
14 Correct 184 ms 620 KB Output is correct
15 Correct 3 ms 620 KB Output is correct
16 Correct 686 ms 748 KB Output is correct
17 Correct 847 ms 748 KB Output is correct
18 Correct 873 ms 620 KB Output is correct