結果
問題 | No.19 ステージの選択 |
ユーザー | ぴろず |
提出日時 | 2014-12-19 13:15:42 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 150 ms / 5,000 ms |
コード長 | 3,624 bytes |
コンパイル時間 | 2,468 ms |
コンパイル使用メモリ | 83,604 KB |
実行使用メモリ | 42,056 KB |
最終ジャッジ日時 | 2024-06-02 12:27:22 |
合計ジャッジ時間 | 6,434 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 107 ms
40,500 KB |
testcase_01 | AC | 119 ms
41,436 KB |
testcase_02 | AC | 110 ms
40,192 KB |
testcase_03 | AC | 120 ms
41,352 KB |
testcase_04 | AC | 150 ms
41,728 KB |
testcase_05 | AC | 133 ms
41,732 KB |
testcase_06 | AC | 136 ms
41,644 KB |
testcase_07 | AC | 133 ms
41,716 KB |
testcase_08 | AC | 136 ms
41,664 KB |
testcase_09 | AC | 142 ms
41,500 KB |
testcase_10 | AC | 127 ms
41,500 KB |
testcase_11 | AC | 120 ms
41,148 KB |
testcase_12 | AC | 107 ms
40,472 KB |
testcase_13 | AC | 116 ms
41,292 KB |
testcase_14 | AC | 125 ms
41,420 KB |
testcase_15 | AC | 126 ms
41,652 KB |
testcase_16 | AC | 142 ms
41,792 KB |
testcase_17 | AC | 135 ms
41,688 KB |
testcase_18 | AC | 136 ms
42,056 KB |
testcase_19 | AC | 128 ms
41,764 KB |
testcase_20 | AC | 129 ms
41,324 KB |
testcase_21 | AC | 137 ms
41,604 KB |
testcase_22 | AC | 125 ms
41,424 KB |
testcase_23 | AC | 134 ms
41,584 KB |
ソースコード
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<n;i++) { l[i] = sc.nextInt(); int s = sc.nextInt() - 1; g.addEdge(s, i, 1); } ArrayList<ArrayList<Integer>> scc = g.stronglyConnectedComponents(); ArrayList<Pair> p = new ArrayList<>(); for(ArrayList<Integer> 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<Graph.Edge> al:g2.graph) { for(Graph.Edge e:al) { first[e.to] = false; } } int ans = 0; for(int i=0;i<n2;i++) { Pair pp = p.get(i); if (first[i]) { ans += pp.first; }else{ ans += pp.second; } } System.out.println(ans / 2D); } static class Pair { int first,second; public Pair(int first,int second) { this.first = first; this.second = second; } public String toString() { return first + "," + second; } } } class Graph { public static final int INF = 1<<29; int n; ArrayList<Edge>[] graph; @SuppressWarnings("unchecked") public Graph(int n) { this.n = n; this.graph = new ArrayList[n]; for(int i=0;i<n;i++) { graph[i] = new ArrayList<Edge>(); } } 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<ArrayList<Integer>> stronglyConnectedComponents() { ArrayList<ArrayList<Integer>> scc = new ArrayList<ArrayList<Integer>>(); int[] num = new int[n]; int[] low = new int[n]; int[] time = new int[1]; ArrayDeque<Integer> s = new ArrayDeque<Integer>(); boolean[] inS = new boolean[n]; for(int u=0;u<n;u++) { if (num[u] == 0) { visitSCC(u,scc,s,inS,low,num,time); } } return scc; } private void visitSCC(int v, ArrayList<ArrayList<Integer>> scc, ArrayDeque<Integer> 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<Integer> scc1 = new ArrayList<Integer>(); while(true) { int w = s.poll(); inS[w] = false; scc1.add(w); if (v == w) { break; } } scc.add(scc1); } } public Graph mergedGraph(ArrayList<ArrayList<Integer>> scc) { int[] map = new int[n]; int m = scc.size(); for(int i=0;i<m;i++) { ArrayList<Integer> l = scc.get(i); for(int v: l) { map[v] = i; } } Graph g2 = new Graph(m); HashSet<Long> edge = new HashSet<Long>(); for(int u=0;u<n;u++) { for(Edge e:graph[u]) { int v = e.to; if (map[u] == map[v]) { continue; } edge.add(((long) map[u]<<32) + map[v]); } } for(long e:edge) { g2.addEdge((int) (e>>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; } } }