根号分治。
暴力 1:
考虑直接枚举 的邻居并开个 map 判断是否是 的邻居。记得特判 和 相邻的情况。
时间复杂度 。
暴力 2:
对于每个点开 bitset 存储所有邻居(包括自己)。
利用位运算可以快速排除 的邻居中同时也是 的邻居的点。记住要清掉 和 。
时间复杂度 ,空间复杂度 。
暴力 3:
枚举 的邻居并在 的 bitset 中排除。
时间复杂度 。
考虑根号分治。设阈值为 。以下称度数大于阈值的为大点,否则为小点。显然每个点的度数之和为 ,那么大点数量最多为 。之后预处理大点连接边的 bitset。
-
为小点。
直接采用暴力 1 即可。时间复杂度 。
-
为大点, 为大点。
使用暴力 2。时间复杂度 。
-
为大点, 为小点。
使用暴力 3。时间复杂度 。
由于大点最多可能会有 个,所以空间复杂度大概在 。
取 为 40 时可以通过。
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int mod=1e9+7,inf=0x3f3f3f3f3f3f3f3f;
const int B=40;
vector<bitset<200010> >vec;
vector<int>e[200010];
map<int,int>mp[200010];
int n,m,q;
int id[200010];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin >> n >> m >>q;
while(m--)
{
int u,v;
cin >> u >>v;
e[u].push_back(v);
e[v].push_back(u);
mp[u][v]=mp[v][u]=1;
}
for ( int i = 1 ; i <= n ; i++ )
{
if(e[i].size()>B)
{
id[i]=vec.size();
vec.push_back(0);
for ( auto v:e[i] )vec[id[i]][v]=1;
vec[id[i]][i]=1;
}
}
while(q--)
{
int u,v;
cin >> u >> v;
if(e[u].size()<=B)
{
int ans=0;
for ( auto i:e[u] )
{
if(!mp[v][i]&&i!=v)ans++;
}
cout << ans << endl;//Blogn
}else{
if(e[v].size()>B)
{
bitset<200010>bit=((vec[id[u]]&vec[id[v]])^vec[id[u]]);
bit[u]=0;
cout << bit.count() << endl;//n/w,[n^2/8/B]
}else{
bitset<200010>bit=vec[id[u]];
for ( auto i:e[v] )
{
bit[i]=0;
}
bit[v]=0;
bit[u]=0;
cout << bit.count() << endl;//B
}
}
}
return 0;
}