This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.io.*;
import java.util.*;
public class bosses {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < n; i++) graph.add(new ArrayList<Integer>());
for(int i = 0; i < n; i++) {
int k = s.nextInt();
for(int j = 0; j < k; j++) {
int v = s.nextInt() - 1;
graph.get(v).add(i);
}
}
long ans = Long.MAX_VALUE;
for(int i = 1; i < n; i++) {
long cur = 1;
Queue<Integer> q = new LinkedList<>();
q.add(i);
int[] dist = new int[n];
Arrays.fill(dist, 0);
dist[i] = 1;
while(!q.isEmpty()) {
int node = q.poll();
for(int node2: graph.get(node)) {
if(dist[node2] == 0) {
cur += dist[node] + 1;
dist[node2] = dist[node] + 1;
q.add(node2);
}
}
}
boolean reached = true;
for(int x: dist) if(x == 0) reached = false;
if(reached) ans = Math.min(ans, cur);
}
System.out.println(ans);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |