結果

問題 No.556 仁義なきサルたち
ユーザー tentententen
提出日時 2022-10-14 15:50:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 2,302 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 74,188 KB
実行使用メモリ 54,976 KB
最終ジャッジ日時 2023-09-08 19:43:04
合計ジャッジ時間 4,877 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,440 KB
testcase_01 AC 43 ms
49,292 KB
testcase_02 AC 43 ms
49,480 KB
testcase_03 AC 45 ms
49,368 KB
testcase_04 AC 44 ms
49,180 KB
testcase_05 AC 45 ms
49,472 KB
testcase_06 AC 46 ms
49,236 KB
testcase_07 AC 47 ms
49,492 KB
testcase_08 AC 48 ms
49,272 KB
testcase_09 AC 52 ms
49,360 KB
testcase_10 AC 59 ms
50,440 KB
testcase_11 AC 64 ms
51,520 KB
testcase_12 AC 75 ms
50,224 KB
testcase_13 AC 57 ms
50,520 KB
testcase_14 AC 112 ms
52,500 KB
testcase_15 AC 113 ms
52,188 KB
testcase_16 AC 119 ms
52,844 KB
testcase_17 AC 123 ms
52,920 KB
testcase_18 AC 126 ms
54,928 KB
testcase_19 AC 127 ms
54,548 KB
testcase_20 AC 128 ms
54,976 KB
testcase_21 AC 131 ms
54,872 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();
        int m = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(n);
        for (int i = 0; i < m; i++) {
            uft.unite(sc.nextInt() - 1, sc.nextInt() - 1);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(uft.find(i) + 1).append("\n");
        }
        System.out.print(sb);
    }
    
    static class UnionFindTree {
        int[] parents;
        int[] counts;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            counts = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
                counts[i] = 1;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return;
            }
            if (counts[xx] > counts[yy] || (counts[xx] == counts[yy] && xx < yy)) {
                parents[yy] = xx;
                counts[xx] += counts[yy];
            } else {
                parents[xx] = yy;
                counts[yy] += counts[xx];
            }
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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