結果

問題 No.1451 集団登校
ユーザー tentententen
提出日時 2023-07-13 09:00:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 376 ms / 2,000 ms
コード長 4,036 bytes
コンパイル時間 3,888 ms
コンパイル使用メモリ 80,796 KB
実行使用メモリ 58,956 KB
最終ジャッジ日時 2023-10-12 22:24:38
合計ジャッジ時間 11,671 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
49,548 KB
testcase_01 AC 47 ms
49,788 KB
testcase_02 AC 48 ms
49,376 KB
testcase_03 AC 48 ms
49,428 KB
testcase_04 AC 48 ms
49,388 KB
testcase_05 AC 47 ms
49,372 KB
testcase_06 AC 48 ms
49,432 KB
testcase_07 AC 47 ms
49,464 KB
testcase_08 AC 343 ms
58,956 KB
testcase_09 AC 46 ms
49,396 KB
testcase_10 AC 265 ms
58,768 KB
testcase_11 AC 171 ms
54,468 KB
testcase_12 AC 273 ms
58,240 KB
testcase_13 AC 343 ms
58,284 KB
testcase_14 AC 374 ms
58,540 KB
testcase_15 AC 266 ms
57,488 KB
testcase_16 AC 309 ms
58,412 KB
testcase_17 AC 247 ms
56,992 KB
testcase_18 AC 209 ms
57,644 KB
testcase_19 AC 123 ms
51,904 KB
testcase_20 AC 376 ms
58,520 KB
testcase_21 AC 210 ms
56,780 KB
testcase_22 AC 184 ms
56,400 KB
testcase_23 AC 144 ms
55,204 KB
testcase_24 AC 263 ms
58,320 KB
testcase_25 AC 260 ms
58,024 KB
testcase_26 AC 316 ms
58,452 KB
testcase_27 AC 309 ms
58,768 KB
testcase_28 AC 198 ms
55,260 KB
testcase_29 AC 262 ms
57,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static final int MOD = 1000000007;
    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(pow(uft.getValue(i), MOD - 2)).append("\n");
        }
        System.out.print(sb);
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return pow(x, p - 1)* x % MOD;
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        int[] counts;
        long[] values;

        public UnionFindTree(int size) {
            parents = new int[size];
            values = new long[size];
            counts = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
                counts[i] = 1;
                values[i] = 1;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                int p = find(parents[x]);
                if (parents[x] != parents[parents[x]]) {
                    values[x] *= values[parents[x]];
                    values[x] %= MOD;
                }
                return parents[x] = p;
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return;
            }
            if (counts[xx] == counts[yy]) {
                values[yy] *= pow(values[xx], MOD - 2);
                values[yy] %= MOD;
                values[xx] *= 2;
                values[xx] %= MOD;
                counts[xx] += counts[yy];
                parents[yy] = xx;
            } else if (counts[xx] < counts[yy]) {
                values[xx] = 0;
                counts[yy] += counts[xx];
                parents[xx] = yy;
            } else {
                values[yy] = 0;
                counts[xx] += counts[yy];
                parents[yy] = xx;
            }
        }
        
        public long getValue(int x) {
            find(x);
            if (x == parents[x]) {
                return values[x];
            } else {
                return values[x] * values[parents[x]] % MOD;
            }
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0