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) (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) 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 (1≤n≤3⋅105,1≤x,y≤n1≤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 (1≤a,b≤n1≤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 1→2,
  • (2,3)(2,3): his route would be 2→3,
  • (3,2)(3,2): his route would be 3→2
  • (2,1)(2,1): his route would be 2→1,
  • (3,1)(3,1): his route would be 3→2→1.

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

题意 给出一颗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+ ,mod = ,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]=;
siz[k]=;
fa[k]=f;
for(int i=;i<g[k].size();i++)
{
int u=g[k][i];
if(vis[u]==&&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]==)
return ;
else
return dfs2(fa[k1],k2);
}
int main()
{
cin>>n>>x>>y;
for(int i=;i<n-;i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
memset(siz,,sizeof(siz));
memset(vis,,sizeof(vis));
dfs1(,);
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==)
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-)-ans<<endl;
}

Codeforces Round #482 (Div. 2) C Kuro and Walking Route的更多相关文章

  1. Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)

    题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...

  2. 【Trie】【枚举约数】Codeforces Round #482 (Div. 2) D. Kuro and GCD and XOR and SUM

    题意: 给你一个空的可重集,支持以下操作: 向其中塞进一个数x(不超过100000), 询问(x,K,s):如果K不能整除x,直接输出-1.否则,问你可重集中所有是K的倍数的数之中,小于等于s-x,并 ...

  3. Codeforces Round #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C

    题目链接:http://codeforces.com/contest/979/problem/C 大致题意 给出n个点,有n-1个边将他们链接.给出x,y,当某一路径中出现x....y时,此路不通.路 ...

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

    题目连接:http://codeforces.com/contest/979/problem/C 解题心得: 题意就是给你n个点,在点集中间有n-1条边(无重边),在行走的时候不能从x点走到y点,问你 ...

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

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把x..y这条路径上的点标记一下. 然后从x开始dfs,要求不能走到那些标记过的点上.记录节点个数为cnt1(包括x) 然后从y开始 ...

  6. Codeforces Round #482 (Div. 2) :B - Treasure Hunt

    题目链接:http://codeforces.com/contest/979/problem/B 解题心得: 这个题题意就是三个人玩游戏,每个人都有一个相同长度的字符串,一共有n轮游戏,每一轮三个人必 ...

  7. Codeforces Round #482 (Div. 2) B题

    题目链接:http://codeforces.com/contest/979/problem/B B. Treasure Hunt time limit per test1 second memory ...

  8. 【枚举】【贪心】Codeforces Round #482 (Div. 2) B. Treasure Hunt

    题意:给你3个字符串,3个人各对自己的字符串执行n轮操作,每一次选择一个字符变为任意一个和原来不同的字符.最后问你谁能使自己的串中的任意重复子串出现的次数最大化. 显然只需关注字符而非子串. 枚举每个 ...

  9. Codeforces Round #482 (Div. 2) B、Treasure Hunt(模拟+贪心)979B

    题目 大致题意 n表示要进行n次操作,接着给出三个字符串,表示三个人初始拥有的串.每次操作要替换字符串中的字母,询问最后在游戏中曾出现过的相同的子串谁最多. 思路 (1)  讨论最多的子串,肯定是全部 ...

随机推荐

  1. C#特性的介绍及应用场景

    1.特性的任务:特性就是为了支持对象添加一些自我描述的信息,不影响类封装的前提添加额外信息.如果你用这个信息,那特性就有用:如果你不需要这个信息,那么这个特性就没用. 2.特性的基类:Attribut ...

  2. JDK常用类解读--String

    一.字符串的不变性: 文章使用的源码是jdk1.8的.(下同) 1.首先可以看到`String`是`final`类,说明该类不可继承,保证不会被子类改变语义 2.String的值实际上就是一个字符数组 ...

  3. AJPFX学习Java函数知识总结

    函 数:为了提高代码的复用性,可以将其定义成一个单独的功能,该功能的体现就是java中的函数.函数就是体现之一. java中的函数的定义格式:         修饰符 返回值类型 函数名(参数类型 形 ...

  4. AJPFX关于数组获取最值的思路和方法

    思路分析:1.定义一个变量(max,初始值一般为数组中的第一个元素值),用来记录最大值.2.遍历数组,获取数组中的每一个元素,然后依次和max进行比较.如果当前遍历到的元素比max大,就把当前元素值给 ...

  5. 【intellij idea】汇总

    1 右键无法创建,找不到scala class https://blog.csdn.net/u011513853/article/details/52896230 2 缩进 https://jingy ...

  6. 直接插入排序法原理及其js实现

    直接插入排序法就像我们打扑克牌时整理牌面一样,先让我们脑补一下我们打牌的过程. 首先摸了一张6, 接着摸到一张4,比6小,插到6的前面: 又摸到一张7,比6大,插到6的后面: 又摸到一张5,比6小,比 ...

  7. 微信小程序组件解读和分析:十二、picker滚动选择器

    picker滚动选择器组件说明: picker: 滚动选择器,现支持三种选择器,通过mode属性来区分, 分别是普通选择器(mode = selector),时间选择器(mode = time),日期 ...

  8. H.264学习笔记1——相关概念

    此处记录学习AVC过程中的一些基本概念,不定时更新. frame:帧,相当于一幅图像,包含一个亮度矩阵和两个色度矩阵. field:场,一帧图像,通过隔行扫描得到奇偶两场,分别称为顶场和底场或奇场和偶 ...

  9. SQL 索引自动维护计划脚本

    脚本功能: 1,查询数据库中,碎片率在5%以上(官方推荐),有一定数据里的表的索引. 2.如果碎片率在5%<碎片率<=30%  执行重新组织索引.如果在30%以上,执行重建索引 建议在执行 ...

  10. 新手写的一个DBCP工具类

    package com.xx.questionnaire.util.dao; import java.io.IOException; import java.sql.Connection; impor ...