C. Kuro and Walking Route+dfs+组合思维

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ujn20161222/article/details/80321547
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 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
Note

On the first example, Kuro can choose these pairs:

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

Kuro can't choose pair (1,3)

since his walking route would be 123, in which Kuro visits town 1 (Flowrisa) and then visits town 3 (Beetopia), which is not allowed (note that pair (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)
: his route would be 12, (2,1): his route would be 21, (3,2): his route would be 312, (3,1): his route would be 31
  • .

#define happy

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define pb push_back
ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int X,Y;
const int N=3e5+10;
int gpar[N],par[N],sz[N];
bool dchk[N];
vector<int> conn[N];

void dfs(int n){
    if(n!=Y){
        if(par[n]==Y)gpar[n]=n;
        else gpar[n]=gpar[par[n]];
    }
    dchk[n]=true;
    sz[n]=1;
    for(auto it:conn[n]){
        if(dchk[it])continue;
        par[it]=n;
        dfs(it);
        sz[n]+=sz[it];
    }
}



int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    int n=rd();
    X=rd(),Y=rd();
    rep(i,1,n-1){
        int x=rd(),y=rd();
        conn[x].pb(y);
        conn[y].pb(x);
    }
    dfs(Y);

    int c1=sz[X],c2=n-sz[gpar[X]];
    ll ans=(ll)n*(n-1)-(ll)c1*c2;
    return printf("%lld\n",ans),0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页