All Union sets that can be generated by the HDU 3006 enumeration set

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3006

The cherry keyboard I just bought feels really good. Unfortunately, I am not used to coding errors. I wrote all kinds of wrong keys for a single question all morning. debug .....

At first, I thought DFS would not work.

Then, we can enumerate the Union sets of all the two sets in a two-repeating loop. The three-repeating loop can enumerate the Union sets of all three sets. Then, N subsets seem to require n re-loops, NP problems ,,,,,

The practice is to simulate a small number, because there are only 14, so the pressure storage

Example 1

Four subsets: 1, 2, 4

Binary 0001 0010 0011 0100

The first and other three operations get 0001 0010 0011 0100 0011 0101 ....

Then the second number and the new number are different or

In the code, the second round starts with I + 1 and repeats. In fact, it does not save much time (o) O

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>#include <queue>using namespace std;#define IN(s) freopen(s,"r",stdin)#define CL(a,b) memset(a,b,sizeof(a))const int MAXN = 1<<15;int vis[MAXN],ans[MAXN];int main(){    //IN("hdu3006.txt");    int n,m;    int k;    while(~scanf("%d%d",&n,&m))    {        CL(vis,0);        int cnt=0;        for(int j=0;j<n;j++)        {            scanf("%d",&k);            int t=0,a=0;            for(int i=0;i<k;i++)            {                scanf("%d",&t);                a|=1<<(t-1);            }            if(!vis[a])            {                vis[a]=1;                ans[cnt++]=a;            }        }        int ct=cnt;        for(int i=0;i<ct;i++)        {            int s=ans[i];            for(int j=i+1;j<cnt;j++)            {                if(!vis[(s|ans[j])] && i!=j)                {                    vis[s|ans[j]]=1;                    ans[cnt++]=s|ans[j];                }            }        }        printf("%d\n",cnt);    }    return 0;}



All Union sets that can be generated by the HDU 3006 enumeration set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.