# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
594550 | propransh | Bosses (BOI16_bosses) | C++14 | 3 ms | 468 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 boost ios_base::sync_with_stdio(0);
#define ll long long int
#define pb push_back
#define tc long long int t; cin >> t; while(t--)
#define vpll vector<pair<ll,ll>>
#define MOD 1000000007
vector<ll> adj[5003];
bool vis[5003];
ll level[5003];
ll n;
ll bfs(ll v){
for(ll i = 0; i < 5003; ++i){
vis[i] = false;
}
vis[v]=true;
queue <ll> q;
q.push(v);
level[v] = 1;
while(!q.empty()){
ll p = q.front();
//cout<<"here at "<<p<<endl;
//cout<<p<<" has "<<adj[p].size()<<endl;
q.pop();
for(ll i = 0; i < adj[p].size(); i++){
//cout<<"Trying out "<<adj[p][i]<<endl;
if(vis[ adj[p][i] ] == false){
//cout<<"visiting "<<adj[p][i]<<" from "<<p<<endl;
level[ adj[p][i] ] = level[p]+1;
q.push( adj[p][i] );
vis[ adj[p][i] ] = true;
}
}
}
ll ans= 0 ;
for(ll i = 1; i <= n; i++){
if(vis[i] == false){
return INT_MAX;
}
ans += level[i];
//cout << i << " " << level[i] <<endl;
}
return ans;
}
int main(){
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
boost;
ll i;
cin >> n;
for(i = 0; i < n; i++){
ll k;
cin >> k;
for(ll j=0; j < k; j++){
ll x;
cin >> x;
adj[x].pb(i+1);
}
}
ll m = INT_MAX;
for(i = 1; i<=n; i++){
for(ll j = 0; j < 5003; j++){
vis[i] = false;
level[i] = 0;
}
m = min(m, bfs(i));
}
cout << m <<endl;;
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... |