Codeforces979C 、 Kuro and Walking Route(dfs)

版权声明:Amove? https://blog.csdn.net/Amovement/article/details/80323045

C. Kuro and Walking Route

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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⋅105,1≤x,y≤n1≤n≤3⋅105,1≤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.

Examples

input

Copy

3 1 3
1 2
2 3

output

Copy

5

input

Copy

3 1 3
1 2
1 3

output

Copy

4

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

On the second example, Kuro can choose the following pairs:

  • (1,2)(1,2): his route would be 1→21→2,
  • (2,1)(2,1): his route would be 2→12→1,
  • (3,2)(3,2): his route would be 3→1→23→1→2,
  • (3,1)(3,1): his route would be 3→13

 

一、原题地址

传送门

 

二、大致题意

    给出n个点,有n-1个边将他们链接。给出x,y,当某一路径中出现x....y时,此路不通。路径(u,v)和(v,u)是不同的。

 

三、思路

    n个点而只有n-1条边,所以不会出现环路径。点对数为n*(n-1)。此时我们可以先找出哪一些路径是不符合的,dfs一遍从x到y的路径并记录,这样就可以提取出(x,y)这段路中间的点。再分别从x,y为起点跑一遍dfs,可以统计出x,y两侧的点的个数cntx、cnty。

    画一张简图很好理解,答案就是总点对数减去x、y两侧点数的乘积,即为n*(n-1)-cntx*cnty。

 

四、代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf=0x3f3f3f3f;

const int maxn=300000+500;
int n,m,x,y;
vector<int>e[maxn];
bool vis[maxn],xoy[maxn];
int pre[maxn];
void init()
{
	memset(xoy,false,sizeof(xoy));
	memset(vis,false,sizeof(vis));
	memset(pre,-1,sizeof(pre));
	scanf("%d%d%d",&n,&x,&y);
	m=n-1;
	for(int i=0;i<m;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		e[u].push_back(v);
		e[v].push_back(u);
	}
}
void predfs(int u,int fa)
{
	pre[u]=fa;
	if(u==y)
		return ;
	int size=e[u].size();
	for(int i=0;i<size;i++)
	{
		int v=e[u][i]; 
		if(v!=fa)
		{
			predfs(v,u);
		}
	}
}
int dfs(int u)
{
	vis[u]=true;
	int size=e[u].size();
	int here=1;
	for(int i=0;i<size;i++)
	{
		int v=e[u][i];
		if(!vis[v]&&!xoy[v]&&v!=x&&v!=y)
		{
			here+=dfs(v);
		}
	}
	return here;
}
int main()
{
	init();		//初始化
	predfs(x,-1);//预处理记录x到y的路径
	for(int i=pre[y];i!=x;i=pre[i])
		xoy[i]=true;					//标记x到y的路径

	int cntx=dfs(x);	//统计x侧点数
	int cnty=dfs(y);	//统计y侧点数
	long long int ans=(long long)1*n*(n-1)-(long long)cntx*cnty;
	cout<<ans<<endl;
	getchar();
	getchar();
}

 

阅读更多

Random Walking

11-04

Problem DescriptionnThe Army of Coin-tossing Monkeys (ACM) is in the business of producing randomness. Good random numbers are important for many applications, such as cryptography, online gambling, randomized algorithms and panic attempts at solutions in the last few seconds of programming competitions.nnRecently, one of the best monkeys has had to retire. However, before he left, he invented a new, cheaper way to generate randomness compared to directly using the randomness generated by coin-tossing monkeys. The method starts by taking an undirected graph with 2n nodes labelled 0, 1, …, 2n - 1. To generate k random n-bit numbers, they will let the monkeys toss n coins to decide where on the graph to start. This node number is the first number output. The monkeys will then pick a random edge from this node, and jump to the node that this edge connects to. This new node will be the second random number output. They will then select a random edge from this node (possibly back to the node they arrived from in the last step), follow it and output the number of the node they landed on. This walk will continue until k numbers have been output.nnDuring experiments, the ACM has noticed that different graphs give different output distributions, some of them not very random. So, they have asked for your help testing the graphs to see if the randomness is of good enough quality to sell.nnThey consider a graph good if, for each of the n bits in each of the k numbers generated, the probability that this bit is output as 1 is greater than 25% and smaller than 75%.n nnInputnThe input will consist of several data sets. Each set will start with a line consisting of three numbers k, n, e separated by single spaces, where k is the number of n-bit numbers to be generated and e is the number of edges in the graph (1 ≤ k ≤ 100, 1 ≤ n ≤ 10 and 1 ≤ e ≤ 2000). The next e lines will consist of two space-separated integers v1, v2 where 0 ≤ v1, v2 < 2n and v1 ≠ v2. Edges are undirected and each node is guaranteed to have at least one edge. There may be multiple edges between the same pair of nodes.nThe last test case will be followed by a line with k = n = e = 0, which should not be processed.n nnOutputnFor each input case, output a single line consisting of the word Yes if the graph is good, and No otherwise.n nnSample Inputn10 2 3n0 3n1 3n2 3n5 2 4n0 1n0 3n1 2n2 3n0 0 0n nnSample OutputnNonYesn

Walking Race

10-25

Descriptionnnflymouse’s sister wc is very capable at sports and her favorite event is walking race. Chasing after the championship in an important competition, she comes to a training center to attend a training course. The center has N check-points numbered 1 through N. Some pairs of check-points are directly connected by two-way paths. The check-points and the paths form exactly a tree-like structure. The course lasts N days. On the i-th day, wc picks check-point i as the starting point and chooses another check-point as the finishing point and walks along the only simple path between the two points for the day’s training. Her choice of finishing point will make it that the resulting path will be the longest among those of all possible choices.nnAfter every day’s training, flymouse will do a physical examination from which data will obtained and analyzed to help wc’s future training be better instructed. In order to make the results reliable, flymouse is not using data all from N days for analysis. flymouse’s model for analysis requires data from a series of consecutive days during which the difference between the longest and the shortest distances wc walks cannot exceed a bound M. The longer the series is, the more accurate the results are. flymouse wants to know the number of days in such a longest series. Can you do the job for him?nnInputnnThe input contains a single test case. The test case starts with a line containing the integers N (N ≤ 106) and M (M < 109). Then follow N − 1 lines, each containing two integers fi and di (i = 1, 2, …, N − 1), meaning the check-points i + 1 and fi are connected by a path of length di.nnOutputnnOutput one line with only the desired number of days in the longest series.nnSample Inputnn3 2n1 1n1 3nSample Outputnn3

没有更多推荐了,返回首页