ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

问题 L: Special Subsets

2021-08-04 21:34:56  阅读:237  来源: 互联网

标签:non Subsets int 样例 问题 condition contain find Special


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

标签:non,Subsets,int,样例,问题,condition,contain,find,Special
来源: https://www.cnblogs.com/jw-zhao/p/15100833.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有