結果

問題 No.1639 最小通信路
ユーザー tentententen
提出日時 2022-08-05 16:59:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 2,000 bytes
コンパイル時間 3,555 ms
コンパイル使用メモリ 73,980 KB
実行使用メモリ 55,308 KB
最終ジャッジ日時 2023-10-13 16:35:21
合計ジャッジ時間 7,290 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
49,392 KB
testcase_01 AC 39 ms
49,304 KB
testcase_02 AC 48 ms
50,272 KB
testcase_03 AC 54 ms
50,532 KB
testcase_04 AC 136 ms
55,308 KB
testcase_05 AC 46 ms
49,388 KB
testcase_06 AC 46 ms
49,400 KB
testcase_07 AC 44 ms
49,420 KB
testcase_08 AC 51 ms
50,116 KB
testcase_09 AC 46 ms
49,432 KB
testcase_10 AC 46 ms
49,868 KB
testcase_11 AC 44 ms
49,444 KB
testcase_12 AC 43 ms
49,372 KB
testcase_13 AC 45 ms
49,508 KB
testcase_14 AC 39 ms
49,436 KB
testcase_15 AC 43 ms
49,340 KB
testcase_16 AC 46 ms
49,556 KB
testcase_17 AC 46 ms
49,312 KB
testcase_18 AC 42 ms
49,504 KB
testcase_19 AC 44 ms
49,436 KB
testcase_20 AC 48 ms
50,108 KB
testcase_21 AC 40 ms
49,432 KB
testcase_22 AC 42 ms
49,328 KB
testcase_23 AC 43 ms
49,336 KB
testcase_24 AC 42 ms
49,416 KB
testcase_25 AC 42 ms
49,328 KB
testcase_26 AC 42 ms
49,364 KB
testcase_27 AC 41 ms
49,472 KB
testcase_28 AC 40 ms
49,336 KB
testcase_29 AC 42 ms
49,268 KB
testcase_30 AC 50 ms
50,536 KB
testcase_31 AC 41 ms
47,620 KB
testcase_32 AC 46 ms
50,400 KB
testcase_33 AC 43 ms
49,456 KB
testcase_34 AC 53 ms
50,664 KB
testcase_35 AC 60 ms
51,860 KB
testcase_36 AC 53 ms
48,484 KB
testcase_37 AC 48 ms
50,732 KB
testcase_38 AC 41 ms
49,324 KB
testcase_39 AC 42 ms
49,320 KB
testcase_40 AC 39 ms
49,376 KB
testcase_41 AC 41 ms
49,856 KB
testcase_42 AC 51 ms
50,532 KB
testcase_43 AC 43 ms
49,440 KB
testcase_44 AC 42 ms
49,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(n);
        int count = 0;
        while (true) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            String cost = sc.next();
            if (!uft.same(a, b)) {
                uft.unite(a, b);
                count++;
                if (count >= n - 1) {
                    System.out.println(cost);
                    return;
                }
            }
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            parents[find(x)] = find(y);
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0