結果

問題 No.482 あなたの名は
ユーザー ldsyb
提出日時 2017-06-22 19:38:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 842 bytes
コンパイル時間 891 ms
コンパイル使用メモリ 73,684 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-02 12:20:23
合計ジャッジ時間 3,077 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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