code forces 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 uuusing 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 31 22 3

output

Copy

5

input

Copy

3 1 31 21 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.

#include<cmath>
#include<queue>
#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=3e5+10;
vector<int> g[N];
int num[N];
int par[N];
int n,x,y;
int dfs(int u,int fa){
    par[u]=fa;
    int cnt=0;
    for(int v:g[u]){
        if(v!=fa){
            cnt+=dfs(v,u);
        }
    }
    return num[u]=cnt+1;
}
int main() {
    scanf("%d%d%d",&n,&x,&y);
    for(int i=0;i<n-1;i++){
        int u,v;
        scanf("%d%d",&u,&v);
                g[u].push_back(v);
                g[v].push_back(u);
    }
    dfs(x,-1);
    long long ans=0;
    int m=y;
    while(par[m] !=x){
        m=par[m];
    }
    ans=(long long )n*(n-1);
    ans-=(long long )num[y]*(n-num[m]);
    printf("%lld\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/buerdepepeqi/p/9042912.html

时间: 2024-11-06 05:03:15

code forces 979C的相关文章

贪心-Code forces -387B -George and Round

Code forces -387B -George and Round description George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer 

Code Forces Gym 100971D Laying Cables(单调栈)

D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. Whe

Code Forces Gym 100886J Sockets

J - Sockets Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Valera has only one electrical socket in his flat. He also has m devices which require electricity to work. He's got n plug multipliers to plug the devices, the

code forces 140F New Year Snowflake

F. New Year Snowflake As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true. The New Year snowflake consists of t

Code Forces 414B 很不错的双手,以促进合规

http://codeforces.com/problemset/problem/414/B 题目挺不错的.留个纪念,活动脑筋不错的题目 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<stack>

code forces 148D Bag of mice (概率DP)

time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies

code forces 990C

http://codeforces.com/contest/990/problem/C C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A bracket sequence is a string containing only charac

code forces 994C

C. Two Squares time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coo

code forces 999C Alphabetic Removals

C. Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly kk characters (k