結果

問題 No.482 あなたの名は
ユーザー femtofemto
提出日時 2017-02-11 00:37:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 924 bytes
コンパイル時間 1,453 ms
コンパイル使用メモリ 167,872 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 16:16:36
合計ジャッジ時間 3,205 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 21 ms
4,380 KB
testcase_16 AC 22 ms
4,376 KB
testcase_17 AC 21 ms
4,380 KB
testcase_18 AC 21 ms
4,376 KB
testcase_19 AC 21 ms
4,376 KB
testcase_20 AC 21 ms
4,380 KB
testcase_21 AC 21 ms
4,380 KB
testcase_22 AC 21 ms
4,376 KB
testcase_23 AC 21 ms
4,380 KB
testcase_24 AC 21 ms
4,376 KB
testcase_25 AC 21 ms
4,380 KB
testcase_26 AC 21 ms
4,376 KB
testcase_27 AC 21 ms
4,376 KB
testcase_28 AC 21 ms
4,380 KB
testcase_29 AC 19 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct UnionFind {
	vector<int> par;
	int cnt;
	UnionFind(int size_) : par(size_, -1), cnt(size_) {}
	void unite(int x, int y) {
		if((x = find(x)) != (y = find(y))) {
			if(par[y] < par[x]) swap(x, y);
			par[x] += par[y]; par[y] = x; cnt--;
		}
	}
	bool same(int x, int y) { return find(x) == find(y); }
	int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); }
	int size(int x) { return -par[find(x)]; }
	int size() { return cnt; }
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll N, K;
	cin >> N >> K;

	UnionFind uf(N);
	ll sum = 0;
	for(int i = 0; i < N; i++) {
		int d;
		cin >> d;
		d--;
		if(i != d) {
			uf.unite(i, d);
		}
	}

	for(int i = 0; i < N; i++) {
		if(uf.find(i) == i) {
			sum += uf.size(i) - 1;
		}
	}

	if(sum <= K && (K - sum) % 2 == 0) {
		cout << "YES" << endl;
	}
	else {
		cout << "NO" << endl;
	}

}
0