結果

問題 No.2290 UnUnion Find
ユーザー viral8viral8
提出日時 2023-05-06 12:18:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,316 ms / 2,000 ms
コード長 1,161 bytes
コンパイル時間 2,602 ms
コンパイル使用メモリ 78,228 KB
実行使用メモリ 71,608 KB
最終ジャッジ日時 2024-05-03 01:15:23
合計ジャッジ時間 62,144 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
40,132 KB
testcase_01 AC 135 ms
41,124 KB
testcase_02 AC 1,003 ms
60,100 KB
testcase_03 AC 1,148 ms
60,012 KB
testcase_04 AC 1,316 ms
71,608 KB
testcase_05 AC 1,292 ms
59,600 KB
testcase_06 AC 1,184 ms
59,952 KB
testcase_07 AC 1,144 ms
61,344 KB
testcase_08 AC 1,208 ms
60,988 KB
testcase_09 AC 1,194 ms
61,060 KB
testcase_10 AC 1,128 ms
61,320 KB
testcase_11 AC 1,192 ms
60,848 KB
testcase_12 AC 1,175 ms
61,184 KB
testcase_13 AC 1,185 ms
61,168 KB
testcase_14 AC 1,215 ms
60,212 KB
testcase_15 AC 1,195 ms
61,164 KB
testcase_16 AC 1,218 ms
60,996 KB
testcase_17 AC 1,086 ms
60,636 KB
testcase_18 AC 1,132 ms
60,956 KB
testcase_19 AC 1,205 ms
59,940 KB
testcase_20 AC 1,187 ms
59,716 KB
testcase_21 AC 1,022 ms
59,196 KB
testcase_22 AC 1,118 ms
59,788 KB
testcase_23 AC 1,146 ms
59,640 KB
testcase_24 AC 1,224 ms
59,796 KB
testcase_25 AC 1,154 ms
59,756 KB
testcase_26 AC 1,234 ms
59,904 KB
testcase_27 AC 1,222 ms
60,240 KB
testcase_28 AC 1,155 ms
59,172 KB
testcase_29 AC 1,232 ms
59,956 KB
testcase_30 AC 1,129 ms
59,584 KB
testcase_31 AC 1,235 ms
58,840 KB
testcase_32 AC 1,159 ms
59,228 KB
testcase_33 AC 1,206 ms
58,484 KB
testcase_34 AC 1,209 ms
60,436 KB
testcase_35 AC 1,157 ms
60,332 KB
testcase_36 AC 1,110 ms
59,520 KB
testcase_37 AC 1,191 ms
59,136 KB
testcase_38 AC 1,092 ms
59,788 KB
testcase_39 AC 1,112 ms
59,408 KB
testcase_40 AC 1,218 ms
60,060 KB
testcase_41 AC 1,091 ms
60,456 KB
testcase_42 AC 1,182 ms
59,612 KB
testcase_43 AC 1,102 ms
59,580 KB
testcase_44 AC 1,160 ms
59,584 KB
testcase_45 AC 1,140 ms
59,688 KB
testcase_46 AC 1,237 ms
59,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.Arrays;
class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int Q = sc.nextInt();
		UnionFind uf = new UnionFind(N+1);
		StringBuilder answer = new StringBuilder();
		while(Q-->0){
			int q = sc.nextInt();
			if(q==1){
				int u = sc.nextInt();
				int v = sc.nextInt();
				uf.unite(u,v);
			}
			else{
				int v = sc.nextInt();
				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;
	UnionFind(int N){
		this.N = N;
		par = new int[N];
		unUnite = new int[N];
		Arrays.fill(par,-1);
		Arrays.fill(unUnite,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;
		par[rootY] = rootX;
		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