結果

問題 No.2290 UnUnion Find
ユーザー viral8viral8
提出日時 2023-05-06 11:22:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,076 ms / 2,000 ms
コード長 1,185 bytes
コンパイル時間 1,908 ms
コンパイル使用メモリ 77,480 KB
実行使用メモリ 61,516 KB
最終ジャッジ日時 2024-05-03 00:46:31
合計ジャッジ時間 52,499 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
40,132 KB
testcase_01 AC 127 ms
41,232 KB
testcase_02 AC 822 ms
59,808 KB
testcase_03 AC 995 ms
59,468 KB
testcase_04 AC 1,006 ms
61,304 KB
testcase_05 AC 1,058 ms
59,896 KB
testcase_06 AC 1,063 ms
60,072 KB
testcase_07 AC 998 ms
61,360 KB
testcase_08 AC 1,010 ms
61,084 KB
testcase_09 AC 993 ms
61,040 KB
testcase_10 AC 1,030 ms
60,976 KB
testcase_11 AC 1,010 ms
61,096 KB
testcase_12 AC 1,050 ms
60,956 KB
testcase_13 AC 1,064 ms
60,968 KB
testcase_14 AC 1,074 ms
61,412 KB
testcase_15 AC 1,052 ms
60,884 KB
testcase_16 AC 1,030 ms
60,932 KB
testcase_17 AC 969 ms
61,516 KB
testcase_18 AC 1,008 ms
61,168 KB
testcase_19 AC 1,054 ms
59,668 KB
testcase_20 AC 1,018 ms
60,160 KB
testcase_21 AC 891 ms
59,256 KB
testcase_22 AC 939 ms
59,624 KB
testcase_23 AC 983 ms
59,104 KB
testcase_24 AC 1,071 ms
60,452 KB
testcase_25 AC 980 ms
59,504 KB
testcase_26 AC 1,013 ms
60,228 KB
testcase_27 AC 1,061 ms
59,988 KB
testcase_28 AC 1,006 ms
59,292 KB
testcase_29 AC 1,041 ms
60,040 KB
testcase_30 AC 942 ms
60,564 KB
testcase_31 AC 985 ms
59,844 KB
testcase_32 AC 888 ms
58,804 KB
testcase_33 AC 1,076 ms
58,720 KB
testcase_34 AC 1,057 ms
60,492 KB
testcase_35 AC 980 ms
60,024 KB
testcase_36 AC 995 ms
58,952 KB
testcase_37 AC 955 ms
59,992 KB
testcase_38 AC 980 ms
59,856 KB
testcase_39 AC 1,006 ms
59,368 KB
testcase_40 AC 1,029 ms
60,284 KB
testcase_41 AC 933 ms
59,384 KB
testcase_42 AC 1,060 ms
58,984 KB
testcase_43 AC 1,051 ms
59,844 KB
testcase_44 AC 1,017 ms
60,096 KB
testcase_45 AC 1,042 ms
59,800 KB
testcase_46 AC 1,011 ms
59,580 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[] par,unUnite;
	UnionFind(int 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;
		check(rootY);
		check(rootX);
	}
	void check(int x){
		int root = root(x);
		while(unUnite[root]<unUnite.length&&root==root(unUnite[root]))
			unUnite[root]++;
	}
	int unUnitePoint(int x){
		return unUnite[root(x)];
	}
}
0