Codeforces Round #482 (Div. 2) C. Kuro and Walking Route(dfs,思路)

描述

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 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
(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.

input

3 1 3
1 2
2 3

output

5

input

3 1 3
1 2
1 3

output

4

思路

先说题意,给了一个有n个节点的树,有n-1条无向边,又给出一个起点st和一个终点ed,让你任意选两个点(u,v),从u到v,途中不能经过(st,ed)这一条路,因为st有花,ed有蜜蜂,如果先走st点再经过ed点,就会被蜜蜂蛰到,认为(u,v)(v,u)是两条不同的路,题目问满足给出条件的有多少对点能不被蜜蜂蛰到

那么任意选择两个点的组合的方法就为 A n 2 种,那么我们用dfs处理出来每一个点的儿子总数num[],再记录一下每个点的父亲。只需要求出st前边的点的个数a,和ed后边的点的个数b,用 n ( n 1 ) a b 就是答案

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define inf 0x3f3f3f3f
typedef long long ll;
const ll N = 3e5 + 20;
ll n, st, ed;
ll num[N], pre[N];
vector<ll> e[N];
ll dfs(ll u, ll fa)
{
    pre[u] = fa;
    ll cnt = 0;
    for (auto v : e[u])
        if (v != fa)
            cnt += dfs(v, u);
    num[u] = cnt + 1;
    return num[u];
}
int main()
{
    //freopen("in.txt", "r", stdin);
    ll u, v;
    scanf("%lld%lld%lld", &n, &st, &ed);
    for (ll i = 1; i <= n - 1; i++)
    {
        scanf("%lld%lld", &u, &v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    dfs(st, -1);
    ll son = ed;
    while (pre[son] != st)
        son = pre[son];
    ll ans = n * (n - 1);
    ans -= num[ed] * (n - num[son]);
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/riba2534/article/details/80324956