GT and set
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 39 Accepted Submission(s): 17
Problem Description
You are given
N
sets.The
i−
th set has
Ai
numbers.
You should divide the sets into L parts.
And each part should have at least one number in common.
If there is at least one solution,print YES ,otherwise print NO .
You should divide the sets into L parts.
And each part should have at least one number in common.
If there is at least one solution,print YES ,otherwise print NO .
Input
In the first line there is the testcase
T
(
T
≤
20
)
For each teatcase:
In the first line there are two numbers N and L .
In the next N lines,each line describe a set.
The first number is Ai ,and then there are Ai distict numbers stand for the elements int the set.
The numbers in the set are all positive numbers and they're all not bigger than 300 .
1≤ N ≤30 , 1≤ L≤5 , 1≤ Ai ≤10 , 1≤L≤N
You'd better print the enter in the last line when you hack others.
You'd better not print space in the last of each line when you hack others.
For each teatcase:
In the first line there are two numbers N and L .
In the next N lines,each line describe a set.
The first number is Ai ,and then there are Ai distict numbers stand for the elements int the set.
The numbers in the set are all positive numbers and they're all not bigger than 300 .
1≤ N ≤30 , 1≤ L≤5 , 1≤ Ai ≤10 , 1≤L≤N
You'd better print the enter in the last line when you hack others.
You'd better not print space in the last of each line when you hack others.
Output
For each test print
YES
or
NO
Sample Input
2 2 1 1 1 1 2 3 2 3 1 2 3 3 4 5 6 3 2 5 6
Sample Output
NO YESHintFor the second test,there are three sets:{1,2,3},{4,5,6},{2,5,6} You are asked to divide into two parts. One possible solution is to put the second and the third sets into the same part,and put the first in the other part. The second part and the third part have same number 6. Another solution is to put the first and the third sets into the same part,and put the second in the other part.
Source
Recommend
解析:摘自BC官方题解:
可能一些选手题意不是很清楚,我这里再提供一个转化后的问题:
给出N个集合。每次你可以指定一个数,然后所有包含这个元素的集合可以被删掉。
问你能否经过最多L轮操作使得所有集合都被删掉。
因为L只有5,考虑直接dfs。
对于第一个集合,我们枚举它的那个公共的数是多少。
然后线性扫描过去,找到接下来第一个没有这个数的集合。
(它显然不能通过这个公共的数与第一个数在同一个部分)
对于这个集合,再枚举它公共的数是多少,然后线性扫描过去找到第一个没有这两个数的集合……
这样重复5次后如果还是没有,就直接NO好了。若中途扫完序列就是YES。
这样最坏的效率就是O(N∗105),足以通过此题。
其实我们可以预处理使得效率变成O(105) ><然而出题人很良(lan)心(duo)。
代码:
#include<cstdio>
#include<cstring>
#include<cctype>
#define ms0(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=30;
const int maxa=10;
int n,l,a[maxn+10][maxa+10];
bool f[maxn+10][maxn*maxa+10];
int q[10];
int getin()
{
int ans=0;char tmp;
while(!isdigit(tmp=getchar()));
do ans=(ans<<3)+(ans<<1)+tmp-'0';
while(isdigit(tmp=getchar()));
return ans;
}
bool ok(int step,int j)
{
for(int i=1;i<=step;i++)
if(f[j][q[i]])return 1;
return 0;
}
bool dfs(int step,int x)
{
if(step>l)return 0;
int i,j;
for(i=1;i<=a[x][0];i++)
{
q[step]=a[x][i];
for(j=x+1;j<=n;j++)if(!ok(step,j))break;
if(j>n)return 1;
if(dfs(step+1,j))return 1;
}
return 0;
}
int main()
{
freopen("3.in","r",stdin);
int t,i,j;
for(t=getin();t;t--)
{
ms0(f),n=getin(),l=getin();
for(i=1;i<=n;i++)
{
a[i][0]=getin();
for(j=1;j<=a[i][0];j++)
f[i][(a[i][j]=getin())]=1;
}
if(dfs(1,1))printf("YES\n");
else printf("NO\n");
}
return 0;
}