CF - 813C. The Tag Game - dfs

1.题目描述:

C. The Tag Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of n vertices. Vertex 1 is the root of the tree.

Alice starts at vertex 1 and Bob starts at vertex x (x ≠ 1). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.

The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.

You should write a program which will determine how many moves will the game last.

Input

The first line contains two integer numbers n and x (2 ≤ n ≤ 2·1052 ≤ x ≤ n).

Each of the next n - 1 lines contains two integer numbers a and b (1 ≤ a, b ≤ n) — edges of the tree. It is guaranteed that the edges form a valid tree.

Output

Print the total number of moves Alice and Bob will make.

Examples
input
4 3
1 2
2 3
2 4
output
4
input
5 2
1 2
2 3
3 4
2 5
output
6
Note

In the first example the tree looks like this:

The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:

B: stay at vertex 3

A: go to vertex 2

B: stay at vertex 3

A: go to vertex 3

In the second example the tree looks like this:

The moves in the optimal strategy are:

B: go to vertex 3

A: go to vertex 2

B: go to vertex 4

A: go to vertex 3

B: stay at vertex 4

A: go to vertex 4


2.题意概述:

给你一颗有根数,Alice在根节点(编号为1),Bob在编号为x的节点。Alice想追到Bob,但是Bob不希望Alice追上他(mdzz怪不得单身)。问你在这种策略下,他们走的步数和的最大值。

3.解题思路:

我们考虑Bob,无非就三种选择:

①待在原地不动。

②如果有叶子,继续往叶子走下去。

③往根处走。

我们可以考虑枚举终结的地方,如果Alice能比Bob早到达这个地方,那么GG,否则就更新最大值。

求树内的距离,可以两遍dfs,第一遍从Alice开始dfs记录她到每个节点的距离,第二遍再对Bob去dfs。总的复杂度是O(N)。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 200010
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int dep[maxn][2], head[maxn];
int n, x, cnt;
struct node
{
	int to, next;
} E[maxn << 1];
inline bool scan_d(int &num)
{
	char in;bool IsN=false;
	in=getchar(); if(in==EOF) return false;
	while(in!='-'&&(in<'0'||in>'9')) in=getchar();
	if(in=='-'){ IsN=true;num=0;} else num=in-'0';
	while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}
	if(IsN) num=-num; return true;
}
inline void init()
{
	memset(head, -1, sizeof head);
	cnt = 0;
}
inline void addedge(int u, int v)
{
	E[cnt].to = v;
	E[cnt].next = head[u];
	head[u] = cnt++;
}
void dfs(int u, int fa, int step, int id)
{
	dep[u][id] = step;
	for (int i = head[u]; i != -1; i = E[i].next)
	{
		int v = E[i].to;
		if (v != fa)
			dfs(v, u, step + 1, id);
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	init();
	scan_d(n);
	scan_d(x);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v;
		scan_d(u);
		scan_d(v);
		addedge(u, v);
		addedge(v, u);
	}
	dfs(1, 1, 0, 0);
	dfs(x, x, 0, 1);
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
//		printf("%d : %d\n", dep[i][0], dep[i][1]);
		if (dep[i][0] > dep[i][1])
			ans = max(ans, dep[i][0]);
	}
	printf("%d\n", ans << 1);
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值