首页 > 其他 > 详细

问题 L: Special Subsets

时间:2021-08-04 22:41:21      阅读:29      评论:0      收藏:0      [点我收藏+]

题目描述
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;
}

问题 L: Special Subsets

原文:https://www.cnblogs.com/jw-zhao/p/15100833.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!