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 <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <stack>
#include <queue>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <iomanip>
#include <cassert>
#include <random>
using namespace std;
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define pb push_back
#define FOR(i, n) for(int i = 0; i < (n); i++)
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
const int mod = 1e9 + 7;
const int mod2 = 998244353;
void fact_init(int n);
ll exp(ll taban, ll us, ll md);
ll ebob(ll a, ll b);
ll ekok(ll a, ll b);
ll komb(ll a, ll b);
vector<ll> fact;
vector<ll> inv_fact;
void fact_init(int n, int md){
fact.resize(n+5);
inv_fact.resize(n+5);
fact[0] = inv_fact[0] = 1;
for(int i = 1; i <= n; i++){
fact[i] = (fact[i-1] * i) % md;
inv_fact[i] = exp(fact[i], md-2, md);
}
}
ll exp(ll taban, ll us, ll md) {
ll carpan = taban % md;
if(carpan == 0) return 0;
ll temp = us;
ll res = 1;
while(temp){
if(temp % 2) res = (res*carpan) % md;
temp /= 2;
carpan = (carpan*carpan) % md;
}
return res;
}
ll ebob(ll a, ll b){
if(!a)return b;
return ebob(b%a, a);
}
ll ekok(ll a, ll b){
return (a*b)/ebob(a, b);
}
ll komb(int a, int b, int md){
if(a < b) return 0;
return fact[a] * (inv_fact[a-b] * inv_fact[b] % md) % md;
}
const int N = 5e3 + 5;
vector<int> vec[N];
vector<int> rev[N];
int par[N];
int level[N];
void dfs(int cur, int dont){
par[cur] = dont;
level[cur] = level[dont] + 1;
for(auto x: rev[cur]){
if(x != dont && level[x] == 0) dfs(x, cur);
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(NULL);
int n; cin>>n;
for(int i = 1; i <= n; i++){
int k; cin>>k;
for(int j = 0; j < k; j++){
int s; cin>>s;
vec[i].pb(s);
rev[s].pb(i);
}
}
int ans = 1e9;
for(int root = 1; root <= n; root++){
FOR(i, N) level[i] = 0;
dfs(root, 0);
bool constructed = true;
for(int i = 1; i <= n; i++) constructed &= level[i] > 0;
if(!constructed) continue;
/*vector<int> floor[N];
for(int i = 1; i <= n; i++) floor[level[i]].pb(i);*/
//cout<<root<<"\n";
FOR(j, 5) for(int i = n; i >= 1; i--){
int node = i;
//cout<<i<<" "<<node<<"\n";
level[node] = level[par[node]] + 1;
int selected_boss = 0;
int selected_level = level[node] - 1;
for(auto boss: vec[node]){
if(level[boss] < selected_level){
selected_level = level[boss];
selected_boss = boss;
}
}
if(selected_boss == 0) continue;
level[node] = selected_level + 1;
par[node] = selected_boss;
}
for(int i = 1; i <= n; i++) level[i] = level[par[i]] + 1;
int cur = 0;
for(int i = 1; i <= n; i++) cur += level[i];
/*cout<<root<<"\n";
for(int i = 1; i <= n; i++) cout<<i<<" "<<par[i]<<" "<<level[i]<<"\n";
cout<<cur<<"\n";*/
ans = min(ans, cur);
}
cout<<ans<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |