Submission #20967077


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, l, r) for (int i = l; i <= (int)(r); i++)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define MOD (int) (998244353)
#define INF (int) 1e9
#define LLINF (ll) 1e18
#define MAX_N (int) 200005


struct UnionFind{
    ll par[MAX_N]; // 親
    ll rank[MAX_N]; // 木の深さ
    ll siz[MAX_N]; // 素集合のサイズ

    // n要素で初期化
    void init(ll n){
        for(ll i=0;i<n;i++){
            par[i] = i;
            rank[i] = 0;
            siz[i] = 1;
        }
    }

    // 木の根を求める
    ll root(ll x){
        if(par[x] == x){
            return x;
        }else{
            return par[x] = root(par[x]);
        }
    }

    // xとyの属する集合を併合
    bool unite(ll x, ll y){
        x = root(x);
        y = root(y);
        if(x==y) return false; // 併合失敗
        if(rank[x] < rank[y]){
            par[x] = y;
            siz[y] += siz[x];
        }else{
            par[y] = x;
            siz[x] += siz[y];
            if(rank[x] == rank[y]) rank[x]++;
        }
        return true; // 併合成功
    }

    bool issame(ll x, ll y){
        return root(x)==root(y);
    }

    ll size(ll x){
        return siz[root(x)];
    }
};

ll modpow(ll a, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

int main(){
	int n;
	cin >> n;

	UnionFind uf;
	uf.init(n);

	rep(i, n){
		int f;
		cin >> f;
		uf.unite(i, f-1);
	}

	set<int> s;
	rep(i, n){
		s.insert(uf.root(i));
	}

	cout << (modpow(2, s.size())-1+MOD)%MOD << endl;
	// printf("%d\n", N);
	return 0;
}

Submission Info

Submission Time
Task B - Special Subsets
User pontsuyo
Language C++ (GCC 9.2.1)
Score 400
Code Size 1928 Byte
Status AC
Exec Time 100 ms
Memory 17504 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 25
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 5 ms 3584 KiB
02.txt AC 6 ms 3584 KiB
03.txt AC 3 ms 3612 KiB
04.txt AC 3 ms 3512 KiB
05.txt AC 2 ms 3612 KiB
06.txt AC 75 ms 8132 KiB
07.txt AC 75 ms 8180 KiB
08.txt AC 70 ms 8260 KiB
09.txt AC 2 ms 3648 KiB
10.txt AC 100 ms 17504 KiB
11.txt AC 71 ms 8184 KiB
12.txt AC 75 ms 8288 KiB
13.txt AC 74 ms 8192 KiB
14.txt AC 71 ms 8308 KiB
15.txt AC 76 ms 8108 KiB
16.txt AC 71 ms 8288 KiB
17.txt AC 97 ms 16372 KiB
18.txt AC 86 ms 11996 KiB
19.txt AC 76 ms 8796 KiB
20.txt AC 78 ms 8904 KiB
21.txt AC 57 ms 8288 KiB
22.txt AC 58 ms 8184 KiB
s1.txt AC 2 ms 3428 KiB
s2.txt AC 4 ms 3532 KiB
s3.txt AC 3 ms 3456 KiB