結果

問題 No.100 直列あみだくじ
ユーザー scachescache
提出日時 2014-12-12 00:28:25
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,491 bytes
コンパイル時間 3,204 ms
コンパイル使用メモリ 74,504 KB
実行使用メモリ 58,472 KB
最終ジャッジ日時 2023-09-02 14:14:30
合計ジャッジ時間 10,875 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,696 KB
testcase_01 AC 126 ms
55,992 KB
testcase_02 AC 123 ms
55,440 KB
testcase_03 AC 128 ms
55,468 KB
testcase_04 WA -
testcase_05 AC 121 ms
55,468 KB
testcase_06 AC 121 ms
55,540 KB
testcase_07 AC 122 ms
55,680 KB
testcase_08 AC 120 ms
55,760 KB
testcase_09 AC 120 ms
55,452 KB
testcase_10 AC 123 ms
55,992 KB
testcase_11 AC 122 ms
56,196 KB
testcase_12 AC 124 ms
55,932 KB
testcase_13 AC 127 ms
55,460 KB
testcase_14 AC 122 ms
56,340 KB
testcase_15 AC 122 ms
55,996 KB
testcase_16 AC 119 ms
56,392 KB
testcase_17 AC 121 ms
55,960 KB
testcase_18 AC 121 ms
55,904 KB
testcase_19 AC 122 ms
56,076 KB
testcase_20 AC 122 ms
55,716 KB
testcase_21 AC 122 ms
55,836 KB
testcase_22 WA -
testcase_23 AC 120 ms
55,572 KB
testcase_24 AC 122 ms
55,844 KB
testcase_25 AC 121 ms
55,832 KB
testcase_26 AC 122 ms
55,452 KB
testcase_27 AC 124 ms
53,960 KB
testcase_28 AC 123 ms
56,336 KB
testcase_29 AC 119 ms
55,708 KB
testcase_30 AC 126 ms
55,804 KB
testcase_31 AC 123 ms
55,756 KB
testcase_32 WA -
testcase_33 AC 123 ms
55,800 KB
testcase_34 AC 120 ms
55,680 KB
testcase_35 AC 125 ms
56,204 KB
testcase_36 AC 126 ms
55,896 KB
testcase_37 AC 126 ms
55,972 KB
testcase_38 AC 127 ms
55,684 KB
testcase_39 AC 128 ms
56,292 KB
testcase_40 AC 125 ms
55,680 KB
testcase_41 AC 128 ms
56,356 KB
testcase_42 AC 128 ms
55,744 KB
testcase_43 AC 129 ms
55,784 KB
testcase_44 WA -
testcase_45 AC 125 ms
55,724 KB
testcase_46 AC 127 ms
55,472 KB
testcase_47 AC 128 ms
55,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;


public class Main0100 {
	public static void main(String[] args) {
		Main0100 p = new Main0100();
	}

	public Main0100() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < a.length; i++)
			a[i] = sc.nextInt()-1;
		solve(n, a);
	}

	public void solve(int n, int[] a) {
		boolean res = true;
		uf(n);
		for(int i=0;i<a.length;i++){
			unite(i, a[i]);
		}
		
		int gc = 0;
		boolean[] isVisit = new boolean[n];
		for(int i=0;i<n;i++){
			if(isVisit[i])
				continue;
			
			int c = 0;
			isVisit[i] = true;
			for(int j=0;j<n;j++){
				if(same(i, j)){
					isVisit[j] = true;
					c++;
				}
			}
			if(c%2==0){
				gc++;
			}
		}
		
		System.out.println(gc%2==0 ? "Yes" : "No");
	}
	
	
	int[] par;
	int[] rank;
	private void uf(int n){
		par = new int[n];
		rank = new int[n];
		for(int i=0;i<n;i++){
			par[i] = i;
			rank[i] = 0;
		}
	}
	
	// 木の根を返す
	private int find(int x){
		if(par[x] == x)
			return x;
		else
			return par[x] =find(par[x]);
	}
	
	// 併合
	private void unite(int x, int y){
		x = find(x);
		y = find(y);
		
		// すでに同じグループ
		if(x==y)
			return;
		
		if(rank[x]<rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x]==rank[y])
				rank[x]++;
		}
	}
	
	// 同じグループなら真
	private boolean same(int x, int y){
		return find(x) == find(y);
	}	
}
0