codeforces 757 C. Felicity is Coming!

来源:互联网 发布:沈阳软件学院 编辑:程序博客网 时间:2024/05/17 12:54
C. Felicity is Coming!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has n gyms. The i-th gym has gi Pokemon in it. There are m distinct Pokemon types in the Himalayan region numbered from 1 to m. There is a special evolution camp set up in the fest which claims to evolve any Pokemon. The type of a Pokemon could change after evolving, subject to the constraint that if two Pokemon have the same type before evolving, they will have the same type after evolving. Also, if two Pokemon have different types before evolving, they will have different types after evolving. It is also possible that a Pokemon has the same type before and after evolving.

Formally, an evolution plan is a permutation f of {1, 2, ..., m}, such that f(x) = y means that a Pokemon of type x evolves into a Pokemon of type y.

The gym leaders are intrigued by the special evolution camp and all of them plan to evolve their Pokemons. The protocol of the mountain states that in each gym, for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. They now want to find out how many distinct evolution plans exist which satisfy the protocol.

Two evolution plans f1 and f2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon type in the two plans, i. e. there exists an i such that f1(i) ≠ f2(i).

Your task is to find how many distinct evolution plans are possible such that if all Pokemon in all the gyms are evolved, the number of Pokemon of each type in each of the gyms remains the same. As the answer can be large, output it modulo 109 + 7.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1051 ≤ m ≤ 106) — the number of gyms and the number of Pokemon types.

The next n lines contain the description of Pokemons in the gyms. The i-th of these lines begins with the integer gi (1 ≤ gi ≤ 105) — the number of Pokemon in the i-th gym. After that gi integers follow, denoting types of the Pokemons in the i-th gym. Each of these integers is between 1 and m.

The total number of Pokemons (the sum of all gi) does not exceed 5·105.

Output

Output the number of valid evolution plans modulo 109 + 7.

Examples
input
2 32 1 22 2 3
output
1
input
1 33 1 2 3
output
6
input
2 42 1 23 2 3 4
output
2
input
2 23 2 2 12 1 2
output
1
input
3 72 1 22 3 43 5 6 7
output
24
Note

In the first case, the only possible evolution plan is:

In the second case, any permutation of (1,  2,  3) is valid.

In the third case, there are two possible plans:

In the fourth case, the only possible evolution plan is:



首先为了满足第一个条件 “if two Pokemon have the same type before evolving, they will have the same type after evolving.”f应该为一个排列。

然后发现要满足第二个条件“for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. ”只有在每个gym上出现次数都相同的数x,y可以存在f[x]=y或f[y]=x。那么最终在每个gym出现次数都相同的数会成为一个个集合。设这些集合的大小为x1,x2,x3.....

显然对于一个大小为x的集合,方案数为x!,即为他们的全排列数。那么ans=x1!*x2!*x3!.....xk!


然而,重点在于如何去求解这些个集合。


考场上我选择了维护这些集合,每次将同集合内的出现同次数的元素分离出该集合建立新集合,并回收空集合下标,这样做的话,设共有K个数,那么每个元素被操作1次,理论上复杂度为O(K*log(K)*C+D),其中C和D为常数,K<=500000,按理应该可以过的,可惜我并不熟悉STL,对于处理每个gym上某数的出现次数和出现该次数的数的集合,我又用了一个set和一个map,导致C和D过大,TLE at 31。

实际上set,map是可以直接判断是否相等的,那么我们只需要开set<pair<int,int>>数组S[],S[i]存数i的信息,pair的第一个元素存出现的gym编号,第二个元素存在该gym中出现的次数。显然如果某两个数i,j在每个gym中出现次数都相等的话S[i]=S[j],那就再开一个map<set<pair<int,int>>,int>num计数,遍历num用最开始的结论求解即可。


另,get_new:读入优化的char ch一定要赋初值,否则内存若不干净的刚刚好(概率10/127)就GG了......

在cf上已经出了两回bug了......

#include<iostream>#include<algorithm>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>#include<climits>#include<queue>#include<stack>#include<map>#include<set>#define pii pair<int,int>#define N 1000020#define mod 1000000007using namespace std;typedef long long ll;inline int read(){int x=0,f=1;char ch;while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}int n,m,a[N],p[N],all;int vis[N];ll fac[N],ans=1;map<int,int>cnt;map<set<pii>,int>:: iterator it;set<pii>S[N];map<set<pii>,int>num;int main(){//freopen("in.txt","r",stdin);//freopen("my.txt","w",stdout);n=read(),m=read();for(int i=1;i<=n;i++){cnt.clear();int num=read();for(int j=1;j<=num;j++){a[j]=read();cnt[a[j]]++;}all=0;for(int j=1;j<=num;j++){if(vis[a[j]]!=i)vis[a[j]]=i,p[++all]=a[j];}for(int j=1;j<=all;j++){int x=p[j],nx=cnt[x];pii par;par.first=i,par.second=nx;S[x].insert(par);}}fac[0]=1;for(int i=1;i<=1000000;i++)fac[i]=fac[i-1]*i%mod;for(int i=1;i<=m;i++)num[S[i]]++;for(it=num.begin();it!=num.end();it++){int x=it->second;ans=ans*fac[x]%mod;}printf("%I64d \n",ans);}


代码

2 0
原创粉丝点击