zoukankan      html  css  js  c++  java
  • Codeforces Round #485 (Div. 1) C. AND Graph

    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≤22, 1m2n1≤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:

    思路:对于每个x(0 <= x < 1 << n),向刚好从x中去掉某个2进制位的1的所有数y连一条边。然后对于每个a[i],从~a[i]跑dfs,中途遇到a[j]时,再继续从~a[j]跑,跑完一套dfs就求出了一个联通块。

     1 #include <iostream>
     2 #include <fstream>
     3 #include <sstream>
     4 #include <cstdlib>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <string>
     8 #include <cstring>
     9 #include <algorithm>
    10 #include <queue>
    11 #include <stack>
    12 #include <vector>
    13 #include <set>
    14 #include <map>
    15 #include <list>
    16 #include <iomanip>
    17 #include <cctype>
    18 #include <cassert>
    19 #include <bitset>
    20 #include <ctime>
    21 
    22 using namespace std;
    23 
    24 #define pau system("pause")
    25 #define ll long long
    26 #define pii pair<int, int>
    27 #define pb push_back
    28 #define mp make_pair
    29 #define clr(a, x) memset(a, x, sizeof(a))
    30 
    31 const double pi = acos(-1.0);
    32 const int INF = 0x3f3f3f3f;
    33 const int MOD = 1e9 + 7;
    34 const double EPS = 1e-9;
    35 
    36 /*
    37 #include <ext/pb_ds/assoc_container.hpp>
    38 #include <ext/pb_ds/tree_policy.hpp>
    39 
    40 using namespace __gnu_pbds;
    41 tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> T;
    42 */
    43 
    44 int n, m, a[5000015], c;
    45 bool vis[9000015];
    46 int nex[9000015];
    47 void dfs(int x) {
    48     vis[x] = 1;
    49     if (~nex[x] && !vis[nex[x]]) dfs(nex[x]);
    50     if (c <= x) {
    51         x -= c;
    52         int p = x;
    53         for (; p; p -= p & -p) {
    54             int y = x - (p & -p) + c;
    55             if (vis[y]) continue;
    56             dfs(y);
    57         }
    58     }
    59 }
    60 int main() {
    61     scanf("%d%d", &n, &m);
    62     for (int i = 1; i <= m; ++i) scanf("%d", &a[i]);
    63     c = 1 << n;
    64     clr(nex, -1);
    65     for (int i = 1; i <= m; ++i) {
    66         nex[a[i]] = 2 * c - 1 - a[i];
    67         nex[a[i] + c] = a[i];
    68     }
    69     int cnt = 0;
    70     for (int i = 1; i <= m; ++i) {
    71         if (!vis[a[i]]) {
    72             ++cnt;
    73             dfs(a[i]);
    74         }
    75     }
    76     return !printf("%d
    ", cnt);
    77 }
    View Code
  • 相关阅读:
    小埋的Dancing Line之旅:比赛题解&热身题题解
    洛谷P1311 选择客栈
    洛谷 P1039 侦探推理
    洛谷P1140 相似基因
    反⑨baka拖更大队:临时约法
    随机数据生成器
    洛谷P2285 【[HNOI2004]打鼹鼠】
    洛谷P3958 奶酪
    YII 关联查询
    Filter
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/9120932.html
Copyright © 2011-2022 走看看