這玩意嚴格意義上算是水法(因為可能會被卡)
題目中,如果按照一般的bfs來搜索,那麼有平方級邊,會tle
如果按照補邊的線性來搜索,那麼時間複雜度變為min(k*k,m)*n,視n,m同階,則時間複雜度為nsqrt(n)
接下來需要設計一個和補邊數相關的算法
維護3個隊列,q1,q2,q3。q1代表待擴展節點列表,q2代表已經擴展節點列表,q3代表不能擴展節點列表
每次從q1中取出一個u,然後放進q2,然後聯通塊數目+1
再當q2非空時,從中取出一個v,嘗試用v擴展所有q1中的節點。當其與q1中的當前節點沒有邊,則將其放入q3,否則放入q2
最後,將q3所有節點放入q1
用哈希判斷邊是否存在
有可能會被卡,但是出題人顯然沒有卡
代碼:
#include<bits/stdc++.h>
using namespace std;
#define jzm 1000007
#pragma GCC optimize("O3")
int n,m,q;
set<int>s[100010];
int main(){
//freopen("connect.in","r",stdin);
//freopen("connect.out","w",stdout);
scanf("%d%d%d",&n,&m,&q);
while(m--){
int x,y;
scanf("%d%d",&x,&y);
s[x].insert(y);
s[y].insert(x);
}
while(q--){
int t,x,ct=0;
scanf("%d",&t);
queue<int>q1,q2,q3;
for(int i=1;i<=t;i++){
scanf("%d",&x);
q1.push(x);
}
while(!q1.empty()){
int x=q1.front();q1.pop();
ct++;q2.push(x);
while(!q2.empty()){
int z=q2.front();q2.pop();
while(!q1.empty()){
int y=q1.front();q1.pop();
if(s[z].count(y))q3.push(y);
else q2.push(y);
}
while(!q3.empty()){
int y=q3.front();q3.pop();
q1.push(y);
}
}
}
printf("%d\n",ct);
}
}