Submission #350794

#TimeUsernameProblemLanguageResultExecution timeMemory
350794Hossein8320Bosses (BOI16_bosses)C++17
100 / 100
885 ms1028 KiB
// That's what she said !
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const int maxn = 5e3 + 9 , modn = 1e9 + 7;
int n, v, k, ans = 1e18, cnt[maxn], h[maxn];
bool mark[maxn];
vector <int> adj[maxn], adj2[maxn];
int bfs(int v)
{
    queue <int> q;
    int H = 0, sum = 0, g = 0;
    mark[v] = 1;
    cnt[0] = 1;
    q.push(v);
    while(q.size()){
        v = q.front();
        q.pop();
        for(auto i : adj[v]){
            if(mark[i])
                continue;
            h[i] = h[v] + 1;
            cnt[h[i]]++;
            H = max(H , h[i]);
            mark[i] = 1;
            q.push(i);
        }
    }
    for(int i = H; i > -1; i--){
        g += cnt[i];
        sum += g;
    }
    for(int i = 1; i <= n; i++)
        if(!mark[i])
            return -1;
    return sum;
}
int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n;
	for(int i = 1; i <= n; i++){
        cin >> k;
        for(int j = 0; j < k; j++){
            cin >> v;
            adj[v].push_back(i);
        }
	}
    for(int i = 1; i <= n; i++){
        int p = bfs(i);
        if(p != -1)
            ans = min(ans , p);
        for(int i = 0; i <= n; i++){
            cnt[i] = h[i] = 0;
            mark[i] = 0;
        }
    }
    cout << ans;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...