C - Sleep Buddies Gym - 101063C 状压

Posted poler

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C - Sleep Buddies Gym - 101063C 状压相关的知识,希望对你有一定的参考价值。

C - Sleep Buddies Gym - 101063C 

题目链接:

https://vjudge.net/problem/473487/origin

Description:

It is nighttime in the Earth Colony on Mars and everyone is getting ready to sleep. It is common to sleep in pairs, so that if anything were to happen during the night people could aid each other.

To decide who is a suitable candidate to sleep with whom, the leaders of GEMA asked everyone to answer a questionnaire about attributes they desire their partner to have from a list of M possible items.

To choose the pairs, GEMA uses the Jaccard index between the desired attributes of both persons. The Jaccard index for two sets A and B is defined as , that is, the size of the intersection between A and B divided by the size of their union. They allow a pair to be formed if the Jaccard index of the two attribute sets for the pair is at least K.

Thanks to GEMA, there are too many people living on Mars these days. Help the high commanders decide how many allowed pairs there are out of the N people living there.

Input

The input begins with two integers, N and M (1 ≤ N ≤ 1051 ≤ M ≤ 10), the number of people on Mars and the number of possible attributes on the questionnaire.

Then follow N lines, each beginning with an integer Q (1 ≤ Q ≤ M), the number of desired attributes on the list of the i-th person. Then follow Q integers q (1 ≤ q ≤ M), encoding these attributes. There numbers are all different.

The last line of input contains a real number K (0 ≤ K ≤ 1), the minimum required Jaccard index for a pair.

Output

Output the number of pairs that are allowed.

Example

Input
2 5
2 1 3
5 3 1 5 4 2
0.66489
Output
0
Input
3 1
1 1
1 1
1 1
0.85809
Output
3
题意:
N个人,M种属性,每个人有Qi个性属性,分pair要求两个人的

>=K,问有多少对人满足。
Hint:
M只有10,考虑状压,二进制位保存没有这种属性。
AC代码:
#include<bits/stdc++.h>
#define LL long long
#define maxn 100010
#define eps 1e-6
using namespace std;
int n,m,q,Q;
LL vis[maxn];
LL num[maxn];
double k;
set<int> s;
vector<int> G[20];
int solve(int x){
    int res=0;
    while(x>0){
        if(x&1)
            res++;
        x>>=1;
    }
    return res;
}
int main(){
    cin>>n>>m;
    int now=0,Q;
    for(int i=1;i<=n;i++){
        scanf("%d",&Q);
        now=0;    
        for(int j=1;j<=Q;j++){
            scanf("%d",&q);
            now+=(1<<(q-1));
        }    
        s.insert(now);
        vis[now]++;
    }
    cin>>k;
    LL ans=0;
    LL aa,bb;
    for(int i=0;i<(1<<10);i++){
        if(vis[i]==0) continue;
        for(int j=i+1;j<(1<<10);j++){
            if(vis[j]==0) continue;
            now=(i&j);
            aa=solve(now);
            bb=solve(i)+solve(j)-solve(now);
            if((double)aa/bb>=k){
                ans+=(vis[i]*vis[j]);
            }
        }
        aa=vis[i];
        if(aa>=2){
            ans+=(aa*(aa-1))/2;
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

 

 

以上是关于C - Sleep Buddies Gym - 101063C 状压的主要内容,如果未能解决你的问题,请参考以下文章

Sleep Buddies (二进制状态压缩)

ElasticSearch嵌套查询无法按预期工作

GYM - 101147 C.The Wall

gym 102392 C

Gym-101630C:Connections(生成树&构造)

C - Insertion Sort Gym - 101955C