結果

問題 No.19 ステージの選択
ユーザー ぴろずぴろず
提出日時 2014-12-19 13:15:42
言語 Java19
(openjdk 21)
結果
AC  
実行時間 174 ms / 5,000 ms
コード長 3,624 bytes
コンパイル時間 2,646 ms
コンパイル使用メモリ 89,700 KB
実行使用メモリ 56,472 KB
最終ジャッジ日時 2023-08-24 23:26:19
合計ジャッジ時間 7,245 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,316 KB
testcase_01 AC 122 ms
55,876 KB
testcase_02 AC 122 ms
55,556 KB
testcase_03 AC 124 ms
55,520 KB
testcase_04 AC 174 ms
55,848 KB
testcase_05 AC 143 ms
56,328 KB
testcase_06 AC 143 ms
56,184 KB
testcase_07 AC 143 ms
55,768 KB
testcase_08 AC 143 ms
55,548 KB
testcase_09 AC 144 ms
55,696 KB
testcase_10 AC 128 ms
55,820 KB
testcase_11 AC 123 ms
55,608 KB
testcase_12 AC 124 ms
55,884 KB
testcase_13 AC 123 ms
55,896 KB
testcase_14 AC 128 ms
55,700 KB
testcase_15 AC 134 ms
56,188 KB
testcase_16 AC 139 ms
55,468 KB
testcase_17 AC 132 ms
56,016 KB
testcase_18 AC 145 ms
56,116 KB
testcase_19 AC 135 ms
56,052 KB
testcase_20 AC 127 ms
55,908 KB
testcase_21 AC 137 ms
56,472 KB
testcase_22 AC 132 ms
55,808 KB
testcase_23 AC 135 ms
56,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
		}
	}
}
0