CodeForces-813C The Tag Game

题目链接:
https://vjudge.net/problem/CodeForces-813C

The Tag Game
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
4 3
1 2
2 3
2 4

4

5 2
1 2
2 3
3 4
2 5

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

题目的大意就是Alice和Bob(后面用A和B代替)在玩一个游戏,A的初始位置在1,B的初始位置在x。然后A和B同时走,直到A和B同时在一个地方游戏结束,在这个过程中A想用尽可能少的步数追上B,但是B又想让A追上他的步数尽可能多(就是A想尽可能快的追上B,但是B又不想让A那么快的追上),大致就是这个意思。B是可以选择移动或者待在原地不动的,最后是要我们求A和B总共移动的步数。

思路:
这道题其实就是求最短路径的最大值,因为A和B是同时走的,并且就算B待在原地不动的话最后总共还是会+1(这里好像用时间好理解一点,即最后让我们求A追上B的时间,移动一步算1s,B如果原地不动时间也会+1)。所有说求最后总共的步数的话,就相当于是求A一共走过的步数的两倍。因为A和B肯定在同一棵树上,A又想尽可能快的追上B,所以A会直接向B走,不会走其他的支路,B是不想让A那么快追上,所以如果B还能往更深的地方走的话,他肯定会走下去,直到到底了以后,他就会在原地不动。这时候问题就转化成了求A能到达的最远的节点,同时也要判断B能否到达,而且要保证B是能比A先到的,这样就可以求出最大值了。

我们最开始是用最短路来求,最后过了,后来在网上翻博客,发现都是用搜索写的,不过思路都是一样的。

那就把两种代码都贴上:

//比赛时用最短路写的
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 400100
int first[maxn],Next[maxn],dis[maxn],dis1[maxn],u[maxn],v[maxn];
int main()
{
    int n,m,maxx=0;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        dis[i]=inf;
        first[i]=-1;
        dis1[i]=inf;
    }
    for(int i=1; i<2*n-1; i+=2)
    {
        cin>>u[i]>>v[i];
        u[i+1]=v[i];
        v[i+1]=u[i];
    }
    for(int i=1; i<2*n-1; i++)
    {
        Next[i]=first[u[i]];
        first[u[i]]=i;
    }
    queue<int>q;
    q.push(1);
    dis[1]=0;
    while(!q.empty())
    {
        int h=first[q.front()];
        q.pop();
        while(h!=-1)
        {
            if(dis[v[h]]==inf)
            {
                dis[v[h]]=dis[u[h]]+1;
                q.push(v[h]);
            }
            h=Next[h];
        }
    }
    while(!q.empty())
        q.pop();
    q.push(m);
    dis1[m]=0;
    while(!q.empty())
    {
        int h=first[q.front()];
        q.pop();
        while(h!=-1)
        {
            if(dis1[v[h]]==inf)
            {
                dis1[v[h]]=dis1[u[h]]+1;
                q.push(v[h]);
            }
            h=Next[h];
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(dis[i]>maxx&&(dis1[i]!=0||i==m)&&dis[i]>dis1[i])
            maxx=dis[i];
    }
    cout<<maxx*2<<endl;
    return 0;
}


//后来看到用dfs写的

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=2e6;
vector<int>a[N];
int dis[2][N];
int ans;
void dfs(int x,int y,int z,int k)
{

    dis[z][x]=k;
    if(z==1&&dis[0][x]==k)
    {
        ans=max(k,ans);
        return;
    }
    if(z==1&&k<dis[0][x])
    {
        ans=max(ans,dis[0][x]*2);
    }
    for(int i=0; i<a[x].size(); i++)
    {
        int s=a[x][i];
        if(s==y) continue;
        dfs(s,x,z,k+1);
    }
}

int main()
{
    int n,x;
    cin>>n>>x;
    ans=0;
    for(int i=0; i<n-1; i++)
    {
        int x,y;
        cin>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfs(1,-1,0,0);
    dfs(x,-1,1,0);
    cout<<ans<<endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44137005/article/details/89040434
tag