# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
338123 | MilosMilutinovic | Bosses (BOI16_bosses) | C++14 | 1127 ms | 876 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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+1];
int dist[n+1],cnt[n+1],dp[n+1];
for(int i=0;i<=n;i++)was[i]=false,dist[i]=N*2,cnt[i]=0,dp[i]=0;
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();
}
for(int i=1;i<=n;i++){
if(dist[i]==2*N)return N*N*2;
cnt[dist[i]]++;
}
for(int i=n-1;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;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |