結果

問題 No.1054 Union add query
ユーザー htensaihtensai
提出日時 2020-05-26 13:15:48
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,364 bytes
コンパイル時間 2,231 ms
コンパイル使用メモリ 77,720 KB
実行使用メモリ 56,032 KB
最終ジャッジ日時 2024-04-21 04:15:33
合計ジャッジ時間 8,815 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
36,940 KB
testcase_01 AC 52 ms
36,520 KB
testcase_02 AC 52 ms
36,512 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 657 ms
51,980 KB
testcase_07 AC 667 ms
52,924 KB
testcase_08 AC 687 ms
51,464 KB
testcase_09 AC 643 ms
55,384 KB
testcase_10 AC 491 ms
51,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
 	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] first = br.readLine().split(" ", 2);
		int n = Integer.parseInt(first[0]);
		int q = Integer.parseInt(first[1]);
		UnionFindTree uft = new UnionFindTree(n);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < q; i++) {
		    String[] line = br.readLine().split(" ", 3);
		    int type = Integer.parseInt(line[0]);
		    int a = Integer.parseInt(line[1]);
		    int b = Integer.parseInt(line[2]);
		    if (type == 1) {
		        uft.unite(a - 1, b - 1);
		    } else if (type == 2) {
		        uft.add(a - 1, b);
		    } else {
		        sb.append(uft.get(a - 1)).append("\n");
		    }
		}
		System.out.print(sb);
	}
	
	static class UnionFindTree {
	    int[] parents;
	    int[] ranks;
	    int[] points;
	    
	    public UnionFindTree(int size) {
	        parents = new int[size];
	        ranks = new int[size];
	        points = new int[size];
	        for (int i = 0; i < size; i++) {
	            parents[i] = i;
	        }
	    }
	    
	    public int find(int x) {
	        if (parents[x] == x) {
	            return x;
	        } else {
	            points[x] += innerGet(parents[x]);
	            return parents[x] = find(parents[x]);
	        }
	    }
	    
	    private int innerGet(int x) {
	        if (parents[x] == x) {
	            return 0;
	        } else {
	            points[x] += innerGet(parents[x]);
	            return points[x];
	        }
	    }
	    
	    public void unite(int x, int y) {
	        int xx = find(x);
	        int yy = find(y);
	        if (xx == yy) {
	            return;
	        }
	        if (ranks[xx] < ranks[yy]) {
	            parents[xx] = yy;
	            points[xx] -= points[yy];
	            ranks[yy] = Math.max(ranks[yy], ranks[xx] + 1);
	        } else {
	            parents[yy] = xx;
	            points[yy] -= points[xx];
	            ranks[xx] = Math.max(ranks[xx], ranks[yy] + 1);
	        }
	    }
	    
	    public void add(int x, int value)  {
	        points[find(x)] += value;
	    }
	    
	    public int get(int x) {
	        if (parents[x] == x) {
	            return points[x];
	        } else {
	            return points[find(x)] + points[x];
	        }
	    }
	}
}
0