Submission #329225

#TimeUsernameProblemLanguageResultExecution timeMemory
329225iliccmarkoBosses (BOI16_bosses)C++14
0 / 100
1 ms492 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define INF 1000000000
#define LINF 1000000000000000LL
#define pb push_back
#define all(x) x.begin(), x.end()
#define len(s) (int)s.size()
#define test_case { int t; cin>>t; while(t--)solve(); }
#define input(n, v) {for(int i = 0;i<n;i++) cin>>v[i];}
#define output(n, v) {for(int i = 0;i<n;i++) cout<<v[i]<<" "; cout<<endl;}
#define single_case solve();
#define line cout<<"------------"<<endl;
#define ios { ios_base::sync_with_stdio(false); cin.tie(NULL); }
using namespace std;
int n;
const int N = 5e3 + 5;
vector<vector<int> > v(N);
vector<vector<int> > g(N);
bitset<N> vidjen;
int dp[N];

int dfs(int u, int pret)
{
    ll a = 0;
    ll ans = 0;
    for(int x : g[u])
    {
        if(x==pret) continue;
        ans += dfs(x, u);
        a += dp[x];
    }
    dp[u] = a + 1LL;
    return dp[u] + ans;
}

int make_graph(int u)
{
    for(int i = 0;i<n;i++)
        vidjen[i] = 0;
    queue<int> q;
    q.push(u);
    for(int i = 0;i<n;i++)
        g[i].clear();
    vidjen[u] = 1;
    while(len(q))
    {
        int top = q.front();
        q.pop();
        for(int x : v[top])
        {
            if(vidjen[x]) continue;
            q.push(x);
            g[top].pb(x);
            vidjen[x] = 1;
        }
    }
    for(int i = 0;i<n;i++)
        dp[i] = 0;
    for(int i = 0;i<n;i++)
        if(!vidjen[i]) return INF;
    return dfs(u, -1);
}

int main()
{
    //ios
    scanf("%d", &n);
    for(int i = 0;i<n;i++)
    {
        int k;
        cin>>k;
        while(k--)
        {
            int a;
            cin>>a;
            a--;
            v[a].pb(i);
        }
    }
    int res = INF;
    int root = 0;
    int mini = INF;
    for(int i = 0;i<n;i++)
    {
        queue<pair<int, int> > q;
        for(int j = 0;j<n;j++)
            vidjen[j] = 0;
        vidjen[i] = 1;
        q.push({i, 0});
        int maks = 0;
        int cnt = 1;
        while(len(q))
        {
            auto x  = q.front();
            int s = x.first;
            q.pop();
            maks = max(maks, x.second);
            for(int i : g[s])
            {
                if(vidjen[i]) continue;
                vidjen[i] = 1;
                cnt++;
                q.push({i, x.second+1});
            }
        }
        if(maks<mini&&cnt==n)
        {
            mini = maks;
            root = i;
        }
    }

    res = make_graph(root);

    printf("%d", res);





    return 0;
}

Compilation message (stderr)

bosses.cpp: In function 'int main()':
bosses.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...