This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In His Name
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<long long, int> pli;
typedef pair<long long, long long> pll;
#define ll long long
#define int ll
#define F first
#define S second
#define pb push_back
#define bug(x) cout << "Ah shit , here we go again : " << x <<endl
#define all(x) x.begin() , x.end()
#define ceil(x,y) x/y + min(1,x%y);
const int maxn = 1e5 + 10, MOD = 1e9 + 7;
const ll INF = 1e18;
int n , h[maxn] , ans = INF , ians = 0;
vector<int> adj[maxn] , adj2[maxn];
bool mark[maxn];
void dfs(int v){
mark[v] = true;
for(int u : adj2[v]){
if(!mark[u]){
dfs(u);
h[v] += h[u];
}
}
}
main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i++){
int x;
cin >> x;
for(int u = 1 ; u <= x ; u++){
int y;
cin >> y;
adj[y].pb(i);
}
}
queue<int> q;
for(int i = 1 ; i <= n ; i++){
q.push(i);
mark[i] = true;
while(!q.empty()){
int v = q.front();
q.pop();
for(int u : adj[v]){
if(!mark[u]) {
q.push(u);
mark[u] = true;
h[u] = h[v]+1;
}
}
}
int maxi = -1e9 , ok = 1;
for(int j = 1 ; j <= n ; j++) if(!mark[j]) ok = 0;
for(int j = 1 ; j <= n ; j++){
if(h[j] > maxi) maxi = h[j];
h[j] = 0;
mark[j] = false;
}
if(ok and maxi < ans) ans = maxi , ians = i;
}
q.push(ians);
mark[ians] = true;
while(!q.empty()){
int v = q.front();
q.pop();
for(int u : adj[v]){
if(!mark[u]) {
q.push(u);
mark[u] = true;
h[u] = h[v]+1;
adj2[v].pb(u);
}
}
}
memset(mark , false , sizeof mark);
fill(h , h+n+1 , 1);
dfs(ians);
int sum = 0;
for(int i = 1 ; i <= n ; i++) sum += h[i];
cout << sum;
}
Compilation message (stderr)
bosses.cpp:32:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
32 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |