結果

問題 No.482 あなたの名は
ユーザー atkrymatkrym
提出日時 2017-02-19 13:50:44
言語 Java
(openjdk 23)
結果
AC  
実行時間 878 ms / 2,000 ms
コード長 724 bytes
コンパイル時間 2,803 ms
コンパイル使用メモリ 77,204 KB
実行使用メモリ 55,356 KB
最終ジャッジ日時 2024-12-30 05:02:43
合計ジャッジ時間 19,734 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	private static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		long k = sc.nextLong();
		int[] l = new int[200010];
		boolean[] b = new boolean[200010];
		for (int i = 1;i <= n;i++) {
			l[i] = sc.nextInt();
		}

		int r = 0;
		for (int i = 1;i <= n;i++) {
			if (b[i]) continue;
			int s = i;
			int j = l[i];
			int c = 0;
			b[i] = true;
			while (true) {
				if (j==s) break;
				b[j] = true;
				j = l[j];
				c++;
			}
			r += c;
			b[i] = true;
		}

		if (r<=k) {
			if ((k-r)%2==0) {
				System.out.println("YES");
			} else {
				System.out.println("NO");
			}
		} else {
			System.out.println("NO");
		}
	}
}
0