제출 #338121

#제출 시각아이디문제언어결과실행 시간메모리
338121MilosMilutinovicBosses (BOI16_bosses)C++14
67 / 100
1589 ms1004 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=5050;
vector<int> E[N];
int n;
int BFS(int u){
    bool was[N];
    int dist[N];
    for(int i=1;i<=N;i++)was[i]=false,dist[i]=N*2;
    deque<int> q;
    q.pb(u);
    dist[u]=0,was[u]=1;
    while(!q.empty()){
        int x=q[0];
        for(int c:E[x]){
            dist[c]=min(dist[c],dist[x]+1);
            if(!was[c]){
                was[c]=1;
                q.pb(c);
            }
        }
        q.pop_front();
    }
    map<int,int> cnt;
    for(int i=1;i<=n;i++){
        if(dist[i]==2*N)return N*N*2;
        cnt[dist[i]]++;
    }
    int dp[N];
    for(int i=0;i<N;i++)dp[i]=0;
    for(int i=N-2;i>=0;i--)dp[i]=dp[i+1]+cnt[i];
    int ans=0;
    for(int i=0;i<N;i++)ans+=dp[i];
    return ans;
}
int main(){
    scanf("%i",&n);
    for(int i=1;i<=n;i++){
        int sz;scanf("%i",&sz);
        while(sz--){
            int p;scanf("%i",&p);
            E[p].pb(i);
        }
    }
    int ans=N*N;
    for(int i=1;i<=n;i++){
        int tmp=BFS(i);
        ans=min(ans,tmp);
        //printf("%i %i\n",i,tmp);
    }
    printf("%i",ans);
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

bosses.cpp: In function 'int main()':
bosses.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   38 |     scanf("%i",&n);
      |     ~~~~~^~~~~~~~~
bosses.cpp:40:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   40 |         int sz;scanf("%i",&sz);
      |                ~~~~~^~~~~~~~~~
bosses.cpp:42:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   42 |             int p;scanf("%i",&p);
      |                   ~~~~~^~~~~~~~~
bosses.cpp: In function 'int BFS(int)':
bosses.cpp:10:32: warning: iteration 5049 invokes undefined behavior [-Waggressive-loop-optimizations]
   10 |     for(int i=1;i<=N;i++)was[i]=false,dist[i]=N*2;
      |                          ~~~~~~^~~~~~
bosses.cpp:10:18: note: within this loop
   10 |     for(int i=1;i<=N;i++)was[i]=false,dist[i]=N*2;
      |                 ~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...