結果

問題 No.1451 集団登校
ユーザー tentententen
提出日時 2023-07-13 08:57:15
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,970 bytes
コンパイル時間 4,664 ms
コンパイル使用メモリ 81,548 KB
実行使用メモリ 60,672 KB
最終ジャッジ日時 2023-10-12 22:21:37
合計ジャッジ時間 13,401 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,460 KB
testcase_01 AC 46 ms
50,008 KB
testcase_02 AC 45 ms
49,508 KB
testcase_03 AC 46 ms
49,400 KB
testcase_04 AC 46 ms
49,480 KB
testcase_05 AC 45 ms
50,004 KB
testcase_06 AC 47 ms
49,524 KB
testcase_07 AC 45 ms
47,576 KB
testcase_08 AC 314 ms
60,672 KB
testcase_09 AC 45 ms
49,460 KB
testcase_10 AC 238 ms
58,380 KB
testcase_11 AC 157 ms
54,616 KB
testcase_12 AC 250 ms
58,648 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 120 ms
52,292 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 240 ms
58,396 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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 (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