# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1030977 | dpsaveslives | Bosses (BOI16_bosses) | C++17 | 1036 ms | 1148 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;
const int MAXN = 5010;
vector<int> adj[MAXN];
vector<int> graph[MAXN],st;
int N,ans,cnt;
void dfs(int u, int p){
for(auto v : graph[u]){
if(v != p){
dfs(v,u);
st[u] += st[v];
}
}
cnt += st[u];
}
int bfs(int start){
queue<int> q; q.push(start);
vector<int> dist(N+1,-1),par(N+1,-1); dist[start] = 0;
int maxd = -1, ret = 0;
while(!q.empty()){
int u = q.front(); q.pop();
for(auto v : adj[u]){
if(dist[v] == -1){
dist[v] = dist[u]+1;
par[v] = u;
q.push(v);
}
}
}
for(int i = 1;i<=N;++i) graph[i].clear();
for(int i = 1;i<=N;++i){
if(dist[i] == -1) return -1;
graph[par[i]].push_back(i);
}
st = vector<int>(N+1,1); cnt = 0;
dfs(start,0);
return cnt;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
for(int i = 1;i<=N;++i){
int k; cin >> k;
for(int j = 0;j<k;++j){
int v; cin >> v;
adj[v].push_back(i);
}
}
int res = -1, root = -1;
for(int i = 1;i<=N;++i){
int ret = bfs(i);
if(ret == -1) continue;
if(ret < res || res == -1){
res = ret;
root = i;
}
}
cout << res << "\n";
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... |