結果

問題 No.2290 UnUnion Find
ユーザー viral8viral8
提出日時 2023-05-06 11:15:25
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,098 bytes
コンパイル時間 2,609 ms
コンパイル使用メモリ 76,992 KB
実行使用メモリ 60,564 KB
最終ジャッジ日時 2024-05-03 00:42:35
合計ジャッジ時間 10,891 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 156 ms
40,992 KB
testcase_01 AC 156 ms
41,372 KB
testcase_02 AC 1,878 ms
57,932 KB
testcase_03 AC 1,919 ms
58,860 KB
testcase_04 AC 1,820 ms
60,332 KB
testcase_05 AC 1,793 ms
59,872 KB
testcase_06 AC 1,833 ms
59,688 KB
testcase_07 AC 1,923 ms
60,012 KB
testcase_08 AC 1,932 ms
59,752 KB
testcase_09 AC 1,954 ms
59,744 KB
testcase_10 AC 1,920 ms
60,060 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 1,910 ms
59,868 KB
testcase_14 AC 1,933 ms
58,920 KB
testcase_15 AC 1,778 ms
59,688 KB
testcase_16 AC 1,847 ms
59,788 KB
testcase_17 AC 1,782 ms
60,400 KB
testcase_18 AC 1,806 ms
59,264 KB
testcase_19 AC 1,910 ms
59,964 KB
testcase_20 AC 1,878 ms
60,056 KB
testcase_21 AC 1,998 ms
58,788 KB
testcase_22 AC 1,900 ms
59,272 KB
testcase_23 AC 1,952 ms
59,028 KB
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 1,904 ms
60,052 KB
testcase_27 AC 1,851 ms
60,240 KB
testcase_28 AC 1,829 ms
59,380 KB
testcase_29 AC 1,835 ms
60,168 KB
testcase_30 AC 1,957 ms
58,580 KB
testcase_31 AC 1,906 ms
59,900 KB
testcase_32 TLE -
testcase_33 AC 1,950 ms
59,560 KB
testcase_34 AC 1,843 ms
60,564 KB
testcase_35 AC 1,817 ms
59,604 KB
testcase_36 AC 1,996 ms
58,852 KB
testcase_37 AC 1,959 ms
59,472 KB
testcase_38 TLE -
testcase_39 AC 1,978 ms
59,444 KB
testcase_40 AC 1,833 ms
60,008 KB
testcase_41 AC 1,999 ms
59,320 KB
testcase_42 AC 1,859 ms
59,824 KB
testcase_43 AC 1,825 ms
59,856 KB
testcase_44 AC 1,806 ms
59,820 KB
testcase_45 AC 1,708 ms
59,580 KB
testcase_46 AC 1,726 ms
59,876 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);
		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);
				if(ans>N)
					ans = -1;
				System.out.println(ans);
			}
		}
	}
}
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