이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
#define sep ' '
#define F first
#define S second
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define kill(x) {cout << x << endl;return;}
#define sz(x) int(x.size())
#define SP(x) setprecision(x)
#define mod(x) (1ll*x%M+M)%M
#define pq priority_queue
#define mid (l+r)/2
// #define mid2 (l+r+1)/2
#define pll pair<ll, ll>
#define REP(i, k) for (int i = 0 ; i < k ; i ++)
#define MP make_pair
const int M = 998244353;
const int N = 5e3+1;
int n;
int k[N];
vi adj[N];
int dis[N];
int calc(int r) {
queue<int> q;
fill(dis, dis+n, -1);
dis[r] = 1;
q.push(r);
while(!q.empty()) {
int v = q.front();
q.pop();
for(int neigh: adj[v]) {
if (dis[neigh] == -1) {
dis[neigh] = dis[v] + 1;
q.push(neigh);
}
}
}
int ans = 0;
for(int i = 0; i < n ; i ++) {
if (dis[i] == -1) return n*(n+1)/2;
ans += dis[i];
}
return ans;
}
void solve() {
cin >> n;
for(int i = 0 ; i < n ; i ++) {
cin >> k[i];
for(int j = 0 ; j < k[i] ; j ++) {
int u; cin >> u; u--;
adj[u].pb(i);
}
}
int ans = n*(n+1)/2;
for(int i = 0 ; i < n ; i ++)
ans = min(ans, calc(i));
cout << ans << endl;
}
// check MAXN
int32_t main() {
fastIO;
solve();
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... |