AND Graph CodeForces - 987F (狀壓)
阿新 • • 發佈:2019-02-18
tps queue targe div printf back amp font n)
鏈接
大意:給定$m$個數, 若$x\&y=0$, 則在$x$與$y$之間連一條無向邊. 求無向圖的連通塊個數
暴力連邊顯然超時的, 可以通過輔助結點優化連邊, 復雜度$O(n2^n)$
#include <iostream> #include <algorithm> #include <cstdio> #include <queue> #define REP(i,a,n) for(int i=a;i<=n;++i) #define pb push_back using namespace std; const int N = 1<<22; int n, m; int a[N], vis[N], f[N]; int main() { scanf("%d%d", &n, &m); REP(i,1,m) scanf("%d",a+i),f[a[i]]=1; int mx = (1<<n)-1, ans = 0; REP(i,1,m) if (!vis[a[i]]) { ++ans; queue<int> q; q.push(a[i]); while (q.size()) { int x = q.front();q.pop(); if (vis[x]) continue; vis[x] = 1; if (f[x]) q.push(mx^x); for (int y=x; y; y^=y&-y) q.push(x^y&-y); } } printf("%d\n", ans); }
AND Graph CodeForces - 987F (狀壓)