이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC optimize("avx2")
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(xyxy, yxy) (xyxy) = max((xyxy), (yxy))
#define smin(xyxy, yxy) (xyxy) = min((xyxy), (yxy))
#define all(xyxy) (xyxy).begin(), (xyxy).end()
#define len(xyxy) ((int)(xyxy).size())
const int maxn = 5000 + 5;
const int inf = 1e9 + 7;
int n, d[maxn];
vector<int> adj[maxn];
queue<int> q;
ll bfs(int v){
memset(d, 63, sizeof d);
while(q.size()) q.pop();
d[v] = 0;
q.push(v);
while(q.size()){
v = q.front();
q.pop();
for(auto u: adj[v]){
if(d[u] > d[v] + 1){
d[u] = d[v] + 1;
q.push(u);
}
}
}
ll res = 0;
for(int i = 0; i < n; i++) res += d[i];
return res;
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++){
int k; cin >> k;
for(int j = 0; j < k; j++){
int u; cin >> u;
u--;
adj[u].pb(i);
}
}
ll ans = inf;
for(int i = 0; i < n; i++) smin(ans, bfs(i));
cout << ans + n << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |