Submission #1094298

#TimeUsernameProblemLanguageResultExecution timeMemory
1094298InvMODBosses (BOI16_bosses)C++14
100 / 100
506 ms848 KiB
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 5e3+1;
const ll INF = 1e18;

// each time we get new nodes
// contribute 1 to all of its ancestor
// contribute 1 for it

ll n, answer;
vector<int> adj[N];

void solve()
{
    cin >> n;

    for(int i = 1; i <= n; i++){
        int k; cin >> k;
        for(int j = 1; j <= k; j++){
            int x; cin >> x;
            adj[x].push_back(i);
        }
    }

    answer = INF;
    for(int i = 1; i <= n; i++){
        // rooted at i and do breadth first search

        queue<int> q;
        vector<int> h(n+1);

        h[i] = 1; q.push(i);
        while(!q.empty()){
            int x = q.front(); q.pop();
            for(int v : adj[x]){
                if(h[v]) continue;
                h[v] = h[x] + 1;
                q.push(v);
            }
        }

        ll cur_answer = 0;
        for(int j = 1; j <= n; j++){
            if(!h[j]){
                // can't visit all node
                cur_answer = INF;
                break;
            }
            cur_answer += (1ll * h[j]);
        }
        answer = min(answer, cur_answer);
    }
    cout << answer <<"\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    solve();
    return 0;
}

Compilation message (stderr)

bosses.cpp: In function 'int32_t main()':
bosses.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bosses.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...