結果

問題 No.2290 UnUnion Find
ユーザー viral8viral8
提出日時 2023-05-06 12:25:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 493 ms / 2,000 ms
コード長 1,603 bytes
コンパイル時間 2,210 ms
コンパイル使用メモリ 77,796 KB
実行使用メモリ 50,136 KB
最終ジャッジ日時 2024-05-03 01:17:45
合計ジャッジ時間 27,743 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
37,064 KB
testcase_01 AC 52 ms
36,824 KB
testcase_02 AC 389 ms
47,484 KB
testcase_03 AC 457 ms
48,268 KB
testcase_04 AC 469 ms
48,440 KB
testcase_05 AC 454 ms
48,404 KB
testcase_06 AC 469 ms
49,264 KB
testcase_07 AC 471 ms
48,328 KB
testcase_08 AC 468 ms
50,136 KB
testcase_09 AC 467 ms
48,504 KB
testcase_10 AC 468 ms
49,000 KB
testcase_11 AC 467 ms
48,748 KB
testcase_12 AC 469 ms
49,296 KB
testcase_13 AC 474 ms
49,504 KB
testcase_14 AC 470 ms
49,144 KB
testcase_15 AC 470 ms
48,292 KB
testcase_16 AC 466 ms
49,444 KB
testcase_17 AC 468 ms
48,480 KB
testcase_18 AC 462 ms
48,600 KB
testcase_19 AC 461 ms
47,632 KB
testcase_20 AC 480 ms
49,160 KB
testcase_21 AC 424 ms
47,140 KB
testcase_22 AC 443 ms
48,392 KB
testcase_23 AC 433 ms
47,916 KB
testcase_24 AC 467 ms
48,356 KB
testcase_25 AC 424 ms
47,608 KB
testcase_26 AC 466 ms
48,764 KB
testcase_27 AC 465 ms
48,584 KB
testcase_28 AC 445 ms
47,760 KB
testcase_29 AC 457 ms
48,208 KB
testcase_30 AC 428 ms
47,476 KB
testcase_31 AC 493 ms
48,556 KB
testcase_32 AC 432 ms
47,632 KB
testcase_33 AC 438 ms
47,592 KB
testcase_34 AC 481 ms
49,012 KB
testcase_35 AC 482 ms
48,580 KB
testcase_36 AC 446 ms
47,624 KB
testcase_37 AC 438 ms
47,320 KB
testcase_38 AC 438 ms
47,640 KB
testcase_39 AC 440 ms
47,920 KB
testcase_40 AC 454 ms
48,604 KB
testcase_41 AC 441 ms
47,948 KB
testcase_42 AC 462 ms
47,576 KB
testcase_43 AC 452 ms
47,924 KB
testcase_44 AC 468 ms
48,612 KB
testcase_45 AC 455 ms
47,724 KB
testcase_46 AC 454 ms
48,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
class Main{
	public static void main(String[] args)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] str = br.readLine().split(" ");
		int N = Integer.parseInt(str[0]);
		int Q = Integer.parseInt(str[1]);
		UnionFind uf = new UnionFind(N+1);
		StringBuilder answer = new StringBuilder();
		while(Q-->0){
			str = br.readLine().split(" ");
			int q = Integer.parseInt(str[0]);
			if(q==1){
				int u = Integer.parseInt(str[1]);
				int v = Integer.parseInt(str[2]);
				uf.unite(u,v);
			}
			else{
				int v = Integer.parseInt(str[1]);
				int ans = uf.unUnitePoint(v);
				answer.append(ans>N?-1:ans);
				answer.append('\n');
			}
		}
		System.out.println(answer.toString());
	}
}
class UnionFind{
	int N;
	int[] par,unUnite,size;
	UnionFind(int N){
		this.N = N;
		par = new int[N];
		unUnite = new int[N];
		size = new int[N];
		Arrays.fill(par,-1);
		Arrays.fill(unUnite,1);
		Arrays.fill(size,1);
		unUnite[1] = 2;
	}
	int root(int x){
		if(par[x]==-1)
			return x;
		return par[x] = root(par[x]);
	}
	void unite(int x,int y){
		int rootX = root(x);
		int rootY = root(y);
		if(rootX==rootY)
			return;
		if(size[rootX]<size[rootY]){
			int temp = rootX;
			rootX = rootY;
			rootY = temp;
		}
		par[rootY] = rootX;
		size[rootX] += size[rootY];
		int max = Math.max(unUnite[rootX],unUnite[rootY]);
		while(max<N&&root(max)==rootX)max++;
		unUnite[rootX] = max;
	}
	int unUnitePoint(int x){
		return unUnite[root(x)];
	}
}
0