Discription

Little town Nsk consists of n junctions connected by m bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junction by these roads. The distance between two junctions is equal to the minimum possible number of roads on a path between them.

In order to improve the transportation system, the city council asks mayor to build one new road. The problem is that the mayor has just bought a wonderful new car and he really enjoys a ride from his home, located near junction s to work located near junction t. Thus, he wants to build a new road in such a way that the distance between these two junctions won't decrease.

You are assigned a task to compute the number of pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built the distance between s and t won't decrease.

Input

The firt line of the input contains integers nms and t (2 ≤ n ≤ 1000, 1 ≤ m ≤ 1000, 1 ≤ s, t ≤ ns ≠ t) — the number of junctions and the number of roads in Nsk, as well as the indices of junctions where mayors home and work are located respectively. The i-th of the following m lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi), meaning that this road connects junctions ui and vi directly. It is guaranteed that there is a path between any two junctions and no two roads connect the same pair of junctions.

Output

Print one integer — the number of pairs of junctions not connected by a direct road, such that building a road between these two junctions won't decrease the distance between junctions s and t.

Example

Input
5 4 1 5
1 2
2 3
3 4
4 5
Output
0
Input
5 4 3 5
1 2
2 3
3 4
4 5
Output
5
Input
5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5
Output
3

两遍dfs之后暴力判断即可。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1005;
bool a[maxn][maxn],v[maxn];
int n,m,S,T,d[maxn],g[maxn],ans;
int to[maxn*2],ne[maxn*2],hd[maxn]; inline void BFS(){
queue<int> q; int x;
q.push(S),v[S]=1;
while(!q.empty()){
x=q.front(),q.pop();
for(int i=hd[x];i;i=ne[i]) if(!v[to[i]]){
v[to[i]]=1,d[to[i]]=d[x]+1;
q.push(to[i]);
}
} memset(v,0,sizeof(v));
q.push(T),v[T]=1;
while(!q.empty()){
x=q.front(),q.pop();
for(int i=hd[x];i;i=ne[i]) if(!v[to[i]]){
v[to[i]]=1,g[to[i]]=g[x]+1;
q.push(to[i]);
}
}
} int main(){
scanf("%d%d%d%d",&n,&m,&S,&T);
int uu,vv;
for(int i=1;i<=m;i++){
scanf("%d%d",&uu,&vv),a[uu][vv]=a[vv][uu]=1;
to[i]=vv,ne[i]=hd[uu],hd[uu]=i;
to[i+m]=uu,ne[i+m]=hd[vv],hd[vv]=i+m;
} BFS(); for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++) if(!a[i][j])
if(d[i]+g[j]>=d[T]-1&&d[j]+g[i]>=d[T]-1) ans++; printf("%d\n",ans);
return 0;
}

  

 

Codeforces 954 D Fight Against Traffic的更多相关文章

  1. Codeforces 954D Fight Against Traffic(BFS 最短路)

    题目链接:Fight Against Traffic 题意:有n个点个m条双向边,现在给出两个点S和T并要增加一条边,问增加一条边且S和T之间距离不变短的情况有几种? 题解:首先dfs求一下S到其他点 ...

  2. 最短路 CF954D Fight Against Traffic

    CF954D Fight Against Traffic 题意描述: 给你一张无向图,一共有n个点(2 <= n <= 1000),由m条边连接起来(1 <= m <= 100 ...

  3. Codeforces 954 E. Water Taps

    http://codeforces.com/problemset/problem/954/E 式子变成Σ xi*(ti-T)=0 sum0表示>=T的ai*ti之和 sum1表示<T的ai ...

  4. Codeforces 954 G. Castle Defense

    http://codeforces.com/problemset/problem/954/G 二分答案 检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越 ...

  5. Fight Against Traffic -简单dijkstra算法使用

    题目链接 http://codeforces.com/contest/954/problem/D 题目大意 n m s t 分别为点的个数, 边的个数,以及两个特殊的点 要求s与t间的距离在新增一条边 ...

  6. CodeForcesEducationalRound40-D Fight Against Traffic 最短路

    题目链接:http://codeforces.com/contest/954/problem/D 题意 给出n个顶点,m条边,一个起点编号s,一个终点编号t 现准备在这n个顶点中多加一条边,使得st之 ...

  7. codeforces 487A A. Fight the Monster(二分)

    题目链接: A. Fight the Monster time limit per test 1 second memory limit per test 256 megabytes input st ...

  8. [CodeForces954D]Fight Against Traffic(最短路)

    Description 题目链接 Solution 从起点和终点分别做一次最短路并记录结果 枚举每一条可能的边判断 Code #include <cstdio> #include < ...

  9. Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check

    A B C D 给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法 因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果d ...

随机推荐

  1. Python 语言规范

    Python 语言规范 pychecker  对你的代码运行pychecker 定义: pychecker 是一个在Python 源代码中查找bug 的工具. 对于C 和C++这样的不那 么动态的( ...

  2. mybatis获取存储过程返回结果

    获取存储过程返回结果 代码: // Map<String,Object> map = new HashMap<String,Object>(); map.put("i ...

  3. 【整理】treeGrid 树形表格

    treeGrid 树形表格 https://fly.layui.com/extend/treeGrid/

  4. Maven实战读书笔记(三):Maven依赖

    3.1 依赖的配置 一个依赖声明可以包含下面元素: <dependencies> <dependency> <groupId></groupId> &l ...

  5. 【详●析】[GXOI/GZOI2019]逼死强迫症

    [详●析][GXOI/GZOI2019]逼死强迫症 脑子不够用了... [题目大意] 在\(2\times N\)的方格中用\(N-1\)块\(2\times 1\)的方砖和\(2\)块\(1\tim ...

  6. 哈理工(HUST)第八届程序设计竞赛--小乐乐的组合数

    这道题目是一道数学题,我们可以假设n为7,m为14. 这样的话我们就可以很清晰地看到7和7可以拼接在一起,这是一对,然后是7和14拼接在一起,第二对. 我们可以直接让n/7,m/7,这样就是1*2,就 ...

  7. <Spring Cloud>入门二 Eureka Client

    1.搭建一个通用工程 1.1 pom 文件 <?xml version="1.0" encoding="UTF-8"?> <project x ...

  8. linux shell 搭建本地yum 源,通过IOS镜像 Centeros6,7还有redhat

    Centeros: 准备工作: 将系统镜像放到 /opt 下 脚本: #!/bin/bash mkdir /mnt/cdrom mount -o loop /opt/*.ios;# 此处改为你的系统镜 ...

  9. Ubuntu 和 centos7 服务的启动

    Ubuntu 下: /etc/init.d/nginx  start | stop | reload Centos7下: service nginx start | stop | reload

  10. Python3.6中文文档 又来推荐一个,之前的Python3.52看得有点懵逼 https://www.rddoc.com/doc/Python/3.6.0/zh/

    https://www.rddoc.com/doc/Python/3.6.0/zh/    大家有空看下