結果

問題 No.556 仁義なきサルたち
ユーザー Daigo HIROOKADaigo HIROOKA
提出日時 2018-06-27 00:07:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,435 ms / 2,000 ms
コード長 1,242 bytes
コンパイル時間 3,562 ms
コンパイル使用メモリ 74,836 KB
実行使用メモリ 66,348 KB
最終ジャッジ日時 2023-09-13 14:16:50
合計ジャッジ時間 16,070 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,936 KB
testcase_01 AC 135 ms
55,884 KB
testcase_02 AC 127 ms
55,724 KB
testcase_03 AC 130 ms
56,012 KB
testcase_04 AC 131 ms
55,892 KB
testcase_05 AC 158 ms
56,236 KB
testcase_06 AC 160 ms
55,644 KB
testcase_07 AC 181 ms
55,892 KB
testcase_08 AC 201 ms
56,024 KB
testcase_09 AC 253 ms
59,108 KB
testcase_10 AC 269 ms
59,140 KB
testcase_11 AC 265 ms
60,688 KB
testcase_12 AC 279 ms
61,440 KB
testcase_13 AC 324 ms
58,128 KB
testcase_14 AC 474 ms
61,384 KB
testcase_15 AC 621 ms
61,356 KB
testcase_16 AC 675 ms
61,716 KB
testcase_17 AC 1,223 ms
66,348 KB
testcase_18 AC 1,430 ms
65,984 KB
testcase_19 AC 1,435 ms
65,868 KB
testcase_20 AC 1,356 ms
66,076 KB
testcase_21 AC 1,272 ms
66,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class No556{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int m = sc.nextInt();
		ArrayList<Integer> monkeys = new ArrayList<Integer>(n);
		for(int i = 0; i < n; i++){
			monkeys.add(i+1);
		}
		for(int i = 0; i < m; i++){
			int a = sc.nextInt();
			int b = sc.nextInt();

			int boss_a = monkeys.get(a-1);
			int boss_b = monkeys.get(b-1);
			int num_teama = 0, num_teamb = 0;
			for(int j = 0; j < n; j++){
				if(monkeys.get(j) == boss_a) num_teama++;
				if(monkeys.get(j) == boss_b) num_teamb++;
			}

			if(num_teama > num_teamb){
				for(int j = 0; j < n; j++){
					if(monkeys.get(j) == boss_b) monkeys.set(j, boss_a);
				}
			}else if(num_teamb > num_teama){
				for(int j = 0; j < n; j++){
					if(monkeys.get(j) == boss_a) monkeys.set(j, boss_b);
				}
			}else if(boss_a < boss_b){
				for(int j = 0; j < n; j++){
					if(monkeys.get(j) == boss_b) monkeys.set(j, boss_a);
				}
			}else if(boss_b < boss_a){
				for(int j = 0; j < n; j++){
					if(monkeys.get(j) == boss_a) monkeys.set(j, boss_b);
				}
			}
			// System.out.println(monkeys.toString());
		}

		for(int i = 0; i < n; i++){
			System.out.println(monkeys.get(i));
		}
	}
}
0