結果
問題 |
No.482 あなたの名は
|
ユーザー |
![]() |
提出日時 | 2017-02-10 23:41:14 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,205 bytes |
コンパイル時間 | 2,029 ms |
コンパイル使用メモリ | 170,196 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-29 07:24:46 |
合計ジャッジ時間 | 4,004 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 WA * 9 |
ソースコード
#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(sum <= 3) { if(sum <= K && K % 2 == sum % 2) { cout << "YES" << endl; } else { cout << "NO" << endl; } } else { if(sum <= K) { cout << "YES" << endl; } else { cout << "NO" << endl; } } }