結果

問題 No.1054 Union add query
ユーザー tentententen
提出日時 2022-08-30 15:55:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 739 ms / 2,000 ms
コード長 2,820 bytes
コンパイル時間 2,622 ms
コンパイル使用メモリ 77,664 KB
実行使用メモリ 71,144 KB
最終ジャッジ日時 2024-04-25 00:59:21
合計ジャッジ時間 10,516 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
36,844 KB
testcase_01 AC 56 ms
36,960 KB
testcase_02 AC 54 ms
36,796 KB
testcase_03 AC 701 ms
52,184 KB
testcase_04 AC 689 ms
52,932 KB
testcase_05 AC 670 ms
51,504 KB
testcase_06 AC 699 ms
60,512 KB
testcase_07 AC 661 ms
71,144 KB
testcase_08 AC 739 ms
62,108 KB
testcase_09 AC 660 ms
53,288 KB
testcase_10 AC 489 ms
50,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int q = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(n);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < q; i++) {
            int type = sc.nextInt();
            int a = sc.nextInt();
            int b = sc.nextInt();
            if (type == 1) {
                uft.unite(a - 1, b - 1);
            } else if (type == 2) {
                uft.add(a - 1, b);
            } else {
                sb.append(uft.getValue(a - 1)).append("\n");
            }
        }
        System.out.print(sb);
    }
    
    static class UnionFindTree {
        int[] parents;
        int[] values;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            values = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                int p = find(parents[x]);
                if (p != parents[x]) {
                    values[x] += values[parents[x]];
                }
                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;
            }
            parents[xx] = yy;
            values[xx] -= values[yy];
        }
        
        public void add(int idx, int v) {
            values[find(idx)] += v;
        }
        
        public int getValue(int idx) {
            int p = find(idx);
            if (p == idx) {
                return values[p];
            } else {
                return values[p] + values[idx];
            }
        }
    }
}
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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0