【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 ...
随机推荐
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1
地址:http://codeforces.com/contest/768/problem/B 题目: B. Code For 1 time limit per test 2 seconds memor ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (上下界网络流)
正解: #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN=1 ...
- [Python]返回函数,装饰器拾遗
def lazy_print(*args): def pr(): print(args) return pr 当我们调用lazy_print()时,返回的并不是求和结果,而是求和函数: >> ...
- Tasks in parallel
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- linux 用到的命令
1. cd / 直接到服务器根目录 2. pwd 查看 当前所在目录 3. ll 查看当前文件下文件列表 4.ls 查看当前文件下文件,横向排列 5.ps -ef | gre ...
- python requests的使用说明
#GET参数实例 requests.get('http://www.dict.baidu.com/s', params={'wd': 'python'}) #或 url = 'http://www.b ...
- C++编程中设置文件长度的方法
转自:http://blog.csdn.net/rrrfff/article/details/6705685 bool SetFileLength(const char *FilePath, off_ ...
- SQL Server 对XML数据类型的SQL语句总结
--创建XMLTable , ) primary key, XMLCol xml); go ------------------------------------------------------ ...
- clientWidth offsetWidth scrollWidth
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- centos7 firewall 操作
一.firewall配置 The configuration for firewalld is stored in various XML files in /usr/lib/firewalld/ a ...