結果

問題 No.556 仁義なきサルたち
ユーザー fal_rndfal_rnd
提出日時 2017-08-11 23:18:44
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,403 bytes
コンパイル時間 3,534 ms
コンパイル使用メモリ 82,940 KB
実行使用メモリ 98,964 KB
最終ジャッジ日時 2024-04-20 23:34:52
合計ジャッジ時間 15,759 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
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 OLE -
testcase_15 OLE -
testcase_16 OLE -
testcase_17 OLE -
testcase_18 OLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

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(b))
					uf.connect(a,b);
				else
					uf.connect(b,a);
			}
			System.out.println(uf);
		}
		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;
		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)];
	}
	@Override
	public String toString(){
		return Arrays.toString(uni);
	}
}
0