codeforces 813C The Tag Game
来源:互联网 发布:sketch软件 编辑:程序博客网 时间:2024/06/05 15:54
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
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 201015using 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;}
阅读全文
0 0
- codeforces 813C The Tag Game
- codeforces 813C. The Tag Game
- Codeforces 813 C The Tag Game
- Codeforces 813C The Tag Game 题解
- codeforces 813C The Tag Game dfs
- codeforces 813C The Tag Game
- codeforces 813C The Tag Game
- Codeforces 813C The Tag Game
- Codeforces 813C The Tag Game【思维+Dfs】
- codeforces 813C The Tag Game dfs && 路径长度
- The Tag Game CodeForces
- The Tag Game(CodeForces
- C. The Tag Game
- Educational Codeforces Round 22 C. The Tag Game dfs
- Educational Codeforces Round 22-C. The Tag Game-搜索,贪心
- Educational Codeforces Round 22 C. The Tag Game(思维)
- Educational Codeforces Round 22 C. The Tag Game 搜索
- Codeforces Round 22 C. The Tag Game ( 搜索
- 【JavaSE学习笔记】逻辑运算符
- basler、halcon在vs下的配置方法
- 7
- 傻瓜都能看懂的高并发量服务器架构
- 配置Linux Kernel时make menuconfig执行流程分析
- codeforces 813C The Tag Game
- oracle——expdp与impdp备份使用方法
- bootsrap学习笔记(一)<div>块如何水平居中
- 【持续更新】【cmd】cmd命令大全(2017年6月9日09:37:53)
- Retrofit+RxJava的联合使用
- Eclipse安装Svn无法连接问题
- Linux 下 fork, vfork,和clone的区别
- ECharts图表中修改每个category的颜色
- js中使用splice删除数组中某一项或几项的几种方法