• codeforces 813C The Tag Game 树+dfs追击问题


    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

    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

    题意:一颗无向有根树,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.

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

    [cpp] view plain copy
     
      1. #include <iostream>  
      2. #include<stdio.h>  
      3. #include<string.h>  
      4. #include<algorithm>  
      5. #include<vector>  
      6. #define siz 201015  
      7. using namespace std;  
      8. int n,x,cot;  
      9. int d[siz],path[siz],vis[siz],dep[siz];  
      10. vector<int>G[siz];  
      11. int dfs(int u,int fa)//求深度  
      12. {  
      13.     int len = G[u].size(),Max = 0;  
      14.     for(int i=0; i<len; i++)  
      15.     {  
      16.         int v = G[u][i];  
      17.         if(v == fa) continue;  
      18.         dep[v] = dep[u] + 1;  
      19.         Max = max(dfs(v,u),Max);  
      20.     }  
      21.     return d[u] = Max+1;  
      22. }  
      23. void pdfs(int u,int fa,int t)//求出路径  
      24. {  
      25.     if(cot) return;  
      26.     path[t] = u;  
      27.     if(u == x)  
      28.     {  
      29.         cot = t;  
      30.         return;  
      31.     }  
      32.     int len = G[u].size();  
      33.     for(int i=0; i<len; i++)  
      34.     {  
      35.         int v = G[u][i];  
      36.         if(v == fa) continue;  
      37.         pdfs(v,u,t+1);  
      38.     }  
      39. }  
      40. void solve()  
      41. {  
      42.     int ans = 0;  
      43.     memset(d,0,sizeof(d));  
      44.     memset(vis,0,sizeof(vis));  
      45.     cot = 0,dep[1] = 0;  
      46.     dfs(1,-1);  
      47.     pdfs(1,-1,1);  
      48.     for(int i=cot,j=1; i; i--,j++)//B先走 然后再A走,vis[i] 表示A所在的位置。  
      49.     {  
      50.         vis[j] = 1;  
      51.         if(!vis[i]) ans = max(ans,d[path[i]]+dep[path[i]]-1);  
      52.     }  
      53.     cout<<ans*2<<endl;  
      54. }  
      55. int main()  
      56. {  
      57.     int u,v;  
      58.     while(~scanf("%d %d",&n,&x))  
      59.     {  
      60.         for(int i=1; i<=n; i++) G[i].clear();  
      61.         for(int i=1; i<n; i++)  
      62.         {  
      63.             scanf("%d %d",&u,&v);  
      64.             G[u].push_back(v);  
      65.             G[v].push_back(u);  
      66.         }  
      67.         solve();  
      68.     }  
      69.     return 0;  
      70. }  
  • 相关阅读:
    PreferenceScreen 偏好显示类 的使用
    Android实战技巧:如何在ScrollView中嵌套ListView
    android onActivityResult不执行问题
    java zip解压中文乱码问题
    RandomAccessFile读取大文件时效率很低,现进行改进---BufferedRandomAccessFile
    HTTP RANGE(多线程下载相关)
    Android实现ListView异步加载图片
    自己调用webservice方法总结(带请求头SoapHeader)
    Android将程序崩溃信息保存本地文件
    Android的intent之间复杂参数的传递
  • 原文地址:https://www.cnblogs.com/yzm10/p/7247829.html
Copyright © 2020-2023  润新知