이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
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;
};
bool ok = 1;
fore(i, 0, n){
ok &= depth[i] > -1;
}
if(ok){
ans = min(ans, bfs(root));
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |