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. 修改JRE system library

    MyEclipse 默认的情况下JRE system library 是:MyEclipse 的,如何修改工程中的JRE system library呢?步骤如下: 1.选择工程->Proper ...

  2. python大文件读取

    python大文件读取 https://stackoverflow.com/questions/8009882/how-to-read-a-large-file-line-by-line-in-pyt ...

  3. iview modal 点击打开窗口,打开前先销毁里面的内容再打开

    <Modal v-model="addSubOrgModal" @on-cancel="addSubOrgCancel" @on-visible-chan ...

  4. Asp.Net Core 入门(二)——Startup.cs做了什么

    上篇介绍了Program.cs中Main做了什么,这篇我们来讨论下Startup.cs它又做了什么呢? 我们新建一个Asp.Net Core Mvc项目,先来开一下Startup的代码 public ...

  5. Java集合(六)--ArrayList、LinkedList和Vector对比

    在前两篇博客,学习了ArrayList和LinkedList的源码,地址在这: Java集合(五)--LinkedList源码解读 Java集合(四)--基于JDK1.8的ArrayList源码解读 ...

  6. STL || HDU 1894 String Compare

    如果一个词包含再另一个词的前面(前缀),是一对前缀,求一共有多少对 *解法:STL万岁 #include<string>:https://www.cnblogs.com/SZxiaochu ...

  7. centOS出现 -bash: vim: command not found

    问题描述 用centos 的主机的時候, 用 vim 时出现 -bash: vim: command not found. 只能使用 vi. 那么如何安裝 vim 呢? 解决步骤 1.查看是否安装 输 ...

  8. Oracle清空数据库中数据表数据的方法

    一.简介最近在项目发版测试的时候,导出dmp的时候不小心把开发库中的一些脏数据导出来了,测试那边导入进去之后一堆不规范的数据,为了不影响测试结果,于是总结了一个快速清空数据库数据表所有数据的方法. 二 ...

  9. C++中static,extern和extern "C"关键字

    1. extern 变量 extern 表明该变量在别的地方已经定义过了,在这里要使用那个变量. 当extern不与"C"在一起修饰变量或函数时,如在头文件中: extern in ...

  10. 解决浏览器自动填充input

    浏览器输入框自动填充解决办法 emmmmm:今天处理公司后台系统遇到的:登录页面浏览器保存账号密码后:浏览器会自动在其他页面进行填充:解决如下图: 浏览器会默认填充input type值为passwo ...