Codeforces Round #482 (Div. 2) C. Kuro and Walking Route / 979C

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⋅1051≤n≤3⋅105, 1≤x,y≤n1≤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→1.

 

不想说题意了 好累。

有一颗绝对连通的树,然后存在x点和一个y点,经过x后,就不可以再经过y、或者达到y,问在这棵树上所有点之间可能存在的路径有多少条

 

对于这颗树,很明显可以知道x到y只有一条路径,我们的思路应该是找出所有任意点之间的路径条数 - 不满足的条数,就可以得到可行路径数目了。

首先在一棵树上任意节点的路径条数显然是1到n求和,即(n - 1)* n / 2,对于不满足的条数,我们察觉他们可能的连接方式就是左右两边红色的点相连,那么路径就是 3 * 4 , 可以dfs求出左边红色部分节点个数 加上 中间黑色的,用n 减去这个点数就可以得到右边,反之同理。

 

代码如下:

#include <bits/stdc++.h>


using namespace std;

typedef long long ll;
typedef pair<int,int>  P;

vector<vector<int> >v;
const int maxn = 4e5;

ll x,y,n;
ll vis[maxn];

ll dfs(int x,int fa,int en) {
    for(auto d:v[x]) {
        if(d != fa && d != en) {
            vis[x] += dfs(d,x,en);
        }
    }
    return vis[x];
}
int main() {
    while(cin >> n >> x >> y) {
        v.clear();
        v.resize(n + 500);
        for(int i = 1; i <= n; i++) vis[i] = 1;
        for(int i = 1; i < n; i++) {
            int u,vv;
            cin >> u >> vv;
            v[vv].push_back(u);
            v[u].push_back(vv);
        }
        ll ans1 = n * (n - 1);
        for(int i = 1; i <= n; i++) vis[i] = 1;
        ll  num1 = dfs(y,0,x);
        for(int i = 1; i <= n; i++) vis[i] = 1;
        ll num2 = dfs(x,0,y);
        num1 = n - num1;
        num2 = n - num2;
//       cout << num1 << endl;
//       cout << num2 << endl;
        ans1 -= num1 * num2;
        cout << ans1 << endl;
    }
    return 0;
}
//

7  1 3
1 2
1 3
2 4
2 5
3 6
3 7
//
//
//11 3 7
//1 2
//2 4
//2 3
//3 5
//5 7
//6 7
//7 8
//5 9
//9 10
//3 11


7 1 3
1 2
1 3
2 4
2 5
3 6
3 7
//

6 1 3
1 2
2 3
1 4
1 5
1 6

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值