結果
問題 | No.2289 順列ソート |
ユーザー |
|
提出日時 | 2023-05-05 21:33:41 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 910 bytes |
コンパイル時間 | 1,985 ms |
コンパイル使用メモリ | 196,564 KB |
最終ジャッジ日時 | 2025-02-12 17:38:03 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;using Vi = std::vector<int> ;using Vl = vector<ll>;class UnionFind {Vi par;Vi siz;Vl val;public :UnionFind(int N = 0) {par.resize(N);siz.resize(N);iota(par.begin(), par.end(), 0);fill(siz.begin(), siz.end(), 0);}void init(int N) {par.resize(N);siz.resize(N);iota(par.begin(), par.end(), 0);fill(siz.begin(), siz.end(), 0);}int root(int v) {if (v == par[v]) {return v;} else {return par[v] = root(par[v]);}}bool unit(int a, int b) {a = root(a);b = root(b);if (a == b) {return false;}if (siz[a] < siz[b]) {swap(a, b);}par[b] = a;siz[a] += siz[b];return true;}};int main () {int N;cin >> N;UnionFind uu(N);int ans = 0;for (int i = 0; i < N; i ++) {int p;cin >> p;p --;ans += uu.unit(p, i);}cout << ans << endl;}