codeforces813C The Tag Game
来源:互联网 发布:淘客采集上传淘宝店铺 编辑:程序博客网 时间:2024/06/05 23:42
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.
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.
Print the total number of moves Alice and Bob will make.
4 31 22 32 4
4
5 21 22 33 42 5
6
题解:
找到bob比alice先到的点中深度最大的一个 答案为(该点深度-1)*2
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn=200000+10;vector<int>A[maxn];int a[maxn];int b[maxn];int n,p;int ans=0;inline void dfs(int x,int f,int d,int *a){a[x]=d;for(int i=0;i<A[x].size();i++){int u=A[x][i];if(u==f)continue;dfs(u,x,d+1,a);}}int main(){//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);scanf("%d %d",&n,&p);int x,y;for(int i=1;i<n;i++){scanf("%d %d",&x,&y);A[x].push_back(y);A[y].push_back(x);}dfs(1,0,1,a);dfs(p,0,1,b);for(int i=1;i<=n;i++){if(a[i]>b[i])ans=max(ans,(a[i]-1)*2);}printf("%d\n",ans);return 0;}
- codeforces813C The Tag Game
- The Tag Game
- C. The Tag Game
- The Tag Game CodeForces
- The Tag Game(CodeForces
- The Tag Game
- The Tag Game
- The Tag Game(bfs)
- C. The Tag Game【dfs】
- 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
- Educational Codeforces Round 22 C. The Tag Game dfs
- SQL Antipattern 树之反模式(评论回复 数据库表设计)
- 工作笔记20170622
- Raft一致性算法
- MFC 在某些特定条件下改变控件内文本的颜色的方法
- spring 相关注解
- codeforces813C The Tag Game
- Kotlin学习笔记(二)---从零学习Kotlin
- post json数据到springmvc中并获取http请求的内容
- NET中WebBrowser控件内部页面的JS代码与外部C#代码的相互调用
- 设计模式 适配器模式 以手机充电器为例
- 硬币排成线
- 微信小程序如何推广
- java+JavaScript 实现 非对称加密 rsa
- Android研究院之应用开发线程池的经典使用