結果
| 問題 |
No.19 ステージの選択
|
| ユーザー |
ぴろず
|
| 提出日時 | 2014-12-19 13:15:42 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 166 ms / 5,000 ms |
| コード長 | 3,624 bytes |
| コンパイル時間 | 2,245 ms |
| コンパイル使用メモリ | 91,024 KB |
| 実行使用メモリ | 42,000 KB |
| 最終ジャッジ日時 | 2024-12-23 10:00:30 |
| 合計ジャッジ時間 | 6,341 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
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;
}
}
}
ぴろず