zoukankan      html  css  js  c++  java
  • Codefoces 986C AND Graph(DFS)

    C. AND Graph
    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a set of size mm with integer elements between 00 and 2n12n−1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers xx and yy with an edge if and only if x&y=0x&y=0. Here && is the bitwise AND operation. Count the number of connected components in that graph.

    Input

    In the first line of input there are two integers nn and mm (0n220≤n≤221m2n1≤m≤2n).

    In the second line there are mm integers a1,a2,,ama1,a2,…,am (0ai<2n0≤ai<2n) — the elements of the set. All aiai are distinct.

    Output

    Print the number of connected components.

    Examples
    input
    Copy
    2 3
    1 2 3
    
    output
    Copy
    2
    
    input
    Copy
    5 5
    5 19 10 20 12
    
    output
    Copy
    2
    
    Note

    Graph from first sample:

    Graph from second sample:



    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<cstdio>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<queue>
    #define ll long long
    using namespace std;
    int f[35];
    int n,m,isp[5001000],a[5001000],vis[5001000],N,ans=0,i;
    void dfs(int x)
    {
    //	printf("%d %d
    ",x,i);
    	if(vis[x]==1) return ;
    	vis[x]=1;
    	if(isp[x]) dfs(N^x);
    	for(int i=1;i<=n;i++) if(x&(1<<(i-1))) dfs(x^(1<<(i-1)));
    }
    void solve()
    {
    	for(int i=1;i<=m;i++)
    	   if(!vis[a[i]])
    	     {
    	     	ans++;
    	     	vis[a[i]]=1;
    	     	dfs(N^a[i]);
    		 }
    }
    int main()
    {
    f[1]=1;
    for(int i=2;i<=30;i++) f[i]=f[i-1]*2;
    scanf("%d%d",&n,&m);
    N=f[n+1]-1;
    for(int i=1;i<=m;i++) scanf("%d",&a[i]);
    for(int i=1;i<=m;i++) isp[a[i]]=1;
    solve();
    printf("%d
    ",ans);	
    } 


  • 相关阅读:
    加密算法整理
    NSURLConnection类说明
    ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
    "ld: library not found for l...." 问题的解决
    ios5 自定义导航条问题
    objectivec 语言知识点
    JSON
    [转]XCode中修改缺省公司名称/开发人员名称
    [转]iPhone开源项目汇总
    清除SQL 数据库日志 欧阳锋
  • 原文地址:https://www.cnblogs.com/The-Pines-of-Star/p/9878828.html
Copyright © 2011-2022 走看看