結果

問題 No.482 あなたの名は
ユーザー ldsybldsyb
提出日時 2017-06-22 19:38:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 842 bytes
コンパイル時間 714 ms
コンパイル使用メモリ 73,776 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 10:31:08
合計ジャッジ時間 3,961 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

struct UF{
	vector<int> par, rank;

	UF(int n){
		for(int i = 0; i < n; i++){
			par.emplace_back(i);
		}
		rank.assign(n, 0);
	}

	int root(int x){
		return par[x] == x ? x : par[x] = root(par[x]);
	}

	bool same(int x, int y){
		return root(x) == root(y);
	}

	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if(rank[x] < rank[y]) par[x] = y;
		else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}
};

int main(){
	using int64 = int64_t;
	int64 n, k;
	cin >> n >> k;
	UF uf(n);
	for(int i = 0; i < n; i++){
		int d;
		cin >> d;
		uf.unite(i, d - 1);
	}
	int64 a[n]{};
	for(int i = 0; i < n; i++) a[uf.root(i)]++;
	int64 count = 0;
	for(int i = 0; i < n; i++) if(a[i]) count += a[i] - 1;
	cout << (count <= k && (k - count) % 2 == 0 ? "YES" : "NO") << endl;
}
0