【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 ...
随机推荐
- 300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 好的博客参考之Spring
https://blog.csdn.net/bao19901210/article/details/41724355
- 【转】安装Vue.js的方法
安装vue.js的方法 一.简介 Vue.js 是什么 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量 ...
- Python面试题之Python面向对象编程汇总
面向对象的设计思想是从自然界中来的,因为在自然界中,类(Class)和实例(Instance)的概念是很自然的.Class是一种抽象概念,比如我们定义的Class——Student,是指学生这个概念, ...
- 20145302张薇《Java程序设计》实验二报告
20145302张薇<Java程序设计>实验二:Java面向对象程序设计 使用TDD的方式设计实现复数类:Complex 测试代码 import org.junit.Test; publi ...
- C++ 程序崩溃时生成Dump文件
#include <DbgHelp.h> //生产DUMP文件 int GenerateMiniDump(HANDLE hFile, PEXCEPTION_POINTERS pExcept ...
- Android studio 运行模拟器报:Application Installation Failed
前两天笔记本加了个SSD硬盘,原机械硬盘移植到光驱位,硬盘盘符都变了,结果在用android studio 运行以前的程序编译不报错,运行模拟器就会报如下错误. Installation failed ...
- python 这个stdin怎么写
!/usr/bin/env python -- coding: utf-8 -- import json import pprint import sys reload(sys) sys.setdef ...
- CA证书,https讲解
关于具体连接过程,https://blog.csdn.net/wangjun5159/article/details/51510594 这篇博客写的应该比较准确. 我的理解,其中关键的一点是 http ...
- nginx 开启rewrite thinkcmf
server{ ... 省略 location / { index index.php index.html index.htm; #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则 if ...