Educational Codeforces Round 22 C. The Tag Game 搜索
来源:互联网 发布:香港科技大学gpa算法 编辑:程序博客网 时间:2024/06/07 06:43
传送门
一颗1为树根的无向树,小a在1,小b在m,小a总想最快靠近小b,小b总想最晚被接近,他们两个各走一步。问你最大的回合数(每回合一次)
思路:每个人都走最优,是不会走回头路的(B来回走可以看作在一点不动),那么我们可以先求下A,B分别到所有节点的最短时间,对于每个节点如果B走的时间小于A走的时间那B就可以走到这个。所以要在先求了从根节点到其他节点的max距离之后,再逆序遍历其x位置走的距离。 因为B走的位置必须在A之前走。 就是说可能相遇,得排除相遇这种情况。
#include<bits/stdc++.h>using namespace std;const int N=2e5+1;int fa[N],dep[N],len[N];vector<int>adj[N];int n,x;int dfs(int cur,int father,int lens)//dfs遍历所有点{ dep[cur]=lens; len[cur]=0; fa[cur]=father; for(int i=0;i<adj[cur].size();i++) { int ne=adj[cur][i]; if(ne==father)continue; len[cur]=max(len[cur],dfs(ne,cur,lens+1)); } return len[cur]+1;}int main(){//freopen("t.txt","r",stdin); scanf("%d%d",&n,&x); int a,b; for(int i=0;i<n;i++) { scanf("%d%d",&a,&b); adj[a].push_back(b); adj[b].push_back(a); } fa[1]=0; dfs(1,0,0); int ll=0; int ans=dep[x]+len[x]; while((x=fa[x])!=0)//逆序遍历 { ll++; if(dep[x]<=ll)break; ans=max(ans,dep[x]+len[x]); } printf("%d\n",ans*2);}
阅读全文
0 0
- Educational Codeforces Round 22-C. The Tag Game-搜索,贪心
- Educational Codeforces Round 22 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(思维 搜索)
- Codeforces Round 22 C. The Tag Game ( 搜索
- Educational Codeforces Round 22 C The Tag Game(树的深度)
- Educational Codeforces Round 5 C. The Labyrinth
- Educational Codeforces Round 5 C. The Labyrinth
- Educational Codeforces Round 5 C. The Labyrinth
- 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 Round #413(Div. 1 + Div. 2)
- Linux学习之路 -- 常用命令和各种总结(不断补充完善)
- 格式日期函数
- 读书笔记-大型网站系统与Java中间件7
- Dijksta算法原理
- Educational Codeforces Round 22 C. The Tag Game 搜索
- vs2015 读取\写入 中文txt,和_T()方法,L""字符串,还有TCHAR类型和wchar_t类型
- 机器学习笔记-聚类分析之K-means算法案例及其Python实现
- C#WPF简述
- 计算理论与图灵机
- hdu 2037 今年暑假不AC(贪心)
- 手机分享网站...设置标题等信息
- 深度强化学习控制移动机器人
- Linux内核 哈希表的结构与实现