結果

問題 No.482 あなたの名は
ユーザー wakapaijazzwakapaijazz
提出日時 2020-10-01 01:16:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,527 bytes
コンパイル時間 2,610 ms
コンパイル使用メモリ 206,016 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2023-09-20 21:34:09
合計ジャッジ時間 4,824 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 62 ms
10,904 KB
testcase_16 AC 63 ms
11,072 KB
testcase_17 AC 62 ms
11,004 KB
testcase_18 AC 63 ms
11,004 KB
testcase_19 AC 62 ms
10,892 KB
testcase_20 AC 61 ms
11,136 KB
testcase_21 AC 62 ms
11,020 KB
testcase_22 AC 62 ms
10,944 KB
testcase_23 AC 62 ms
11,040 KB
testcase_24 AC 62 ms
10,884 KB
testcase_25 AC 63 ms
10,904 KB
testcase_26 AC 61 ms
10,936 KB
testcase_27 AC 62 ms
10,932 KB
testcase_28 AC 62 ms
11,024 KB
testcase_29 AC 52 ms
8,688 KB
testcase_30 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

struct UnionFind {
    vector<int> d;
    UnionFind(int N) : d(N, -1){}

    int root(int x) {
        if (d[x] < 0) return x;
        return d[x] = root(d[x]);
    }

    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false; 
        if (d[x] > d[y]) swap(x, y);
        d[x] += d[y];
        d[y] = x;
        return true;
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    int size(int x) {
        return -d[root(x)];
    }
};

void Main(){
    ll n, k; cin >> n >> k;
    UnionFind UF(n);
    rep(i, n){
        int d; cin >> d; d--;
        if(i != d){
            UF.unite(i, d);
        }
    }
    set<int> Root;
    rep(i, n){
        int r = UF.root(i);
        Root.insert(r);
    }
    ll ans = n - Root.size();
    if(k < ans || (k - ans) % 2) cout << "NO" << endl;
    else cout << "YES" << endl; 
    return;
}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}
0