Submission #962587

#TimeUsernameProblemLanguageResultExecution timeMemory
962587shezittBosses (BOI16_bosses)C++14
100 / 100
483 ms1112 KiB
#include <iostream> #include <vector> #include <algorithm> #include <cassert> #include <cstring> #include <queue> using namespace std; using ll = long long; #define int ll #define fore(i, a, b) for(int i=a; i<b; ++i) #define endl '\n' #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define pb push_back #define ii pair<int,int> #define vi vector<int> signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<vi> k(n); vector<vi> adj(n); fore(i, 0, n){ int tam; cin >> tam; k[i].resize(tam); fore(j, 0, tam){ cin >> k[i][j]; k[i][j]--; adj[k[i][j]].pb(i); } } int ans = 1e9; vi depth(n, -1); fore(root, 0, n){ auto bfs = [&](int i) -> int { depth.assign(n, -1); int res = 0; queue<int> q; q.push(i); depth[i] = 1; while(sz(q)){ int cur = q.front(); q.pop(); res += depth[cur]; for(int v : adj[cur]){ if(depth[v] == -1){ depth[v] = depth[cur] + 1; q.push(v); } } } return res; }; int tmp = bfs(root); bool ok = 1; fore(i, 0, n){ ok &= depth[i] > -1; } if(ok){ ans = min(ans, tmp); } } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...