?? ?? ??
题意:求点对中,满足要互达必须经过a,b两点的对数,图为无向连通图
若(x,y)要满足互达必须经过(a,b),反过来想,
a必须通过b点到达y点:满足a--->b--->y;
b必须通过a点到达x点:满足b--->a--->x,无向图:x--->a--->b;
连起来即为:x--->a--->b--->y;
int vis[MAXN];
vector<int>edge[MAXN];
void dfs(int x,int e)
{
vis[x]=1;
if(x==e) return ;
for(auto v:edge[x])
{
if(vis[v]==0)
{
vis[v]=1;
dfs(v,e);
}
}
}
int main()
{
int t;cin>>t;
while(t--)
{
int n,m,a,b;
cin>>n>>m>>a>>b;//
rpp(i,n) edge[i].clear(),vis[i]=0;
rep(i,m)
{
int x,y;cin>>x>>y;
edge[x].push_back(y);
edge[y].push_back(x);
}
int num0=0,num1=0;
dfs(a,b);
rpp(i,n) if(!vis[i]) ++num0;
rpp(i,n) vis[i]=0;
dfs(b,a);
rpp(i,n) if(!vis[i]) ++num1;
ll ans=1ll*num0*num1;
cout<<ans<<endl;
}
//stop;
return 0;
}
原文地址:https://www.cnblogs.com/Herlo/p/12064106.html
时间: 2024-10-09 07:09:22