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)/~

发布了414 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/104186201
two