【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 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
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)的更多相关文章
- 【Codeforces Round #482 (Div. 2) C】Kuro and Walking Route
		
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把x..y这条路径上的点标记一下. 然后从x开始dfs,要求不能走到那些标记过的点上.记录节点个数为cnt1(包括x) 然后从y开始 ...
 - 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 ...
 - 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 ...
 - 【Codeforces Rockethon 2014】Solutions
		
转载请注明出处:http://www.cnblogs.com/Delostik/p/3553114.html 目前已有[A B C D E] 例行吐槽:趴桌子上睡着了 [A. Genetic Engi ...
 - 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 ...
 - 【57.97%】【codeforces Round #380A】Interview with Oleg
		
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
 - 【42.86%】【Codeforces Round #380D】Sea Battle
		
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
 - 【26.83%】【Codeforces Round #380C】Road to Cinema
		
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
 - 【17.76%】【codeforces round 382C】Tennis Championship
		
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
 
随机推荐
- SSIS 2012 Error: An Integration Services class cannot be found
			
升级SSIS到SQL Server 2012,服务器只安装了SSIS一个功能,应用程序执行dtsx包时报错如下: An Integration Services class cannot be fou ...
 - $Python技巧大全
			
知乎上有一个问题:Python 有什么奇技淫巧?其中有各种不按套路出牌的招数,也不乏一些惊为天人的"奇技淫巧",会让你大呼:居然还有这种操作??? 本文就是对日常使用过的或者觉得很 ...
 - [原创]CSS 去掉点li 的点 使得LI前面的点不在显示
			
我对博客的认识是:记录问题,解决问题,分享知识.如果有轮子,我不需要造轮子. 1.问题解决方式: 设置属性:li {list-style-type:none;} 2.更多属性参数参考 list-sty ...
 - web.xml servlet、servlet-mapping配置
			
Servlet常称为服务器端小程序,即运行在服务器端的程序,用于处理及响应客户的请求. Servlet类是个特殊的java类,继承于HttpServlet. --------------------- ...
 - [Linux 006]——grep和正则表达式
			
在使用系统时,我们或多或少的有一些搜索.查找的需求,必须要在文本中搜索某个关键字,或者过滤出文本中某些特定的行.grep 命令就为我们提供了这样一个功能,同时,grep 还可以使用正则表达式进行匹配, ...
 - 【读书笔记】《深入浅出nodejs》第四章 异步编程
			
1. 异步编程的基础 -- 函数式编程 (1)高阶函数 -- 是可以把函数作为参数,或是将函数作为返回值的函数. (2)偏函数用法 -- 创建一个调用另外一个部分 -- 参数或变量已经预置的函数 -- ...
 - PHP中的strtotime()函数
			
参见: http://www.phppan.com/2011/06/php-strtotime/ http://developer.51cto.com/art/201107/275478.htm PH ...
 - KVM配置及维护
			
kvm使用场景 1.公司测试环境/开发环境 测试开发环境可以使用配置低点的物理机就可以 2.公司生产环境 一般小公司没有私有云或容器团队,运维人员可能就1-2个,然后公司也不舍得花钱买商业化的私有云. ...
 - JAVA多线程本质分析
			
多线程是Java开发中的重中之重,其重要性和难度,可见一斑.掌握并精通多线程开发,是每一个程序员的必修之课.哪怕中间的过程很痛苦,只要坚持了,并最终豁然开朗了,都是一种升华. 多线程的优化:合理利用C ...
 - spring junit4 测试
			
@Service @ContextConfiguration(locations = { "classpath:config/applicationContext.xml" }) ...