package no019; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Graph g = new Graph(n); int[] l = new int[n]; for(int i=0;i> scc = g.stronglyConnectedComponents(); ArrayList p = new ArrayList<>(); for(ArrayList group: scc) { int min = 10000; int sum = 0; for(int x:group) { min = Math.min(min,l[x]); sum += l[x]; } p.add(new Pair(sum+min, sum)); } Graph g2 = g.mergedGraph(scc); int n2 = scc.size(); boolean[] first = new boolean[n2]; Arrays.fill(first, true); for(ArrayList al:g2.graph) { for(Graph.Edge e:al) { first[e.to] = false; } } int ans = 0; for(int i=0;i[] graph; @SuppressWarnings("unchecked") public Graph(int n) { this.n = n; this.graph = new ArrayList[n]; for(int i=0;i(); } } public void addBidirectionalEdge(int from,int to,int cost) { addEdge(from,to,cost); addEdge(to,from,cost); } public void addEdge(int from,int to,int cost) { graph[from].add(new Edge(to, cost)); } public ArrayList> stronglyConnectedComponents() { ArrayList> scc = new ArrayList>(); int[] num = new int[n]; int[] low = new int[n]; int[] time = new int[1]; ArrayDeque s = new ArrayDeque(); boolean[] inS = new boolean[n]; for(int u=0;u> scc, ArrayDeque s, boolean[] inS, int[] low, int[] num, int[] time) { low[v] = num[v] = ++time[0]; s.push(v); inS[v] = true; for(Edge e:graph[v]) { int w = e.to; if (num[w] == 0) { visitSCC(w,scc,s,inS,low,num,time); low[v] = Math.min(low[v],low[w]); }else if (inS[w]) { low[v] = Math.min(low[v],num[w]); } } if (low[v] == num[v]) { ArrayList scc1 = new ArrayList(); while(true) { int w = s.poll(); inS[w] = false; scc1.add(w); if (v == w) { break; } } scc.add(scc1); } } public Graph mergedGraph(ArrayList> scc) { int[] map = new int[n]; int m = scc.size(); for(int i=0;i l = scc.get(i); for(int v: l) { map[v] = i; } } Graph g2 = new Graph(m); HashSet edge = new HashSet(); for(int u=0;u>32), (int) (e & 0xffff), 1); } return g2; } public Graph noSCCGraph() { return mergedGraph(stronglyConnectedComponents()); } class Edge { int to; int cost; public Edge(int to,int cost) { this.to = to; this.cost = cost; } } }