1. 程式人生 > >牛客練習賽39 D 動態連通塊+並查集 X bitset 優化

牛客練習賽39 D 動態連通塊+並查集 X bitset 優化

alt reat lambda stream hid inf view puts tchar

https://ac.nowcoder.com/acm/contest/368/D

題意

小T有n個點,每個點可能是黑色的,可能是白色的。
小T對這張圖的定義了白連通塊和黑連通塊:
白連通塊:圖中一個點集V,若滿足所有點都是白點,並且V中任意兩點都可以只經過V中的點互相到達,則稱V中的點構成了一個白連通塊。
黑連通塊:類似白連通塊的定義。
小T對這n個點m次操作。
1、在兩個點之間連一條邊。
2、詢問白(黑)連通塊個數。
3、給出x,y兩個點,保證同色(為了方便描述,x,y都是白點,黑色同理)。詢問存在多少個黑點,將它改變顏色後,x,y所在的白連通塊會合並為一個。如果x,y已經在一個白連通塊內了,輸出-1。(註意:這裏不會對點的顏色改變,只統計個數)

思路

感覺這個並查集還是很妙的,顏色相同的想要合並就按照正常的並查集來。如果顏色不同,直接合並沒有意義,用bitset記錄下來,兩個白點如果bitset中有相同的黑點,那麽這個點就是第三問中的一個合法點。

技術分享圖片
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    
<cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ   \\ Λ_Λ 來了老弟    \(‘?‘)     > ⌒ヽ    /   へ\    /  / \\    ? ノ   ヽ_つ   / /   / /|  ( (ヽ  | |、\  | 丿 \ ⌒)  | |  ) / ‘ノ )  L?
*/ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//這是一個大根堆q //priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q #define fi first #define se second //#define endl ‘\n‘ #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黃金分割點 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<0||ch>9) f|=(ch==-),ch=getchar(); while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 5e4+9; int col[maxn],fa[maxn]; bitset<50009> b[maxn],tmp; int ans[2]; int find(int x){ if(fa[x] == x) return x; return fa[x] = find(fa[x]); } void uni(int x,int y){ int fx = find(x), fy = find(y); if(col[x] == col[y]){ if(fx != fy){ fa[fx] = fy; ans[col[x]]--; b[fy] |= b[fx]; } } else b[fx].set(y), b[fy].set(x); } void cal(int x,int y){ int fx = find(x), fy = find(y); if(fx == fy) puts("-1"); else { tmp = b[fx] & b[fy]; printf("%d\n", (int)tmp.count()); } } int main(){ int n,m; scanf("%d%d", &n, &m); rep(i, 1, n) scanf("%d", &col[i]), fa[i] = i, ans[col[i]] ++; while(m--){ int op; scanf("%d", &op); if(op == 1) { int x, y; scanf("%d%d", &x, &y); uni(x, y); } else if(op == 2) { int x; scanf("%d", &x); printf("%d\n", ans[x]); } else { int x,y; scanf("%d%d", &x, &y); cal(x, y); } } return 0; }
View Code

牛客練習賽39 D 動態連通塊+並查集 X bitset 優化