Sleep Buddies
Sleep Buddies
2.0 s
256 MB
standard input
standard output
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 andB 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.
The input begins with two integers, N and M (1 ≤ N ≤ 105, 1 ≤ 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 the number of pairs that are allowed.
Examples
2 5
2 1 3
5 3 1 5 4 2
0.66489
0
3 1
1 1
1 1
1 1
0.85809
output
3
//题意: n 个集合,全集为 1 -- m 的整数,然后一个比率 k , 然后问,每两个的集合交集元素个数除并集元素个数大于 k 的个数有多少
//注意到,全集 m 非常小,所以,可以用二进制来表示某个数有没有,这样,每个数都能代表一个集合,最多也就 2^10 = 1024 个
统计数量,暴力二重循环计数即可,又犯了个错,浮点数的大于等于,一定要理解!!!
1 # include <cstdio> 2 # include <cstring> 3 # include <cstdlib> 4 # include <iostream> 5 # include <vector> 6 # include <queue> 7 # include <stack> 8 # include <map> 9 # include <bitset> 10 # include <sstream> 11 # include <set> 12 # include <cmath> 13 # include <algorithm> 14 using namespace std; 15 # define LL long long 16 # define pr pair 17 # define mkp make_pair 18 # define lowbit(x) ((x)&(-x)) 19 # define PI acos(-1.0) 20 # define INF 0x3f3f3f3f3f3f3f3f 21 # define eps 1e-9 22 # define MOD 1000000007 23 24 inline int scan() { 25 int x=0,f=1; char ch=getchar(); 26 while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} 27 while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} 28 return x*f; 29 } 30 inline void Out(int a) { 31 if(a<0) {putchar('-'); a=-a;} 32 if(a>=10) Out(a/10); 33 putchar(a%10+'0'); 34 } 35 # define MX 1050 36 /**************************/ 37 38 int n,m; 39 int st[MX]; 40 41 LL C(LL a,LL b) 42 { 43 if (b<2) return 0; 44 return b*(b-1)/2; 45 } 46 47 double gai(int a,int b) 48 { 49 int all =0 ; 50 int jiao =0; 51 for (int i=0;i<m;i++) 52 { 53 if ( (a&(1<<i)) || (b&(1<<i)) ) all++; 54 if ( (a&(1<<i)) && (b&(1<<i)) ) jiao++; 55 } 56 return (jiao*1.0)/all; 57 } 58 59 int main() 60 { 61 62 while (scanf("%d%d",&n,&m)!=EOF) 63 { 64 memset(st,0,sizeof(st)); 65 for (int i=1;i<=n;i++) 66 { 67 int num; 68 scanf("%d",&num); 69 int sb=0; 70 for (int j=1;j<=num;j++) 71 { 72 int x; 73 scanf("%d",&x); 74 sb = (sb ^ (1<<(x-1))); 75 } 76 st[sb]++; 77 } 78 double p; 79 scanf("%lf",&p); 80 81 LL ans = 0; 82 int ut = (1<<m); 83 for (int i=1;i<ut;i++) 84 { 85 for (int j=i;j<ut;j++) 86 { 87 if (gai(i,j)-p>-eps) 88 { 89 if (i==j) ans = ans+C(2,st[i]); 90 else ans = ans+((LL)st[i])*st[j]; 91 } 92 } 93 } 94 cout<<ans<<endl; 95 } 96 return 0; 97 }