1. 程式人生 > >Codeforces Round #482 (Div. 2) C Kuro and Walking Route

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

any HA msu OS rom element res size false

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) (uvu≠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) 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 (1n3?105,1x,yn1≤n≤3?105,1≤x,y≤n, x≠y) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.

n?1lines follow, each line contains two integers a and b (1a,bn1≤a,b≤n, a≠b), 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)(1,2): his route would be 12,
  • (2,3)(2,3): his route would be 23,
  • (3,2)(3,2): his route would be 32
  • (2,1)(2,1): his route would be 21,
  • (3,1)(3,1): his route would be 321.

Kuro can‘t choose pair (1,3)(1,3) since his walking route would be 1231→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 12,
  • (2,1)(2,1): his route would be 21,
  • (3,2)(3,2): his route would be 3→1→2,
  • (3,1)(3,1): his route would be 31.

題意 給出一顆n個定點的樹 樹上有兩個點 想 x,y 任意兩點相互可達 但u到v的路徑上先經過x再經過y是不允許的 (u,v)和(v , u)是兩條不相同的路徑

問滿足上述條件的路徑有多少條

解析

我們選定一個點作為根節點1 size[ i ]表示 以i為根節點的子樹大小

若x的祖先沒有y y 的祖先沒有x 那麽答案就是size[x]*size[y]

若x 的祖先有y 那麽我們要找x的祖先 且 是y的兒子的那個節點 fa 答案就是(n-size[fa])*size[x]

同理得到另一種情況

AC代碼

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+50 ,mod = 998244353,inf=0x3f3f3f3f;
const double pi=acos(-1.0);
typedef long long ll;
vector<int> g[maxn];
ll siz[maxn],fa[maxn];
ll n,x,y;
int vis[maxn];
void dfs1(int k,int f)
{
    vis[k]=1;
    siz[k]=1;
    fa[k]=f;
    for(int i=0;i<g[k].size();i++)
    {
        int u=g[k][i];
        if(vis[u]==0&&u!=f)
        {
            dfs1(u,k);
            siz[k]+=siz[u];
        }
    }
}
int dfs2(int k1,int k2)
{
    if(fa[k1]==k2)
        return k1;
    else if(fa[k1]==0)
        return 1;
    else
        return dfs2(fa[k1],k2);
}
int main()
{
    cin>>n>>x>>y;
    for(int i=0;i<n-1;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    memset(siz,0,sizeof(siz));
    memset(vis,0,sizeof(vis));
    dfs1(1,0);
    ll ans;
    int f1=dfs2(x,y),f2=dfs2(y,x);
    //cout<<f1<<" "<<f2<<endl;
    if(f1==f2)
        ans=siz[x]*siz[y];
    else if(f1==1)
        ans=(n-siz[f2])*siz[y];
    else
        ans=(n-siz[f1])*siz[x];
//    for(int i=1;i<=n;i++)
//        cout<<i<<" "<<fa[i]<<endl;
    cout<<n*(n-1)-ans<<endl;
}

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