CodeForces - 986C AND Graph
不難想到,x有邊連出的一定是 (2^n-1) ^ x 的一個子集,直接連子集復雜度是爆炸的。。。但是我們可以一個1一個1的消去,最後變成補集的一個子集。
但是必須當且僅當 至少有一個 a 等於 x 的時候, 可以直接dfs(all ^ x) ,否則直接消1連邊。。。
Discription
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
Input
In the first line of input there are two integers nn and mm (0≤n≤220≤n≤22, 1≤m≤2n1≤m≤2n).
In the second line there are mm integers a1,a2,…,am
Output
Print the number of connected components.
Examples
Input2 3Output
1 2 3
2
Input
5 5Output
5 19 10 20 12
2
Note
Graph from first sample:
Graph from second sample:
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=5000005; int ci[233],T,n,a[maxn],ans,all; bool v[maxn],isp[maxn]; inline int read(){ int x=0; char ch=getchar(); for(;!isdigit(ch);ch=getchar()); for(;isdigit(ch);ch=getchar()) x=x*10+ch-‘0‘; return x; } void dfs(int x){ if(v[x]) return; v[x]=1; if(isp[x]) dfs(all^x); for(int i=0;i<=T;i++) if(ci[i]&x) dfs(x^ci[i]); } inline void solve(){ for(int i=1;i<=n;i++) if(!v[a[i]]){ ans++,v[a[i]]=1,dfs(all^a[i]); } } int main(){ ci[0]=1; for(int i=1;i<=22;i++) ci[i]=ci[i-1]<<1; T=read(),n=read(),all=ci[T]-1; for(int i=1;i<=n;i++) a[i]=read(),isp[a[i]]=1; solve(); printf("%d\n",ans); return 0; }
CodeForces - 986C AND Graph