BestCoder Round #60 GT and set (dfs)

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 .
 

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 L5 1 Ai 10 1LN

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 YES
Hint
For 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
hujie   |   We have carefully selected several similar problems for you:   5508  5507  5503  5502  5501 
 


解析:摘自BC官方题解:

 

可能一些选手题意不是很清楚,我这里再提供一个转化后的问题:

给出N个集合。每次你可以指定一个数,然后所有包含这个元素的集合可以被删掉。

问你能否经过最多L轮操作使得所有集合都被删掉。

因为LL只有55,考虑直接dfs。

对于第一个集合,我们枚举它的那个公共的数是多少。

然后线性扫描过去,找到接下来第一个没有这个数的集合。

(它显然不能通过这个公共的数与第一个数在同一个部分)

对于这个集合,再枚举它公共的数是多少,然后线性扫描过去找到第一个没有这两个数的集合……

这样重复55次后如果还是没有,就直接NO好了。若中途扫完序列就是YES。

这样最坏的效率就是O(N*10^{5})O(N105),足以通过此题。

其实我们可以预处理使得效率变成O(10^5)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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值