zoukankan      html  css  js  c++  java
  • Kuro and Walking Route

    Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11 to nn, and n1n−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)(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 (1n3105,1x,yn1≤n≤3⋅105,1≤x,y≤n, xyx≠y) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.

    n1n−1 lines follow, each line contains two integers aa and bb (1a,bn1≤a,b≤n, aba≠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 121→2,
    • (2,3)(2,3): his route would be 232→3,
    • (3,2)(3,2): his route would be 323→2,
    • (2,1)(2,1): his route would be 212→1,
    • (3,1)(3,1): his route would be 3213→2→1.

    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 121→2,
    • (2,1)(2,1): his route would be 212→1,
    • (3,2)(3,2): his route would be 3123→1→2,
    • (3,1)(3,1): his route would be 313→1.

    容易让人想到是搜索,不过怎么搜呢?

    答案:以x为根,搜y有多少个子节点,再以y为根,搜x有多少个子节点。总方案数减去两个子节点乘积,就是答案了。

    #include <iostream>
    #include <bits/stdc++.h>
    #define maxn 600005
    typedef long long ll;
    using namespace std;
    struct edge
    {
        int u;
        int v;
        int next;
    }edge[maxn];
    int head[maxn];
    int cnt=0;
    int x,y;
    void add(int u,int v)
    {
        edge[cnt].u=u;
        edge[cnt].v=v;
        edge[cnt].next=head[u];
        head[u]=cnt++;
    }
    int sizes[maxn];
    bool vis[maxn];
    void dfs(int num)
    {    int flag=0;
         for(int i=head[num];i!=-1;i=edge[i].next)
         {
           if(!vis[edge[i].v])
           {   vis[edge[i].v]=true;
               dfs(edge[i].v);
               sizes[num]+=sizes[edge[i].v];
               flag=1;
           }
         }
         if(flag==0)
           {
               sizes[num]=1;
           }
        else
        {
            sizes[num]++;
        }
    }
    int main()
    {
        memset(head,-1,sizeof(head));
        ll n;
        scanf("%I64d%d%d",&n,&x,&y);
        for(int i=0;i<n-1;i++)
        {   int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        ll sum1=0,sum2=0;
        vis[x]=true;
        dfs(x);
        sum1=sizes[y];
        memset(sizes,0,sizeof(sizes));
        memset(vis,0,sizeof(vis));
        vis[y]=true;
        dfs(y);
        sum2=sizes[x];
        //cout<<sum1<<" "<<sum2<<endl;
        ll ans=n*(n-1);
        ans=ans-sum1*sum2;
        cout<<ans<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    DAY09(for语句)
    Python 下载 tushare 数据,然后调用 C++ DLL 计算 wMA 存入本地 csv 文件再 python 读取
    Python socket编程之八:阶段性总结
    Ctex + Lyx = 自用 Latex 编辑环境
    matlab下二重积分的蒙特卡洛算法
    实验9 根据材料编程
    实验5 编写、调试具有多个段的程序
    实验4 [bx]和loop的使用
    实验3 编程、编译、连接、跟踪
    汇编语言第三章总结
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/9050280.html
Copyright © 2011-2022 走看看