日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

codeforces CF986C AND Graph 建圖 dfs

發布時間:2025/3/15 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 codeforces CF986C AND Graph 建圖 dfs 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

$ \rightarrow $ 戳我進CF原題

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 $ m $ with integer elements between $ 0 $ and $ 2^n-1 $ inclusive.
Let's build an undirected graph on these integers in the following way:
connect two integers $ x $ and $ y $ with an edge if and only if $ x $ & $ 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 $ n $ and $ m ( 0 \le n \le 22, 1 \le m \le 2^n ) $ ,
 
In the second line there are $ m $ integers $ a_1,a_2, \dots ,a_m ( 0 \le a_i \le 2^n ) $ — the elements of the set.
All $ a_i $ are distinct.
 

Output

Print the number of connected components.

 

Examples

input1

2 31 2 3

output1

2

input2

5 55 19 10 20 12

output2

2

 

Note

Graph from first sample:

Graph from second sample:

 

題目大意

  • 給定 $ m $ 個 $ 0 ~ 2^n-1 $ 之間的整數,每個整數代表一個點

  • 兩個整數 $ x,y $ 之間有無向邊當且僅當 $ x $ & $ y=0 $ ,求無向圖有多少個連通塊

  • $ n \le 22 $

 

題解

  • 把 $ 0 ~ 2^n-1 $ 之間的每個點拆成 $ x $ 和 $ x' $ 兩個點

  • $ 1. $ 從 $ x $ 到 $ (~x)' $ 連有向邊

  • $ 2. $ 從 $ x' $ 到 $ (x \quad xor \quad (1 \ll k ))' $ 連有向邊 $ (o \le k < n ) $

  • $ 3. $ 若 $ x $ 屬於給定的 $ m $ 個數,則從 $ x' $ 到 $ x $ 連有向邊

  • 從 $ m $ 個數出發進行遍歷,求連通塊數

 

代碼

#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; #define maxn (1<<23)+5 int n,m,ans,tot; bool vis[maxn],mark[maxn]; void dfs(int u){if(vis[u]) return; vis[u]=1;if(u<(1<<n)) dfs(u+(1<<n));else {tot=(1<<(n+1))-1-u;if(!vis[tot]&&mark[tot]) dfs(tot);for(int i=0;i<n;++i) if(!vis[u|(1<<i)]) dfs(u|(1<<i));} } int main(){scanf("%d %d",&n,&m);for(int x,i=1;i<=m;++i){ scanf("%d",&x); mark[x]=1; }for(int i=0;i<(1<<n);++i) if(mark[i]&&!vis[i]){ ++ans; dfs(i); }printf("%d",ans);return 0; } /* # 40059473 When 2018-07-07 14:30:49 Who PotremZ Problem C - AND Graph Lang GNU C++ Verdict Accepted Time 764 ms Memory 91100 KB */

轉載于:https://www.cnblogs.com/PotremZ/p/9600617.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的codeforces CF986C AND Graph 建圖 dfs的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。