# Codeforces979C 、 Kuro and Walking Route(dfs)

C. Kuro and Walking Route

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11 to nn, and n−1n−1 bidirectional roads connecting these towns. It is possible to reach each town from any other. Each road connects two towns aa and bb. Kuro loves walking and he is planning to take a walking marathon, in which he will choose a pair of towns (u,v)(u,v) (u≠vu≠v) and walk from uu using the shortest path to vv (note that (u,v)(u,v) is considered to be different from (v,u)(v,u)).

Oddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index xx) and Beetopia (denoted with the index yy). Flowrisa is a town where there are many strong-scent flowers, and Beetopia is another town where many bees live. In particular, Kuro will avoid any pair of towns (u,v)(u,v) if on the path from uu to vv, he reaches Beetopia after he reached Flowrisa, since the bees will be attracted with the flower smell on Kuro’s body and sting him.

Kuro wants to know how many pair of city (u,v)(u,v) he can take as his route. Since he’s not really bright, he asked you to help him with this problem.

Input

The first line contains three integers nn, xx and yy (1≤n≤3⋅105,1≤x,y≤n1≤n≤3⋅105,1≤x,y≤n, x≠yx≠y) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.

n−1n−1 lines follow, each line contains two integers aa and bb (1≤a,b≤n1≤a,b≤n, a≠ba≠b), describes a road connecting two towns aa and bb.

It is guaranteed that from each town, we can reach every other town in the city using the given roads. That is, the given map of towns and roads is a tree.

Output

A single integer resembles the number of pair of towns (u,v)(u,v) that Kuro can use as his walking route.

Examples

input

Copy

3 1 3
1 2
2 3


output

Copy

5

input

Copy

3 1 3
1 2
1 3


output

Copy

4

Note

On the first example, Kuro can choose these pairs:

• (1,2)(1,2): his route would be 1→21→2,
• (2,3)(2,3): his route would be 2→32→3,
• (3,2)(3,2): his route would be 3→23→2,
• (2,1)(2,1): his route would be 2→12→1,
• (3,1)(3,1): his route would be 3→2→13→2→1.

Kuro can't choose pair (1,3)(1,3) since his walking route would be 1→2→31→2→3, in which Kuro visits town 11 (Flowrisa) and then visits town 33 (Beetopia), which is not allowed (note that pair (3,1)(3,1) is still allowed because although Kuro visited Flowrisa and Beetopia, he did not visit them in that order).

On the second example, Kuro can choose the following pairs:

• (1,2)(1,2): his route would be 1→21→2,
• (2,1)(2,1): his route would be 2→12→1,
• (3,2)(3,2): his route would be 3→1→23→1→2,
• (3,1)(3,1): his route would be 3→13

给出n个点，有n-1个边将他们链接。给出x,y，当某一路径中出现x....y时，此路不通。路径（u,v）和（v,u）是不同的。

n个点而只有n-1条边，所以不会出现环路径。点对数为n*（n-1）。此时我们可以先找出哪一些路径是不符合的，dfs一遍从x到y的路径并记录，这样就可以提取出（x,y）这段路中间的点。再分别从x，y为起点跑一遍dfs,可以统计出x,y两侧的点的个数cntx、cnty。

画一张简图很好理解，答案就是总点对数减去x、y两侧点数的乘积，即为n*(n-1)-cntx*cnty。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf=0x3f3f3f3f;

const int maxn=300000+500;
int n,m,x,y;
vector<int>e[maxn];
bool vis[maxn],xoy[maxn];
int pre[maxn];
void init()
{
memset(xoy,false,sizeof(xoy));
memset(vis,false,sizeof(vis));
memset(pre,-1,sizeof(pre));
scanf("%d%d%d",&n,&x,&y);
m=n-1;
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
}
void predfs(int u,int fa)
{
pre[u]=fa;
if(u==y)
return ;
int size=e[u].size();
for(int i=0;i<size;i++)
{
int v=e[u][i];
if(v!=fa)
{
predfs(v,u);
}
}
}
int dfs(int u)
{
vis[u]=true;
int size=e[u].size();
int here=1;
for(int i=0;i<size;i++)
{
int v=e[u][i];
if(!vis[v]&&!xoy[v]&&v!=x&&v!=y)
{
here+=dfs(v);
}
}
return here;
}
int main()
{
init();		//初始化
predfs(x,-1);//预处理记录x到y的路径
for(int i=pre[y];i!=x;i=pre[i])
xoy[i]=true;					//标记x到y的路径

int cntx=dfs(x);	//统计x侧点数
int cnty=dfs(y);	//统计y侧点数
long long int ans=(long long)1*n*(n-1)-(long long)cntx*cnty;
cout<<ans<<endl;
getchar();
getchar();
}