题目链接: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 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⋅1051≤n≤3⋅105, 1≤x,y≤n1≤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
Note

On the first example, Kuro can choose these pairs:

  • (1,2)(1,2): his route would be 1→21→2,
  • (2,3)(2,3): his route would be 2→32→3,
  • (3,2)(3,2): his route would be 3→23→2,
  • (2,1)(2,1): his route would be 2→12→1,
  • (3,1)(3,1): his route would be 3→2→13→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).

题意

无向图,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+;
bool vis[N];
LL n, ans1 = , ans2 = , u, v, pre;
vector<int>V[N];
void dfs1(LL s, LL x)
{
vis[s] = ;
if(s == v)
{
pre = x;
return;
}
ans1++;
for(LL i = ; i < V[s].size(); i++)
{
LL k = V[s][i];
if(vis[k]) continue;
dfs1(k, s);
}
}
void dfs2(int s)
{
vis[s] = ;
if(s == u || s == v)
return;
ans2++;
for(LL i = ; 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 = ; i < n; i++)
{
scanf("%lld%lld", &a, &b);
V[a].push_back(b);
V[b].push_back(a);
}
dfs1(u, u);
memset(vis, , sizeof vis);
dfs2(pre);
printf("%lld\n", n*(n-)-(ans1-ans2)*(n-ans1));
return ;
}

【codeforces-482div2-C】Kuro and Walking Route(DFS)的更多相关文章

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

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

  2. codeforces 979 C. Kuro and Walking Route

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

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

  4. 【Codeforces Rockethon 2014】Solutions

    转载请注明出处:http://www.cnblogs.com/Delostik/p/3553114.html 目前已有[A B C D E] 例行吐槽:趴桌子上睡着了 [A. Genetic Engi ...

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

  6. 【57.97%】【codeforces Round #380A】Interview with Oleg

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【42.86%】【Codeforces Round #380D】Sea Battle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【26.83%】【Codeforces Round #380C】Road to Cinema

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【17.76%】【codeforces round 382C】Tennis Championship

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. sgu 100 A+B 解题报告及测试数据

    100.A+B time limit per test: 0.25 sec. memory limit per test: 65536 KB 题解:上手题,不解释. 直接上代码: #include & ...

  2. adb常用操作

    1.安装程序 adb -s serialno install -r path 2.切换电源 adb -s serialno shell input keyevent 26 3.主页键 adb -s s ...

  3. ERROR 1093 (HY000): You can't specify target table 'test' for update in FROM clause

    MySQL执行更新语句报错: 更新语句:UPDATE test SET state=50 WHERE num IN(SELECT num FROM test WHERE state=60): 报错:E ...

  4. 先记录一下吧 开始的程序 hello!java!

    起床后就跟着老师的教学,也稍微学了一些,刚开始用java. 一堆大小写字母注意不过来,很尴尬. 虽然只是成功了一个"hello java "的简单的不能再简单的小程序,不过还是有点 ...

  5. Spring MVC工作流程图

    图一   图二    Spring工作流程描述       1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获:       2. Disp ...

  6. Centos下ftp协议连接远程ftp server主机

    环境说明 [root@Check3 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@Check3 ~]# uname -a L ...

  7. Commons FileUpload

    转载自(https://my.oschina.net/u/2000201/blog/486744) 1    概述 Commons FileUpdate包很容易为你的Servlet和web应用程序添加 ...

  8. Servlet3.0整合Springmvc(注解版)

    在创建maven的web工程时候,如果报错缺少web.xml 则在pom添加如下配置 : <build> <plugins> <plugin> <groupI ...

  9. Linux 服务器buff/cache清理

    使用Top命令查看内存及缓冲区使用情况 当磁盘频繁产生IO时会导致buff/cache占用很高的内存,导致可用物理内存很少 但是当真正需要内存时,缓冲区内存会自动释放. 如果需要清理可以用 cache ...

  10. 在.Net中进行SQL Server数据库备份与还原操作实用类

    #region 类说明 //----------------------------------------------------------------------------- // // 项目 ...