Submission #726972

#TimeUsernameProblemLanguageResultExecution timeMemory
726972rahulvermaBosses (BOI16_bosses)Java
67 / 100
1521 ms14804 KiB
import java.io.*;
import java.util.*;

public class bosses {

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		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++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int k = Integer.parseInt(st.nextToken());
			for(int j = 0; j < k; j++) {
				int v = Integer.parseInt(st.nextToken()) - 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];
			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;
				break;
			}
			if(reached) ans = Math.min(ans,  cur);
		}
		PrintWriter pw = new PrintWriter(System.out);
		pw.println(ans);
		pw.close();
	}

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...