codeforces 813C The Tag Game

本文探讨了一种名为“标签游戏”的策略游戏,在该游戏中,玩家A从根节点出发,玩家B从非根节点开始,目标是A尽可能快地追上B。文章详细解析了最优策略,包括如何通过寻找特定路径上的最深节点来最大化移动步数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. The Tag Game
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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·105, 2 ≤ 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

题意:一颗无向有根树,A在根节点1,B在非根节点x。B先走然后再A走(如此下去),每一步要么向相连点移动要么不动。A要以尽可能减少移动步数的走法,B以尽可能增加步数的走法。当A与B相遇时游戏结束。求最大的移动步数。

分析:B肯定在A的子树里 所以A走法就是不走多余的节点直接去接近B,而B可以找到他能走到的离根节点1距离最大的点这样就能使最后步数最大。

            所以求出刚开始A·B的路径上的节点,找到此路径上深度最大的节点且B能走到的节点Y,ans =( 1到Y+Y的能到的最深深度)*2.

            暂时没有想到更简单的想法和代码的写法,直接贴上代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#define siz 201015
using namespace std;
int n,x,cot;
int d[siz],path[siz],vis[siz],dep[siz];
vector<int>G[siz];
int dfs(int u,int fa)//求深度
{
    int len = G[u].size(),Max = 0;
    for(int i=0; i<len; i++)
    {
        int v = G[u][i];
        if(v == fa) continue;
        dep[v] = dep[u] + 1;
        Max = max(dfs(v,u),Max);
    }
    return d[u] = Max+1;
}
void pdfs(int u,int fa,int t)//求出路径
{
    if(cot) return;
    path[t] = u;
    if(u == x)
    {
        cot = t;
        return;
    }
    int len = G[u].size();
    for(int i=0; i<len; i++)
    {
        int v = G[u][i];
        if(v == fa) continue;
        pdfs(v,u,t+1);
    }
}
void solve()
{
    int ans = 0;
    memset(d,0,sizeof(d));
    memset(vis,0,sizeof(vis));
    cot = 0,dep[1] = 0;
    dfs(1,-1);
    pdfs(1,-1,1);
    for(int i=cot,j=1; i; i--,j++)//B先走 然后再A走,vis[i] 表示A所在的位置。
    {
        vis[j] = 1;
        if(!vis[i]) ans = max(ans,d[path[i]]+dep[path[i]]-1);
    }
    cout<<ans*2<<endl;
}
int main()
{
    int u,v;
    while(~scanf("%d %d",&n,&x))
    {
        for(int i=1; i<=n; i++) G[i].clear();
        for(int i=1; i<n; i++)
        {
            scanf("%d %d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        solve();
    }
    return 0;
}



关于Codeforces上的问题'Trail',目前提供的参考资料中并未直接提及该问题的具体解法或讨论[^1]。然而,在处理类似平台上的编程挑战时,通常会遵循特定的方法论来解决问题。 对于未具体描述的问题'Trail',假设这是一个涉及路径遍历或是图结构中的轨迹计算等问题,一般解决方案可能涉及到深度优先搜索(DFS)、广度优先搜索(BFS)或者是动态规划等技术。这些方法能够有效地探索所有可能性并找到最优解。 考虑到Codeforces平台上许多问题的特点,解决这类题目往往还需要注意边界条件以及输入数据范围的影响。编写代码前应仔细阅读题目说明,确保理解所有的约束条件和特殊案例。 下面是一个简单的Python实现例子,用于展示如何通过深度优先搜索算法在一个假定的网格环境中寻找从起点到终点的有效路径: ```python def dfs(grid, start, end): rows, cols = len(grid), len(grid[0]) visited = set() def explore(r, c): if (r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == '#' or (r,c) in visited): return False if (r, c) == end: return True visited.add((r, c)) directions = [(0, 1), (1, 0), (-1, 0), (0, -1)] for dr, dc in directions: next_r, next_c = r + dr, c + dc if explore(next_r, next_c): return True return False return explore(*start) # Example usage with a simple maze represented as a list of strings. maze = [ '..#.##', '#...#.', '#####.' ] print(dfs(maze, (0, 0), (2, 5))) # Output should be True based on this example layout. ``` 此段代码展示了利用递归方式执行深度优先搜索的过程,适用于某些类型的‘Trail’类问题。当然实际应用中还需根据具体的题目要求调整逻辑细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值