이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define f first
#define s second
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define F0R(i, x) FOR(i, 0, x)
#define FORd(i, a, b) for(int i = (b)-1; i >= (a); i--)
#define F0Rd(i, x) FORd(i, 0, x)
#define ckif(a, b, c) ((c) ? (a) : (b))
const int MAX_N = 5001;
const ll MOD = 1000000007;
const ll INF = 1e18;
int n;
vector<int> adj[MAX_N];
int main(int argc, const char * argv[]){
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
F0R(i, n){
int a; cin >> a;
F0R(j, a){
int x; cin >> x; x--;
adj[x].push_back(a);
}
}
ll ans = INF;
F0R(i, n){
// i is the root of the tree
// ans for this root is just the sum of the depths of all nodes
vector<ll> dist(n, INF), num(n, INF);
queue<pair<pll, int>> q; q.push({{i, 0}, -1});
while(!q.empty()){
ll curr = q.front().f.f, cost = q.front().f.s, par = q.front().s; q.pop();
if(dist[curr] != INF) continue;
if(~par) num[curr]++, num[par]++;
dist[curr] = cost;
for(int child : adj[curr])
q.push({{child, cost+1}, curr});
}
ll tot = 0;
F0R(j, n) tot += dist[j]+1;
ans = min(ans, tot);
}
cout << ans << endl;
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... |