博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CodeForces 1277E --- Two Fairs】DFS
阅读量:2038 次
发布时间:2019-04-28

本文共 2969 字,大约阅读时间需要 9 分钟。

【CodeForces 1277E --- Two Fairs】DFS

题目来源:

Description

There are n 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 1 to n.

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

Find the number of pairs of cities x and y (x≠a,x≠b,y≠a,y≠b) such that if you go from x to y 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,y such that any path from x to y goes through a and b (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) and (y,x) must be taken into account only once.

Input

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

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

The following m lines contain descriptions of roads between cities. Each of road description contains a pair of integers ui,vi (1≤ui,vi≤n, ui≠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 n for all sets of input data in the test does not exceed 2⋅105. The sum of the values of m for all sets of input data in the test does not exceed 5⋅105.

Output

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

Sample 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

Sample Output

4

0
1

解题思路

通过dfs获取到能连接a的非b城市集合s1,以及连接b的非a城市集合s2。在s1的不在s2的城市就只能连接a的城市数量x,反之在s2不在s1的就只能连接b城市数量y。但是中间有a,b城市本身,所以答案是(x-1)*(y-1)

AC代码:

#include 
using namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'using ll = long long;const int MAXN = 4e5+5;vector
v[MAXN];set
s1,s2;void dfs(int x,int y,set
&s){
s.insert(x); for(int a:v[x]) if(a!=y && !s.count(a)) dfs(a,y,s);}int main(){
SIS; int T; cin >> T; while(T--) {
ll n,m,a,b,x,y; cin >> n >> m >> a >> b; for(int i=1;i<=n;i++) v[i].clear(); s1.clear(); s2.clear(); for(int i=0;i
> x >> y; v[x].emplace_back(y); v[y].emplace_back(x); } dfs(a,b,s1); dfs(b,a,s2); x=0;y=0; for(int a:s1) if(!s2.count(a)) x++; y=s2.size()-(s1.size()-x); cout << (x-1)*(y-1) << endl; } return 0;}

转载地址:http://upyof.baihongyu.com/

你可能感兴趣的文章
Bootstrap 自定义弹框
查看>>
MyBatis 分页插件 PageHelper 使用方法
查看>>
AbstractQueuedSynchronizer 源码分析
查看>>
分布式以客户为中心的一致性
查看>>
java 注解
查看>>
CAS:乐观锁实现
查看>>
压力测试工具Apache ab
查看>>
Linux - Shell
查看>>
MySQL 如何执行关联查询
查看>>
算法运行时间n3,n2, n, nlogn对比
查看>>
java 中的suppressedException
查看>>
Rope --高效字符串处理数据结构
查看>>
Gap buffer -- 一个数据结构为可编辑的文本
查看>>
X/Open DTP模型,两阶段提交,JTA接口定义
查看>>
MySql binlog 日志
查看>>
spring 事务管理
查看>>
WebCollector爬虫的数据持久化
查看>>
Redis技术知识总结之三——Redis数据淘汰机制
查看>>
[初心者适用]如何为代码编写基本的文档
查看>>
DailyScrum beta 第三天!
查看>>