Codeforces Round 668 (Div. 1) B题 Tree Tag

题目链接

https://codeforces.com/problemset/problem/1404/B

思路

只有三种情况Alice会赢:

  1. d i s ( a , b ) ≤ d a dis(a,b) \le da dis(a,b)da,第一步就直接获胜
  2. d a × 2 ≥ d b da \times 2 \ge db da×2db,可以不断逼近Bob
  3. d a × 2 ≤ 树的直径 da \times 2 \le 树的直径 da×2树的直径,此时只需要先占据直径中点即可(参考第一个样例)

否则,Bob会赢。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int n, a, b, da, db;
int dis[N];
vector<int>mp[N];
void dfs(int u, int fu, int dist)
{
	dis[u] = dist;
	for (int j : mp[u])
	{
		if (j == fu) continue;
		dfs(j, u, dist + 1);
	}
}
void solve()
{
	cin >> n >> a >> b >> da >> db;
	for (int i = 1; i <= n; i++)
	{
		mp[i].clear();
	}
	for (int i = 1, u, v; i < n; i++)
	{
		cin >> u >> v;
		mp[u].push_back(v);
		mp[v].push_back(u);
	}
	if (da * 2 >= db)
	{
		cout << "Alice" << endl;
		return;
	}
	dfs(a, -1, 0);
	if (dis[b] <= da)
	{
		cout << "Alice" << endl;
	}
	else
	{
		int maxx = *max_element(dis + 1, dis + 1 + n);
		int idx = a;
		for (int i = 1; i <= n; i++)
			if (dis[i] == maxx)
				idx = i;

		dfs(idx, -1, 0);
		int dia = *max_element(dis + 1, dis + 1 + n);
		if (dia <= da * 2)
		{
			cout << "Alice" << endl;
		}
		else cout << "Bob" << endl;
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int test = 1;
	cin >> test;
	for (int i = 1; i <= test; i++)
	{
		solve();
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值