제출 #954681

#제출 시각아이디문제언어결과실행 시간메모리
9546814QT0RBosses (BOI16_bosses)C++17
100 / 100
515 ms860 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long ll visited[5002]; vector<ll> graph[5002]; ll iter=0; queue<pair<ll,ll>> q; ll bfs(ll v, ll n){ visited[v]=iter; q.push({v,1}); ll ans=0; while(q.size()){ auto [v,d]=q.front(); q.pop(); ans+=d; for (auto u : graph[v])if (visited[u]!=iter){ visited[u]=iter; q.push({u,d+1}); } } for (ll i = 1; i<=n; i++)if (visited[i]!=iter)return 1e18; return ans; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll n,k,v; cin >> n; for (ll i = 1; i<=n; i++){ cin >> k; for (ll j = 1; j<=k; j++){ cin >> v; graph[v].push_back(i); } } ll mn=1e18; for (ll i = 1; i<=n; i++){ iter++; mn=min(mn,bfs(i,n)); } cout << mn << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...