結果
問題 | No.1390 Get together |
ユーザー | neko_the_shadow |
提出日時 | 2021-04-13 10:11:41 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,117 ms / 2,000 ms |
コード長 | 6,540 bytes |
コンパイル時間 | 2,915 ms |
コンパイル使用メモリ | 98,508 KB |
実行使用メモリ | 105,976 KB |
最終ジャッジ日時 | 2024-06-29 04:49:01 |
合計ジャッジ時間 | 21,464 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 67 ms
50,728 KB |
testcase_01 | AC | 66 ms
50,872 KB |
testcase_02 | AC | 68 ms
50,980 KB |
testcase_03 | AC | 157 ms
55,292 KB |
testcase_04 | AC | 155 ms
55,804 KB |
testcase_05 | AC | 154 ms
55,812 KB |
testcase_06 | AC | 154 ms
56,172 KB |
testcase_07 | AC | 153 ms
55,824 KB |
testcase_08 | AC | 153 ms
55,724 KB |
testcase_09 | AC | 164 ms
55,500 KB |
testcase_10 | AC | 65 ms
50,792 KB |
testcase_11 | AC | 66 ms
50,864 KB |
testcase_12 | AC | 64 ms
51,000 KB |
testcase_13 | AC | 66 ms
51,240 KB |
testcase_14 | AC | 64 ms
51,232 KB |
testcase_15 | AC | 66 ms
50,996 KB |
testcase_16 | AC | 763 ms
84,828 KB |
testcase_17 | AC | 672 ms
78,836 KB |
testcase_18 | AC | 817 ms
103,916 KB |
testcase_19 | AC | 1,002 ms
103,888 KB |
testcase_20 | AC | 731 ms
78,812 KB |
testcase_21 | AC | 778 ms
78,796 KB |
testcase_22 | AC | 951 ms
93,968 KB |
testcase_23 | AC | 989 ms
92,772 KB |
testcase_24 | AC | 967 ms
102,612 KB |
testcase_25 | AC | 922 ms
103,920 KB |
testcase_26 | AC | 886 ms
94,500 KB |
testcase_27 | AC | 917 ms
105,124 KB |
testcase_28 | AC | 1,018 ms
105,792 KB |
testcase_29 | AC | 1,112 ms
105,976 KB |
testcase_30 | AC | 1,117 ms
104,332 KB |
testcase_31 | AC | 952 ms
94,792 KB |
ソースコード
package _1390; import java.io.BufferedReader; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.io.UncheckedIOException; import java.math.BigInteger; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Deque; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Supplier; import java.util.regex.Pattern; import java.util.stream.IntStream; public class Main { public void exec() { int n = stdin.nextInt(); int m = stdin.nextInt(); int[] b = new int[n]; int[] c = new int[n]; for (int i = 0; i < n; i++) { b[i] = stdin.nextInt(); c[i] = stdin.nextInt(); } Map<Integer, List<Integer>> d = new HashMap<>(); for (int i = 0; i < n; i++) { d.computeIfAbsent(b[i], unused -> new ArrayList<>()).add(c[i]); } Map<Integer, Integer> e = new HashMap<>(); for (int i = 0; i < n; i++) { e.put(c[i], b[i]); } UnionFind uf = new UnionFind(Arrays.stream(c).max().getAsInt()+1); for (List<Integer> slimes : d.values()) { for (int i = 1; i < slimes.size(); i++) { uf.union(slimes.get(0), slimes.get(i)); } } int count = 0; for (int box : d.keySet()) { List<Integer> slimes = d.get(box); if (e.get(uf.find(slimes.get(0))) != box) { count++; } } stdout.println(count); } public class UnionFind { private int[] parent; private int[] size; public UnionFind(int n) { this.parent = IntStream.range(0, n).toArray(); this.size = new int[n]; Arrays.fill(this.size, 1); } public int find(int x) { if (parent[x] == x) { return x; } parent[x] = find(parent[x]); return parent[x]; } public boolean same(int x, int y) { return find(x) == find(y); } public void union(int x, int y) { x = find(x); y = find(y); if (x == y) { return ; } if (size[x] < size[y]) { parent[x] = y; size[y] += size[x]; } else { parent[y] = x; size[x] += size[y]; } } public int size(int x) { return size[find(x)]; } } private static final Stdin stdin = new Stdin(System.in); private static final Stdout stdout = new Stdout(System.out); public static void main(String[] args) { try { new Main().exec(); } finally { stdout.flush(); } } public static class Stdin { private Deque<String> queue; private BufferedReader in; private Pattern space; public Stdin(InputStream in) { this.queue = new ArrayDeque<>(); this.in = new BufferedReader(new InputStreamReader(in)); this.space = Pattern.compile(" "); } public String nextString() { if (queue.isEmpty()) { try { String line = in.readLine(); if (line == null) { throw new EOFException(); } space.splitAsStream(line).forEach(this.queue::addLast); } catch (IOException e) { throw new UncheckedIOException(e); } } return queue.removeFirst(); } public int nextInt() { return Integer.parseInt(nextString()); } public double nextDouble() { return Double.parseDouble(nextString()); } public long nextLong() { return Long.parseLong(nextString()); } public BigInteger nextBigInteger() { return new BigInteger(nextString()); } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = nextString(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public BigInteger[] nexBigIntegerArray(int n) { BigInteger[] a = new BigInteger[n]; for (int i = 0; i < n; i++) a[i] = nextBigInteger(); return a; } public List<Integer> nextIntegerList(int n) { return nextList(n, this::nextInt); } public List<Long> nextLongList(int n) { return nextList(n, this::nextLong); } public List<Double> nextDoubleList(int n) { return nextList(n, this::nextDouble); } public List<String> nextStringList(int n) { return nextList(n, this::nextString); } public List<BigInteger> nextBigIntegerList(int n) { return nextList(n, this::nextBigInteger); } private <T> List<T> nextList(int n, Supplier<T> supplier) { List<T> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(supplier.get()); return a; } } public static class Stdout { private PrintWriter stdout; public Stdout(PrintStream stdout) { this.stdout = new PrintWriter(stdout, false); } public void println(Object ... objs) { for (int i = 0, len = objs.length; i < len; i++) { stdout.print(objs[i]); if (i != len-1) stdout.print(' '); } stdout.println(); } public void flush() { stdout.flush(); } } }