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 $$$n$$$ towns, numbered from $$$1$$$ to $$$n$$$, and $$$n - 1$$$ bidirectional roads connecting these towns. It is possible to reach each town from any other. Each road connects two towns $$$a$$$ and $$$b$$$. Kuro loves walking and he is planning to take a walking marathon, in which he will choose a pair of towns $$$(u, v)$$$ ($$$u \neq v$$$) and walk from $$$u$$$ using the shortest path to $$$v$$$ (note that $$$(u, v)$$$ is considered to be different from $$$(v, u)$$$).

Oddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index $$$x$$$) and Beetopia (denoted with the index $$$y$$$). 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)$$$ if on the path from $$$u$$$ to $$$v$$$, 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 \leq n \leq 3 \cdot 10^5, 1 \leq x, y \leq n$$$, $$$x \ne y$$$) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.

$$$n - 1$$$ lines follow, each line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq n$$$, $$$a \ne 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
3 1 3
1 2
2 3
Output
5
Input
3 1 3
1 2
1 3
Output
4
Note

On the first example, Kuro can choose these pairs:

  • $$$(1, 2)$$$: his route would be $$$1 \rightarrow 2$$$,
  • $$$(2, 3)$$$: his route would be $$$2 \rightarrow 3$$$,
  • $$$(3, 2)$$$: his route would be $$$3 \rightarrow 2$$$,
  • $$$(2, 1)$$$: his route would be $$$2 \rightarrow 1$$$,
  • $$$(3, 1)$$$: his route would be $$$3 \rightarrow 2 \rightarrow 1$$$.

Kuro can't choose pair $$$(1, 3)$$$ since his walking route would be $$$1 \rightarrow 2 \rightarrow 3$$$, in which Kuro visits town $$$1$$$ (Flowrisa) and then visits town $$$3$$$ (Beetopia), which is not allowed (note that pair $$$(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)$$$: his route would be $$$1 \rightarrow 2$$$,
  • $$$(2, 1)$$$: his route would be $$$2 \rightarrow 1$$$,
  • $$$(3, 2)$$$: his route would be $$$3 \rightarrow 1 \rightarrow 2$$$,
  • $$$(3, 1)$$$: his route would be $$$3 \rightarrow 1$$$.

【题意】

给一个$$$n$$$个点,$$$n$$$-1条路径的连通无向图(其实就是树),求不会先经过$$$x$$$再经过$$$y$$$的路径$$$(u, v)$$$的个数,其中$$$(u, v)$$$和$$$(v, u)$$$被视为不同的路径。

【分析】

由于是在树上的操作,从$$$u$$$ 到$$$v$$$的路径是被$$$u,v$$$唯一确定的,所以转而求端点对的个数。

总端点个数有3e5,但是需要检查的点只有2个,遍历所有路径并不划算,不如转而用排除法,总路径数 - 不符合的路径数 = 答案

总路径数很好算,只需要根据组合原理 = $$$A^2_n = n*(n-1)$$$。不符合的路径数可以这么求——把$$$x$$$到$$$y$$$的路径看成一个整体,其他部分看成挂在路径上的,分为:挂在$$$x$$$上的部分,挂在$$$y$$$上的部分,以及挂在这个路径其他点上的部分,把$$$x$$$与挂在$$$x$$$上的部分合称为为$$$x$$$部分,把$$$y$$$和$$$y$$$相连的部分合称为$$$y$$$部分。

一条先经过$$$x$$$再经过$$$y$$$的路径,必定起于与$$$x$$$部分,经过$$$x$$$到$$$y$$$的路径,最后止于与$$$y$$$部分。对于其他路径,起点不在$$$x$$$部分的,经过了$$$x$$$后就不会再经过$$$y$$$了,因为它一定是从$$$x->y$$$的路径的中部走到$$$x$$$的,在经过$$$x$$$后就不能再回头了;终点不在$$$y$$$部分的,自然不会经过y。

所以不符合的路径数=$$$x$$$部分的大小*$$$y$$$部分的大小

上个图演示一下:x是1,y是3,那么从$$$\{1,2,4,5\}$$$到$$$\{6,8,9\}$$$的路径都必须经过$$$1\to 3\to 6$$$,而只要起点不是$$$\{1,2,4,5\}$$$或者终点不是$$$\{6,8,9\}$$$的路径都是可以走的。所以答案是9*8 - 4*3 = 60。

【代码】

#include<stdio.h>
#include<vector>
using std::vector;
#define N_max 100005
typedef long long ll;
int x,y;
vector<int> node[300005];
ll ans, n, cnt[2] = { 0 };
int vis[300005]; //从x出发,搜索到y的路径并把经过的点全部标记为1
int findy(int cur) {
if (cur != y) {
vis[cur] = -1;
int next;
for (int t = 0; t < node[cur].size(); ++t) {
next = node[cur][t];
if (vis[next] != -1)
if (1 == findy(next)) {
vis[cur] = 1; return 1;
}
}
}
if (cur == y) {
vis[cur] = 1; return 1;
}
return 0;//因为是连通图,一定能找到y,返回0是防止编译器检查到没有返回值
} //搜索去掉x->y路径后,与x连通的点的个数
void calx(int cur) {
if (vis[cur] == 2)return;
vis[cur] = 2;
cnt[0]++;
int next;
for (int t = 0; t < node[cur].size(); ++t) {
next = node[cur][t];
if (vis[next] != 1)
calx(next);
}
}
//搜索去掉x->y路径后,与y连通的点的个数
void caly(int cur) {
if (vis[cur] == 2)return;
vis[cur] = 2;
cnt[1]++;
int next;
for (int t = 0; t < node[cur].size(); ++t) {
next = node[cur][t];
if (vis[next] != 1)
caly(next);
}
} //上面的计数可以用一个函数实现的,比赛的时候写的比较无脑:(
int main() {
int a1, a2;
scanf("%lld %d %d", &n,&x,&y);
for (int i = 0; i < n-1; ++i) {
scanf("%d %d", &a1, &a2);
node[a1].emplace_back(a2);
node[a2].emplace_back(a1);
}
ans = n*(n - 1);
findy(x);
calx(x);
caly(y);
printf("%lld\n",ans-cnt[0]*cnt[1]);
}

codeforces 979 C. Kuro and Walking Route的更多相关文章

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

    C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  2. Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)

    Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...

  3. Kuro and Walking Route CodeForces - 979C (树上DFS)

    Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...

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

    题目链接:http://codeforces.com/contest/979/problem/C Kuro is living in a country called Uberland, consis ...

  5. 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时,此路不通.路 ...

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

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

  7. codeforces 979C Kuro and Walking Route

    题意: 给出一棵树,其中有两个点,x和y,限制走了x之后的路径上不能有y,问可以走的路径(u,v)有多少条,(u,v)和(v,u)考虑为两条不同的路径. 思路: 简单树形dp,dfs统计在x到y路径( ...

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

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

  9. CodeForces 979 D Kuro and GCD and XOR and SUM

    Kuro and GCD and XOR and SUM 题意:给你一个空数组. 然后有2个操作, 1是往这个数组里面插入某个值, 2.给你一个x, k, s.要求在数组中找到一个v,使得k|gcd( ...

随机推荐

  1. Conda常用命令整理

    主要参考Anaconda官方指南Using Conda:https://conda.io/docs/using/index.html 环境:Win10 64bit with conda 4.3.14  ...

  2. 5 使用ip代理池爬取糗事百科

    从09年读本科开始学计算机以来,一直在迷茫中度过,很想学些东西,做些事情,却往往陷进一些技术细节而蹉跎时光.直到最近几个月,才明白程序员的意义并不是要搞清楚所有代码细节,而是要有更宏高的方向,要有更专 ...

  3. Django Ajax的使用

    简介: AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. AJAX ...

  4. Redis安装和部署--LINUX

    一.安装 1.下载3.0.7稳定版本即可 地址:http://www.redis.cn/download.html 2.将 redis-3.0.7.tar.gz 压缩包拷贝到 opt 目录下 3.解压 ...

  5. 加装固态硬盘SSD

    参考:http://tieba.baidu.com/p/4224078869 1.首先拆开后盖,取出机械硬盘,把固定框换到固态盘上,把机械盘安装到硬盘托架上. 装上固态硬盘,然后把光驱位的塑料壳子拆下 ...

  6. 【MyBatis】MyBatis实现CRUD操作

    1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...

  7. android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升

    android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升

  8. Maven 阿里源

    由于一些不可抗拒因素,在使用 maven 的时候我们不得不需要改变一些设置,以加快我们的下载速度. ​ 仓库配置 ​ 在maven的settings.xml文件里的mirrors节点,添加如下子节点: ...

  9. Programming Assignment 1: WordNet

    编程作业一 作业链接:WordNet & Checklist 我的代码:WordNet.java & SAP.java & Outcast.java 这是第二部分的编程作业,因 ...

  10. Entity Framework 指定架构无效 错误:1052

    IIS发布网站:如果不发布放到IIS没有问题,发布后IIS部署 打开网站却提示指定架构无效 1052 找到很多解决的问题 1添加wenconfig 2.更改entity名的 其实我认为最简单的就是先找 ...