結果

問題 No.482 あなたの名は
ユーザー femtofemto
提出日時 2017-02-10 23:42:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,204 bytes
コンパイル時間 1,546 ms
コンパイル使用メモリ 168,516 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-28 10:16:56
合計ジャッジ時間 3,351 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 22 ms
4,380 KB
testcase_18 AC 22 ms
4,380 KB
testcase_19 WA -
testcase_20 AC 23 ms
4,380 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 23 ms
4,376 KB
testcase_25 AC 23 ms
4,376 KB
testcase_26 AC 23 ms
4,380 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 20 ms
4,384 KB
testcase_30 AC 1 ms
4,380 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);
		}
	}

	bool f = false;
	for(int i = 0; i < N; i++) {
		if(uf.find(i) == i) {
			sum += uf.size(i) - 1;
			if(uf.size(i) >= 4) f = true;
		}
	}

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


}
0