hdu 5506 GT and set dfs+bitset优化

GT and set

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)



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 (T20)

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.

1N30,1L5,1Ai10,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
 题意:如果n个集合的交集不为空就可以合并,问n个能合并为L个;
思路:暴力搜索,两个集合合并的话用bitset优化即可;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+10,M=1e6+10,inf=1e9+10;
const ll INF=1e18+10,mod=2147493647;
bitset<310> flag[10],a[50],temp,bit;
int n,m,ans;
void dfs(int pos)
{
    if(pos>n||ans)
    {
        ans=1;
        return;
    }
    for(int i=1;i<=m;i++)
    {
        temp=flag[i]&a[pos];
        bit=flag[i];
        if(temp.count())
        {
            flag[i]=temp;
            dfs(pos+1);
            flag[i]=bit;
        }
    }
}
void init()
{
    for(int i=1;i<=n;i++)
        a[i].reset();
    for(int i=1;i<=m;i++)
        flag[i].set();
    ans=0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=n;i++)
        {
            int z;
            scanf("%d",&z);
            for(int j=1;j<=z;j++)
            {
                int x;
                scanf("%d",&x);
                a[i].set(x);
            }
        }
        dfs(1);
        if(ans)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/6243364.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值