結果

問題 No.19 ステージの選択
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-05 12:17:47
言語 Java19
(openjdk 21)
結果
AC  
実行時間 121 ms / 5,000 ms
コード長 2,616 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 75,452 KB
実行使用メモリ 56,016 KB
最終ジャッジ日時 2023-08-24 23:34:45
合計ジャッジ時間 6,523 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
55,496 KB
testcase_01 AC 115 ms
55,704 KB
testcase_02 AC 114 ms
54,396 KB
testcase_03 AC 115 ms
55,764 KB
testcase_04 AC 116 ms
55,740 KB
testcase_05 AC 117 ms
55,608 KB
testcase_06 AC 116 ms
55,672 KB
testcase_07 AC 119 ms
55,580 KB
testcase_08 AC 121 ms
55,512 KB
testcase_09 AC 120 ms
55,580 KB
testcase_10 AC 119 ms
55,824 KB
testcase_11 AC 121 ms
56,016 KB
testcase_12 AC 119 ms
55,640 KB
testcase_13 AC 118 ms
55,396 KB
testcase_14 AC 117 ms
55,460 KB
testcase_15 AC 116 ms
55,448 KB
testcase_16 AC 120 ms
55,776 KB
testcase_17 AC 118 ms
55,548 KB
testcase_18 AC 118 ms
55,500 KB
testcase_19 AC 115 ms
55,780 KB
testcase_20 AC 117 ms
55,508 KB
testcase_21 AC 118 ms
55,784 KB
testcase_22 AC 118 ms
55,780 KB
testcase_23 AC 119 ms
55,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        int[]l=new int[n],s=new int[n];
        for(int i=0;i<n;++i){
            l[i]=sc.nextInt();
            s[i]=sc.nextInt()-1;
        }
        int ans=0;
        boolean[]vis=new boolean[n];
        for(int i=0;i<n;++i){
            if(vis[i])continue;
            Map<Integer,Integer>hm=new HashMap<Integer,Integer>();
            List<Integer>lis=new ArrayList<Integer>();
            int step=0;
            int cur=i;
            while(true){
                if(vis[cur])break;
                vis[cur]=true;
                hm.put(cur,step);
                lis.add(cur);
                cur=s[cur];
                step++;
            }
            int min=1<<30;
            Integer stm=hm.get(cur);
            int st=stm==null?lis.size():stm;
            for(int j=st;j<lis.size();++j)min=Math.min(min,l[lis.get(j)]);
            if(stm==null)min=0;
            ans+=min;
            for(int v:lis)ans+=l[v];
        }
        out.printf("%.1f\n",ans/2.0);
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        int[] nextIntArray(int n){
            int[]r=new int[n];
            for(int i=0;i<n;++i)r[i]=nextInt();
            return r;
        }
    }
}
0