結果
問題 | No.1451 集団登校 |
ユーザー | tenten |
提出日時 | 2023-07-13 08:57:15 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,970 bytes |
コンパイル時間 | 3,235 ms |
コンパイル使用メモリ | 92,824 KB |
実行使用メモリ | 48,700 KB |
最終ジャッジ日時 | 2024-09-14 20:35:00 |
合計ジャッジ時間 | 11,617 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
37,032 KB |
testcase_01 | AC | 54 ms
36,884 KB |
testcase_02 | AC | 54 ms
36,908 KB |
testcase_03 | AC | 53 ms
36,956 KB |
testcase_04 | AC | 53 ms
37,000 KB |
testcase_05 | AC | 54 ms
36,992 KB |
testcase_06 | AC | 53 ms
37,020 KB |
testcase_07 | AC | 52 ms
36,844 KB |
testcase_08 | AC | 352 ms
48,384 KB |
testcase_09 | AC | 52 ms
36,924 KB |
testcase_10 | AC | 271 ms
47,716 KB |
testcase_11 | AC | 168 ms
41,324 KB |
testcase_12 | AC | 268 ms
47,500 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 132 ms
40,140 KB |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 262 ms
47,780 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
ソースコード
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(); } }