結果

問題 No.1054 Union add query
ユーザー htensaihtensai
提出日時 2020-06-04 14:42:54
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,459 ms / 2,000 ms
コード長 2,478 bytes
コンパイル時間 2,142 ms
コンパイル使用メモリ 85,268 KB
実行使用メモリ 71,348 KB
最終ジャッジ日時 2023-08-19 10:24:28
合計ジャッジ時間 16,075 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,324 KB
testcase_01 AC 116 ms
56,140 KB
testcase_02 AC 110 ms
56,228 KB
testcase_03 AC 1,394 ms
67,448 KB
testcase_04 AC 1,429 ms
71,212 KB
testcase_05 AC 1,375 ms
67,064 KB
testcase_06 AC 1,432 ms
69,356 KB
testcase_07 AC 1,392 ms
69,800 KB
testcase_08 AC 1,459 ms
67,636 KB
testcase_09 AC 1,381 ms
71,348 KB
testcase_10 AC 1,166 ms
71,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		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();
		    if (type == 1) {
		        int a = sc.nextInt() - 1;
		        int b = sc.nextInt() - 1;
		        uft.unite(a, b);
		    } else if (type == 2) {
		        int a = sc.nextInt() - 1;
		        int b = sc.nextInt();
		        uft.add(a, b);
		    } else {
		        sb.append(uft.get(sc.nextInt() - 1)).append("\n");
		        sc.nextInt();
		    }
		}
		
		System.out.print(sb);
	}
	
	static class UnionFindTree {
	    int[] parents;
	    int[] points;
	    int[] rank;
	    
	    public UnionFindTree(int size) {
	        parents = new int[size];
	        points = new int[size];
	        rank = new int[size];
	        for (int i = 0; i < size; i++) {
	            parents[i] = i;
	        }
	    }
	    
	    public int find(int x) {
	        if (parents[x] == x) {
	            return x;
	        }
	        int point = getPoint(parents[x]);
	        parents[x] = find(parents[x]);
	        points[x] = points[x] + point - points[parents[x]];
	        return parents[x];
	    }
	    
	    public int getPoint(int x) {
	        if (parents[x] == x) {
	            return points[x];
	        } else {
	            return getPoint(parents[x]) + points[x];
	        }
	    }
	    
	    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;
	        }
	        if (rank[xx] < rank[yy]) {
    	        parents[xx] = yy;
    	        points[xx] -= points[yy];
    	        rank[yy] = Math.max(rank[yy], rank[xx] + 1);
	        } else {
    	        parents[yy] = xx;
    	        points[yy] -= points[xx];
    	        rank[xx] = Math.max(rank[xx], rank[yy] + 1);
	        }
	    }
	    
	    public void add(int x, int value) {
	        points[find(x)] += value;
	    }
	    
	    public int get(int x) {
	        int xx = find(x);
	        if (xx == x) {
	            return points[x];
	        } else {
	            return points[x] + points[xx];
	        }
	    }
	    
	    public String toString() {
	        return Arrays.toString(parents) + Arrays.toString(points);
	    }
	}
}
0