This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 5e3+10;
const int inf = 1e18;
int n,ans,sum;
bool check;
vector<int>G[maxn];
vector<int>H[maxn];
bool marked[maxn];
int val[maxn];
//int lev[maxn];
void bfs(int v){
marked[v] = true;
queue<int>q;
q.push(v);
// lev[v] = 0;
while(!q.empty()){
int p = q.front();
q.pop();
H[p].clear();
for(int b : G[p]){
if(marked[b]) continue;
// lev[b] = lev[p] + 1;
marked[b] = true;
q.push(b);
H[p].push_back(b);
}
}
}
void dfs(int v){
int s = 0;
for(int b : H[v]){
dfs(b);
s += val[b];
}
val[v] = s + 1;
sum += val[v];
if(ans <= sum){
check = false;
return;
}
}
int32_t main(){
ios:: sync_with_stdio(0),cin.tie(0),cout.tie(0);
////////////////////////////////////////////////
ans = inf;
sum = 0;
cin >> n;
for(int i = 0;i<n;i++){
int x; cin >> x;
for(int j = 0;j<x;j++){
int y; cin >> y;
G[y].push_back(i+1);
}
}
for(int i = 0;i<n;i++){
bfs(i+1);
fill(marked,marked+n+1,false);
check = true;
sum = 0;
dfs(i+1);
if(check == true){
ans = min(ans,sum);
}
}
cout << ans << "\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... |