結果

問題 No.556 仁義なきサルたち
ユーザー fal_rndfal_rnd
提出日時 2017-08-11 23:20:01
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,368 bytes
コンパイル時間 2,643 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 61,380 KB
最終ジャッジ日時 2024-04-20 23:37:17
合計ジャッジ時間 9,219 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 132 ms
41,148 KB
testcase_03 AC 125 ms
40,968 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 374 ms
48,428 KB
testcase_20 AC 382 ms
48,532 KB
testcase_21 AC 406 ms
46,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main{
	static IntStream REPS(int v){return IntStream.range(0,v);}
	static IntStream REPS(int l,int r){return IntStream.rangeClosed(l,r);}
	static IntStream INS(int n) {return REPS(n).map(i->getInt());}
	static Scanner s=new Scanner(System.in);
	static int getInt(){return Integer.parseInt(s.next());}

	public static void main(String[]$){
		int n=getInt(),q=getInt();
		UF uf=new UF(n);

		for(int i=0;i<q;++i) {
			int a=getInt()-1,b=getInt()-1;
			int ga=uf.groupSize(a),gb=uf.groupSize(b);
			if(ga<gb){
				uf.connect(b,a);
			}else if(ga>gb){
				uf.connect(a,b);
			}else {
				if(uf.root(a)<uf.root(gb))
					uf.connect(a,b);
				else
					uf.connect(b,a);
			}
		}
		REPS(n).map(i->uf.root(i)+1).forEach(System.out::println);
	}
}
class UF{
	public int[] uni;
	public UF(int size){
		uni=new int[size];
		Arrays.fill(uni,-1);
	}
	public int root(int t){
		if(uni[t]<0)
			return t;
		return uni[t]=root(uni[t]);
	}
	public boolean connect(int p,int c){
		p=root(p);
		c=root(c);
		if(isConnected(p,c))
			return false;
		if(uni[p]>uni[c]){
			final int buf=c;
			c=p;
			p=buf;
		}
		uni[p]+=uni[c];
		uni[c]=p;
		return true;
	}
	public boolean isConnected(int a,int b){
		return root(a)==root(b);
	}
	public int groupSize(int t){
		return -uni[root(t)];
	}
}
0