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. AI学习一:环境安装

    对于Python开发用户来讲,PIP安装软件包是家常便饭.但国外的源下载速度实在太慢,浪费时间.而且经常出现下载后安装出错问题.所以把PIP安装源替换成国内镜像,可以大幅提升下载速度,还可以提高安装成 ...

  2. TCP/IP详解之IP协议

    1.IP协议 IP协议是TCP/IP协议的核心,所有的TCP,UDP,IMCP,IGCP的数据都以IP数据格式传输.要注意的是,IP不是可靠的协议,这是说,IP协议没有提供一种数据未传达以后的处理机制 ...

  3. iOS开发内购全套图文教程

    2015年最全的内购图文教程,首先是填各种资料,最后是代码,废话不多说,直接上图 ======================第一部分协议=============== 第一步 第二步 第三步 第四步 ...

  4. Windows程序设计1(工具、编码、窗口)

    一.几个常用小工具: 1. 编译器:CL.EXE   将源文件转变为目标文件(汇编语言). CL.EXE  /c  xxx.c  或   xx.cpp cl.exe  -? 显示cl帮助 cl.exe ...

  5. vue 组件名和方法名 重名了,报function错误

    vue 组件名和方法名 重名了,报function错误

  6. Ubuntu的防火墙配置-ufw-iptables

    自打2.4版本以后的Linux内核中, 提供了一个非常优秀的防火墙工具.这个工具可以对出入服务的网络数据进行分割.过滤.转发等等细微的控制,进而实现诸如防火墙.NAT等功能.一般来说, 我们会使用名气 ...

  7. TWaver可视化编辑器的前世今生(二)3D编辑器

    接着昨天的继续说哈. 作为一款高效.轻量.自带编辑功能小组件,TWaver Java在电信网管界一炮而红,在各大运营商的OSS,BSS,NMS系统中随处可见. 采用了TWaver图形组件的上海世博会监 ...

  8. Java会话(session)管理

    会话概述 什么是会话 简单的理解:用户打开浏览器,点击多个超链接,访问Web服务器上多个资源,然后关闭浏览器,整个过程称之为一次会话. 需要解决的问题 每个用户在使用浏览器与服务器会话的过程中,会产生 ...

  9. vue在传值的时候经常遇到的问题

    在我用vue编写程序的时候,在传值的时候,经常会遇到些问题,像今天遇到了两个问题,在用父传子的方法去传值,当父组件中的要传的数据是for循环出来的或者是列表的时候,你想每次运行的事件,都去传某一行,或 ...

  10. vue2.0中transition组件的用法

    作用:实现元素进入/离开的过渡效果. 首先,让我们举个栗子: <!DOCTYPE html> <html lang="en"> <head> & ...