# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1094298 | InvMOD | Bosses (BOI16_bosses) | C++14 | 506 ms | 848 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;
using ll = long long;
const int N = 5e3+1;
const ll INF = 1e18;
// each time we get new nodes
// contribute 1 to all of its ancestor
// contribute 1 for it
ll n, answer;
vector<int> adj[N];
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++){
int k; cin >> k;
for(int j = 1; j <= k; j++){
int x; cin >> x;
adj[x].push_back(i);
}
}
answer = INF;
for(int i = 1; i <= n; i++){
// rooted at i and do breadth first search
queue<int> q;
vector<int> h(n+1);
h[i] = 1; q.push(i);
while(!q.empty()){
int x = q.front(); q.pop();
for(int v : adj[x]){
if(h[v]) continue;
h[v] = h[x] + 1;
q.push(v);
}
}
ll cur_answer = 0;
for(int j = 1; j <= n; j++){
if(!h[j]){
// can't visit all node
cur_answer = INF;
break;
}
cur_answer += (1ll * h[j]);
}
answer = min(answer, cur_answer);
}
cout << answer <<"\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
solve();
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... |