結果

問題 No.482 あなたの名は
ユーザー treeonetreeone
提出日時 2017-02-11 01:01:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 1,305 bytes
コンパイル時間 1,275 ms
コンパイル使用メモリ 149,760 KB
実行使用メモリ 8,084 KB
最終ジャッジ日時 2023-08-28 16:17:55
合計ジャッジ時間 3,639 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 64 ms
7,796 KB
testcase_16 AC 63 ms
7,808 KB
testcase_17 AC 64 ms
7,900 KB
testcase_18 AC 64 ms
7,736 KB
testcase_19 AC 64 ms
7,804 KB
testcase_20 AC 64 ms
7,800 KB
testcase_21 AC 64 ms
7,880 KB
testcase_22 AC 64 ms
7,804 KB
testcase_23 AC 63 ms
7,732 KB
testcase_24 AC 65 ms
7,736 KB
testcase_25 AC 64 ms
8,056 KB
testcase_26 AC 63 ms
7,776 KB
testcase_27 AC 62 ms
8,084 KB
testcase_28 AC 62 ms
7,856 KB
testcase_29 AC 60 ms
7,740 KB
testcase_30 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
using namespace std;
typedef pair<int, int> P;

int cnt = 0;
int a[200010];

struct UF{
    vector<int> par;
    vector<int> sz;
    UF(){}
    UF(int n){
        par.resize(n);
        sz.resize(n, 1);
        rep(i, 0, n) par[i] = i;
    }
    int find(int x){
        if(x == par[x]) return x;
        return par[x] = find(par[x]);
    }
    void unite(int x, int y){
        x = find(x); y = find(y);
        if(x == y) return;
        if(sz[x] < sz[y]) swap(x, y);
        sz[x] += sz[y];
        par[y] = x;
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};

signed main(){
    int n, k;
    cin >> n >> k;
    UF uf(n);
    rep(i, 0, n){
        cin >> a[i];
        a[i]--;
    }
    rep(i, 0, n){
        if(a[i] == i) continue;
        uf.unite(i, a[i]);
    }
    int cnt = 0;
    rep(i, 0, n){
        if(uf.find(i) != i) continue;
        cnt += uf.size(i) - 1;
    }
    bool f = true;
    if(cnt > k) f = false;
    else if((k - cnt) % 2 == 1) f = false;
    cout << (f ? "YES" : "NO") << endl;
}
0