結果

問題 No.3650 Teleportation Cycles
コンテスト
ユーザー あるふぁ
提出日時 2026-08-29 08:29:17
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.90.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 75 ms / 2,000 ms
+ 990µs
コード長 837 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,471 ms
コンパイル使用メモリ 357,116 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2026-08-29 08:29:58
合計ジャッジ時間 6,723 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

struct dsu{
    vector<int> par, sz;
    dsu(int n) : par(n), sz(n, 1){
        iota(par.begin(), par.end(), 0);
    }
    int root(int x){
        if (x == par[x]) return par[x];
        return par[x] = root(par[x]);
    }
    void merge(int x, int y){
        x = root(x), y = root(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 root(x) == root(y);
    }
    int size(int x){
        return sz[root(x)];
    }
};

int main(){
    int N;
    cin >> N;
    dsu uf(N);
    for (int i = 0; i < N; i++){
        int x;
        cin >> x;
        uf.merge(i, x-1);
    }
    set<int> roots;
    for (int i = 0; i < N; i++) roots.insert(uf.root(i));
    cout << roots.size() << endl;
}
0