# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
764337 | Ellinor | Bosses (BOI16_bosses) | C++14 | 1 ms | 340 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 rep(i,a,b) for(int i = (a); i < (b); i++)
typedef long long ll;
int N,k;
vector<vector<int>> graph;
vector<bool> visited;
ll a;
queue<pair<int,int>> q; //node, level
void bfs_calc(int node, int level) //bfs !
{
a+=level;
rep(i,0,graph[node].size()){
if (!visited[graph[node][i]]) {
q.push({graph[node][i],level+1});
visited[graph[node][i]]=true;
}
}
}
int32_t main()
{
cin>>N;
graph.assign(N,{});
int root=-1;
rep(i,0,N){
cin>>k;
rep(j,0,k){
cin>>a;
a--;
//boss to employee
graph[a].push_back(i);
}
if(k==0)root=i;
}
ll ans=1e17; //hm !
//ki = 0 ?? !
if(root==-1){
rep(i,0,N){
visited.assign(N,false);
//check all visited
a=0;
q.push({i,1});
visited[i]=true;
while(!q.empty()){
auto x=q.front();
q.pop();
bfs_calc(x.first,x.second);
}
while(true){
rep(i,0,visited.size()){
if(!visited[i]){
assert(1==0);
}
}
break;
}
ans=min(ans,a);
}
}
else{
visited.assign(N,false);
a=0;
q.push({root,1});
visited[root]=true;
while(!q.empty()){
auto x=q.front();
q.pop();
bfs_calc(x.first,x.second);
}
while(true){
rep(i,0,visited.size()){
if(!visited[i]){
assert(1==0);
}
}
break;
}
ans=a;
}
cout << ans;
}
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... |