【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 ...
随机推荐
- ELK日志系统
ELK stack是又Elasticsearch,lostash,kibana 三个开源软件的组合而成,形成一款强大的实时日志收集分析展示系统. Logstash:日志收集工具,可以从本地磁盘,网络服 ...
- Paper List ABOUT Deep Learning
Deep Learning 方向的部分 Paper ,自用.一 RNN 1 Recurrent neural network based language model RNN用在语言模型上的开山之作 ...
- JS正则表达式从入门到入土(3)—— 范围类
范围类 在使用正则表达式时,很多时候,我们会想要匹配a~z的所有字母,很多人想到,可以使用字符类[abcdefg...z],但是,这种方法需要输入所有需要匹配的字母.那么,有没有简单点的方法呢? 所幸 ...
- 20145314郑凯杰 《Java程序设计》实验二 实验报告
20145314郑凯杰 <Java程序设计>实验二 实验报告 实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用 ...
- USB详解
USB作为一种串行接口,应用日益广泛.如同每个工程设计人员必须掌握I2C,RS232这些接口一样,我们也必须掌握USB.但是USB的接口协议实在有点费解,Linux UCHI驱动作者之一Alan St ...
- Spring笔记(二)
1. SPRING aop入门 Aop 面向切面编程 在一个大型的系统中,会写很多的业务类--业务方法 同时,一个大型的系统中,还有很多公共的功能:比如事务管理.日志处理.缓存处理..... 1.1 ...
- 学习记录:交叉编译环境配置(buildroot and gdb&gdbserver)【转】
本文转载自:https://blog.csdn.net/zhy025907/article/details/52332528 1,背景 因为参加公司的路由器逆向培训,首先需要的就是环境的配置准备工作, ...
- vue集成ueditor
相关代码见github 1.引入ueditor相关的文件,具体目录见下图如下 我将下载的文件放在static下面,这里专门用来放置相关的静态文件 (在ueditor.config.js需要配置一下路径 ...
- LCS最长共同子序列
2017-09-02 15:06:57 writer:pprp 状态表示: f(n,m)表示s1[0..n]和s2[0..m]从0开始计数,最终结果是f(N-1,M-1)考虑四种情况: 1/ s1[n ...
- redmine修改附件储存路径
如果想把redmine 1.x.x 版本中的attachments files 放在自定义的目录(例如/home/darkofday/redmineAttachFile/).执行下列命令:cd /ho ...