Codeforces Round #485 (Div. 1) C. AND Graph

Posted bigtom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 2n?12n?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

 



以上是关于Codeforces Round #485 (Div. 1) C. AND Graph的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #485 (Div. 2) C. Three displays

Codeforces Round #485 (Div. 2) E. Petr and Permutations

Codeforces Round #485

Codeforces Round #485 (Div. 2) C Three displays

Codeforces Round #485 (Div. 2)-C-Three displays

Codeforces Round #485 (Div. 2)