結果

問題 No.556 仁義なきサルたち
ユーザー fal_rndfal_rnd
提出日時 2017-08-11 23:12:34
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,396 bytes
コンパイル時間 2,323 ms
コンパイル使用メモリ 84,692 KB
実行使用メモリ 48,384 KB
最終ジャッジ日時 2024-04-20 23:28:22
合計ジャッジ時間 8,170 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 128 ms
41,412 KB
testcase_02 WA -
testcase_03 AC 115 ms
40,436 KB
testcase_04 AC 113 ms
40,020 KB
testcase_05 AC 164 ms
41,456 KB
testcase_06 WA -
testcase_07 AC 156 ms
41,632 KB
testcase_08 AC 146 ms
41,904 KB
testcase_09 AC 192 ms
42,556 KB
testcase_10 WA -
testcase_11 AC 212 ms
42,716 KB
testcase_12 WA -
testcase_13 AC 244 ms
43,156 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 367 ms
48,024 KB
testcase_20 AC 365 ms
48,312 KB
testcase_21 AC 361 ms
48,284 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());}

	static final double finf=0.22941573387056172;
	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(a<b)
					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