Two Fairs(DFS)

There are nn cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from 11 to nn.

Two fairs are currently taking place in Berland — they are held in two different cities aa and bb (1≤a,b≤n1≤a,b≤n; a≠ba≠b).

Find the number of pairs of cities xx and yy (x≠a,x≠b,y≠a,y≠bx≠a,x≠b,y≠a,y≠b) such that if you go from xx to yy you will have to go through both fairs (the order of visits doesn’t matter). Formally, you need to find the number of pairs of cities x,yx,y such that any path from xx to yy goes through aa and bb (in any order).

Print the required number of pairs. The order of two cities in a pair does not matter, that is, the pairs (x,y)(x,y) and (y,x)(y,x) must be taken into account only once.

Input
The first line of the input contains an integer tt (1≤t≤4⋅1041≤t≤4⋅104) — the number of test cases in the input. Next, tt test cases are specified.

The first line of each test case contains four integers nn, mm, aa and bb (4≤n≤2⋅1054≤n≤2⋅105, n−1≤m≤5⋅105n−1≤m≤5⋅105, 1≤a,b≤n1≤a,b≤n, a≠ba≠b) — numbers of cities and roads in Berland and numbers of two cities where fairs are held, respectively.

The following mm lines contain descriptions of roads between cities. Each of road description contains a pair of integers ui,viui,vi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi) — numbers of cities connected by the road.

Each road is bi-directional and connects two different cities. It is guaranteed that from any city you can pass to any other by roads. There can be more than one road between a pair of cities.

The sum of the values of nn for all sets of input data in the test does not exceed 2⋅1052⋅105. The sum of the values of mm for all sets of input data in the test does not exceed 5⋅1055⋅105.

Output
Print tt integers — the answers to the given test cases in the order they are written in the input.

Example
Input
3
7 7 3 5
1 2
2 3
3 4
4 5
5 6
6 7
7 5
4 5 2 3
1 2
2 3
3 4
4 1
4 2
4 3 2 1
1 2
2 3
4 1
Output
4
0
1
这个题目真的想过来就只是一个dfs遍历的问题。
思路:两个点必须都是割点才可以有这样的数对。我们先将一个点a设置为割点,从另一个点b出发,计算不能到达的点数cnt1。然后将b设置为割点,从a出发,计算不能到达的点数cnt2。相乘后就是答案了。两次dfs计算的是分别与a,b两点相连的点数。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e5+100;
vector<int> p[maxx];
int vis[maxx];
int n,m,a,b;
inline void init()
{
	for(int i=1;i<=n;i++) p[i].clear(),vis[i]=0;
}
inline void dfs(int u,int x)
{
	vis[u]=1;
	for(int i=0;i<p[u].size();i++)
	{
		if(vis[p[u][i]]||p[u][i]==x) continue;
		dfs(p[u][i],x);
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int x,y;
		init();
		scanf("%d%d%d%d",&n,&m,&a,&b);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			p[x].push_back(y);
			p[y].push_back(x);
		}
		dfs(a,b);
		int cnt1=0,cnt2=0;
		for(int i=1;i<=n;i++) if(vis[i]==0) cnt1++;
		for(int i=1;i<=n;i++) vis[i]=0;
		dfs(b,a);
		cnt1-=1;cnt2-=1;
		for(int i=1;i<=n;i++) if(vis[i]==0) cnt2++;
		printf("%lld\n",(ll)cnt1*(ll)cnt2);
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值