Codeforces Round #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C

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 n1n−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) (uvu≠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 nnxx and yy (1n3105,1x,yn1≤n≤3⋅105,1≤x,y≤nxyx≠y) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.

n1n−1 lines follow, each line contains two integers aa and bb (1a,bn1≤a,b≤naba≠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 121→2,
  • (2,3)(2,3): his route would be 232→3,
  • (3,2)(3,2): his route would be 323→2,
  • (2,1)(2,1): his route would be 212→1,
  • (3,1)(3,1): his route would be 3213→2→1.

Kuro can't choose pair (1,3)(1,3) since his walking route would be 1231→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 121→2,
  • (2,1)(2,1): his route would be 212→1,
  • (3,2)(3,2): his route would be 3123→1→2,
  • (3,1)(3,1): his route would be 313

一、原题地址

传送门


二、大致题意

    给出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();
}

阅读更多
想对作者说点什么? 我来说一句

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

关闭
关闭
关闭