问题 L: Special Subsets

题目描述
Let S be a set composed of all integers from 1 through N.
f is a function from S to S. You are given the values f(1),f(2),⋯,f(N) as f1,f2,⋯,fN.

Find the number, modulo 998244353, of non-empty subsets T of S satisfying both of the following conditions:

For every a∈T, f(a)∈T.

For every a,b∈T, f(a)≠f(b) if a≠b.

Constraints
1≤N≤2×105
1≤fi≤N
All values in input are integers.
输入
Input is given from Standard Input in the following format:
N
f1 f2 … fN
输出
Print the number of non-empty subsets of S satisfying both of the conditions, modulo 998244353.
样例输入 Copy
【样例1】
2
2 1
【样例2】
2
1 1
【样例3】
3
1 2 3
样例输出 Copy
【样例1】
1
【样例2】
1
【样例3】
7
提示
样例1解释
We have f(1)=2,f(2)=1. Since f(1)≠f(2), the second condition is always satisfied, but the first condition requires T to contain 1 and 2 simultaneously.
样例2解释
We have f(1)=f(2)=1. The first condition requires T to contain 1, and the second condition forbids T to contain 2.
样例3解释
We have f(1)=1,f(2)=2,f(3)=3. Both of the conditions are always satisfied, so all non-empty subsets of T count.
题目说明:
对于集合T, 如果存在i属于T, 那么必须f(i)属于T
如果f(a) != f(b) 那么必须a != b
思路:如果i在集合里面, 那么f(i)一定在集合里面, 构造一条i->f(i)的边

每一个点都有一个出边, 这个图一定会出现环, 输出环的个数2^n - 1

#include<iostream>
using namespace std;
const int N = 2e5 + 10;
int p[N];
int n;
const int mod = 998244353;
int find(int a)
{
    if(p[a] != a)p[a] = find(p[a]);
    return p[a];
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i = 1; i <= n; i ++)p[i] = i;
    for(int i = 1; i <= n; i ++)
    {
        int x;
        cin >> x;
        p[find(x)] = find(i);
    }
    int res = 1;
    for(int i = 1; i <= n; i ++)
    {
        if(p[i] == i)res = res * 2 % mod;
    }
    cout << res - 1 << endl;
    return 0;
}
上一篇:[笔记] @JsonInclude 注解


下一篇:SyntaxError: Non-UTF-8 code starting with ‘\xce‘的一种解决方案