結果
問題 | No.1054 Union add query |
ユーザー | jp_ste |
提出日時 | 2020-05-17 05:03:39 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 419 ms / 2,000 ms |
コード長 | 3,110 bytes |
コンパイル時間 | 2,333 ms |
コンパイル使用メモリ | 78,456 KB |
実行使用メモリ | 68,064 KB |
最終ジャッジ日時 | 2024-09-24 12:23:55 |
合計ジャッジ時間 | 6,627 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
49,784 KB |
testcase_01 | AC | 53 ms
49,888 KB |
testcase_02 | AC | 52 ms
50,108 KB |
testcase_03 | AC | 368 ms
60,756 KB |
testcase_04 | AC | 403 ms
66,236 KB |
testcase_05 | AC | 364 ms
59,056 KB |
testcase_06 | AC | 380 ms
60,812 KB |
testcase_07 | AC | 356 ms
60,248 KB |
testcase_08 | AC | 332 ms
60,552 KB |
testcase_09 | AC | 419 ms
68,064 KB |
testcase_10 | AC | 173 ms
60,536 KB |
ソースコード
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.NoSuchElementException; public class Main { static FastScanner scan = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static UnionFind union; static int N, Q; public static void main(String[] args) { N = scan.nextInt(); Q = scan.nextInt(); union = new UnionFind(N); for(int i=0; i<Q; i++) { int T = scan.nextInt(); int A = scan.nextInt(); int B = scan.nextInt(); if(T == 1) { if(union.same(A-1, B-1)) continue; union.unite(A-1, B-1); } else if(T == 2) { union.add(A-1, B); } else if(T == 3) { pw.println(union.out(A-1)); } } pw.close(); } } class UnionFind { int[] L; long[] V; UnionFind(int N) { L = new int[N]; V = new long[N]; Arrays.fill(L, -1); } int root(int x) { return L[x] < 0 ? x : root(L[x]); } boolean same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) { return; } if (L[x] < L[y]) { int w = x; x = y; y = w; } L[y] += L[x]; L[x] = y; V[x] -= V[y]; } void add(int x, int value) { x = root(x); V[x] += value; } long out(int x) { long ans = 0; while(true){ ans += V[x]; x = L[x]; if(x < 0) break; } return ans; } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } }