C. Kuro and Walking Route

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

).

Oddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index x

) and Beetopia (denoted with the index y). 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) if on the path from u to v

, 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)

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 n

, x and y ( 1n3105, 1x,yn, xy

) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.

n1

lines follow, each line contains two integers a and b ( 1a,bn, ab), describes a road connecting two towns a and b

.

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)

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

题意:

        给你一个图,问你在所有最短路中,不是先经过a,再经过b的路线有哪些。

思路:

        它一共有n个点,有n-1条边,而且两两之间可以相互到达,所以很明显是一棵树,所以最短路就是所有路径,我们很容易想到一共有n*(n-1)条路径,即我们只需要画一个图就能看出怎么做了- -。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<functional>
typedef long long LL;
#define maxn 300005
using namespace std;
vector<int>arr[maxn];
LL n, m,s,e,book[maxn],ac,ah,aq;
void dfs(int x, int step,int f)
{
	if (x == e)
	{
		ac = f;
		return;
	}
	else
	{
		for (int i = 0; i < arr[x].size(); i++)
		{
			if (arr[x][i] != f)
				dfs(arr[x][i], step + 1, x);
		}
	}
	ah++;
}
void dfs2(int x,int f)
{
	if (x == e || x == s)
		return;
	else
		for (int i = 0; i < arr[x].size(); i++)
		{
			if (arr[x][i] != f)
				dfs2(arr[x][i], x);
		}
	aq++;
}
int main()
{
	LL ans;
	scanf("%lld%lld%lld", &n,&s,&e);
	ans = n * (n - 1);
	for (int i = 1; i < n; i++)
	{
		LL a, b;
		scanf("%lld%lld", &a, &b);
		arr[a].push_back(b);
		arr[b].push_back(a);
	}
	dfs(s, -1, -1);
	dfs2(ac, -1);
	cout << ans - (n - ah)*(ah - aq) << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值