• 【codeforces-482div2-C】Kuro and Walking Route(DFS)


    题目链接:http://codeforces.com/contest/979/problem/C

    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 (1n31051≤n≤3⋅105, 1x,yn1≤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.

    input

    3 1 3
    1 2
    2 3

    output

    5
    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).

    题意

    无向图,n个点,n-1条边,每两个点都可以到达,但是从依次经过u,v两点的道路不能走,问有多少个x->y可以到达

     思路

    ans = 总路线条数 - u到v的路线数。u到v路线数 = u端的点数*v端的点数。判断点数用dfs。或者用SPFA记录u到v的所有点,再分别dfs u 和 v

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N = 3e5+4;
    bool vis[N];
    LL n, ans1 = 0, ans2 = 0, u, v, pre;
    vector<int>V[N];
    void dfs1(LL s, LL x)
    {
        vis[s] = 1;
        if(s == v)
        {
            pre = x;
            return;
        }
        ans1++;
        for(LL i = 0; i < V[s].size(); i++)
        {
            LL k = V[s][i];
            if(vis[k]) continue;
            dfs1(k, s);
        }
    }
    void dfs2(int s)
    {
        vis[s] = 1;
        if(s == u || s == v)
            return;
        ans2++;
        for(LL i = 0; i < V[s].size(); i++)
        {
            LL k = V[s][i];
            if(vis[k]) continue;
            dfs2(k);
        }
    }
    int main()
    {
        LL a, b;
        scanf("%lld%lld%lld", &n, &u, &v);
        for(LL i = 1; i < n; i++)
        {
            scanf("%lld%lld", &a, &b);
            V[a].push_back(b);
            V[b].push_back(a);
        }
        dfs1(u, u);
        memset(vis, 0, sizeof vis);
        dfs2(pre);
        printf("%lld
    ", n*(n-1)-(ans1-ans2)*(n-ans1));
        return 0;
    }
  • 相关阅读:
    SCA与spring集成(在spring中开发SOA)
    jdbc 预编译处理 和spring返回自增主键值
    JavaScript异步编程__“回调地狱”的一些解决方案
    高性能滚动 scroll 及页面渲染优化
    程序员如何学习英语
    基于JavaScript实现验证码功能
    HTML上传文件的多种方式
    WEB前端开发规范文档
    JavaScript跨域总结与解决办法
    学习使用:before和:after伪元素
  • 原文地址:https://www.cnblogs.com/lesroad/p/9135635.html
Copyright © 2020-2023  润新知