【搜索】codeforces C. The Tag Game
http://codeforces.com/contest/813/problem/C
【题意】
给定一棵有n个结点的树,初始时Alice在根结点1,Bob在非根结点x;
Alice和Bob轮流走,每一步都有两种选择:走向相邻结点或静止不动,Bob先走;
当Alice和Bob相遇时游戏结束;
Alice的目标是游戏结束是的总步数最少,Bob的目标是游戏结束时总步数最少;
求最后的总步数是多少。
2 ≤ n ≤ 2·10^5, 2 ≤ x ≤ n
【思路】
Alice想要游戏尽早结束,所以Alice的每一步是靠近Bob; Bob不想游戏结束,所以他选择躲Alice,在尽量不遇到Alice的情况下选择一条最长的路径然后逃到这条路的叶节点不动。所以可以这样做:
bfs求出树上每个点k到1的距离dis[k],O(n)
bfs求出树上每个点k到x的距离dist[k],O(n)
如果dis[k]<dist[k],说明k是可行点,在所有可行点中找到最大的dist[k]。
还有一种方法更复杂一点:
找出x到1的路径上的可行点k,所谓可行点就是当Bob到达这点的时候Alice还没有到达,1->k的路径长度+以结点K为根的子树高度
dfs一次找出每个点的深度和高度,O(n)
dfs一次找出1->x路径上的结点,O(n)
【Accepted】
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=2e5+;
const int inf=0x3f3f3f3f;
int n,x;
struct node
{
int to;
int nxt;
}edge[maxn<<];
int tot;
int ans;
int head[maxn];
int du[maxn];
int dis[maxn];
int dist[maxn];
void Init()
{
memset(head,-,sizeof(head));
memset(dis,inf,sizeof(dis));
memset(dist,inf,sizeof(dist));
tot=;
ans=;
}
void add(int u,int v)
{
edge[tot].to=v;
edge[tot].nxt=head[u];
head[u]=tot++;
}
void bfs1(int u)
{
int vis[maxn];
memset(vis,,sizeof(vis));
dist[u]=;
vis[u]=;
queue<int> Q;
Q.push(u);
while(!Q.empty())
{
u=Q.front();
Q.pop();
for(int i=head[u];i!=-;i=edge[i].nxt)
{
int to=edge[i].to;
if(vis[to])
{
continue;
}
dist[to]=dist[u]+;
vis[to]=;
Q.push(to);
}
}
} void bfs2(int u)
{
int vis[maxn];
memset(vis,,sizeof(vis));
dis[u]=;
vis[u]=;
queue<int> Q;
Q.push(u);
while(!Q.empty())
{
u=Q.front();
Q.pop();
for(int i=head[u];i!=-;i=edge[i].nxt)
{
int to=edge[i].to;
if(vis[to])
{
continue;
}
dis[to]=dis[u]+;
vis[to]=;
Q.push(to);
}
}
} void Solve()
{
bfs1();
bfs2(x);
for(int i=;i<=n;i++)
{
// cout<<dis[i]<<" "<<dist[i]<<endl;
if(dis[i]<dist[i])
{
ans=max(ans,dist[i]);
}
}
cout<<ans*<<endl;
}
int main()
{
while(~scanf("%d%d",&n,&x))
{
Init();
int u,v;
for(int i=;i<n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
du[u]++;
du[v]++;
}
Solve();
}
return ;
}
BFS
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll; int n,x;
const int maxn=2e5+;
struct node
{
int to;
int nxt;
}edge[maxn<<];
int tot;
int head[maxn];
int du[maxn];
int dep[maxn];
int path[maxn];
int vis[maxn];
int d[maxn];
int col;
int ans;
void Init()
{
memset(path,,sizeof(path));
memset(du,,sizeof(du));
memset(head,-,sizeof(head));
memset(dep,,sizeof(dep));
memset(vis,,sizeof(vis));
memset(d,,sizeof(d));
tot=;
col=;
ans=;
}
void add(int u,int v)
{
edge[tot].to=v;
edge[tot].nxt=head[u];
head[u]=tot++;
} int dfs(int u,int pre)
{
for(int i=head[u];i!=-;i=edge[i].nxt)
{
int to=edge[i].to;
if(to==pre)
{
continue;
}
dep[to]=dep[u]+;
d[u]=max(d[u],dfs(to,u)+);
}
return d[u];
} void pdfs(int u,int pre,int cnt)
{
if(col!=)
{
return;
}
path[cnt]=u;
if(u==x)
{
col=cnt;
return;
}
for(int i=head[u];i!=-;i=edge[i].nxt)
{
int to=edge[i].to;
if(to==pre)
{
continue;
}
pdfs(to,u,cnt+);
}
}
void Solve()
{
dfs(,);
pdfs(,,);
vis[]=;
for(int i=col,k=;i>=,k<=col;i--,k++)
{
if(!vis[i])
{
ans=max(ans,d[path[i]]+dep[path[i]]);
}
vis[k]=;
}
cout<<ans*<<endl; }
int main()
{
while(~scanf("%d%d",&n,&x))
{
Init();
int u,v;
for(int i=;i<n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
du[u]++;
du[v]++;
}
Solve();
}
return ;
}
DFS
【搜索】codeforces C. The Tag Game的更多相关文章
- CodeForces - 813C The Tag Game (树的dfs遍历)
[传送门]http://codeforces.com/problemset/problem/813/C [题目大意]两个人玩游戏,一个人跑一个人追,轮流决策,可以走也可以不走.给你一棵树,想要从某个结 ...
- node搜索codeforces 3A - Shortest path of the king
发一下牢骚和主题无关: 搜索,最短路都可以 每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...
- Harbor 搜索镜像及查看 tag
在我们搭建完 Harbor 后: https://www.cnblogs.com/klvchen/p/9482153.html 如果想要通过 API 获取 Harbor 上面的镜像及 tag 可以使用 ...
- codeforces 813C The Tag Game 树+dfs追击问题
C. The Tag Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutpu ...
- CodeForces - 813C The Tag Game(拉格朗日乘数法,限制条件求最值)
[传送门]http://codeforces.com/problemset/problem/813/C [题意]给定整数a,b,c,s,求使得 xa yb zc值最大的实数 x,y,z , 其中x ...
- Codeforces 813C The Tag Game (BFS最短路)
<题目链接> 题目大意:A.B两人在一颗树上,A在根节点1上,B在节点x上,现在他们轮流走,每次只能走一步,或者不走.A以尽可能靠近B的方式行走,B以尽可能远离A的方式走,B先开始走.问你 ...
- Git tag 给当前分支打标签
原文已经找不到出处,重新整理格式,仅作个人收藏! 标签(Tag)可以针对某一时间点的版本做标记,常用于版本发布. 列出tag $ git tag # 在控制台打印出当前仓库的所有tag $ git t ...
- bs4--官文--搜索文档树
搜索文档树 Beautiful Soup定义了很多搜索方法,这里着重介绍2个: find() 和 find_all() .其它方法的参数和用法类似,请读者举一反三. 再以“爱丽丝”文档作为例子: ht ...
- DT二次开发之-资讯列表中调用 TAG 关键词
资讯列表加文章关键词:(列表或搜索主字段加上 ,tag) {if $t[tag]} <p class="key"> 关键词: {php $tag = str_repla ...
随机推荐
- Asp.net 字符(三)
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using ...
- 导Excel数据表
需要把EXcel转换格式:
- 通俗易懂的Nhibernate教程(2) ---- 配置之Nhibernate配置
在上一个教程中,我们讲了Nhibernate的基本使用!So,让我们回顾下Nhibernate使用基本的步骤吧 1.NHibernate配置 ----- 这一步我们告诉了Nhibernate:数据库 ...
- jQuery选择器之基本筛选选择器
<h2>基本筛选器</h2> <h3>:first/:last/:even/:odd</h3> <div class="left&quo ...
- Backbone.js之Todo源码浅析
相信每个接触了解过backbone的人都知道todo,网上的关于它的分析教程也都分析乱了.但是,知识只有自己学习领悟才是自己的,话不多说,正文开始. 在分析todo的源码之前,首先我们要知道todo具 ...
- 【HEVC帧间预测论文】P1.4 Motion Vectors Merging: Low Complexity Prediction Unit Decision
Motion Vectors Merging: Low Complexity Prediction Unit Decision Heuristic for the inter-Prediction o ...
- HYSBZ 1503 郁闷的出纳员 (Splay树)
题意: 作为一名出纳员,我的任务之一便是统计每位员工的工资.但是我们的老板反复无常,经常调整员工的工资.如果他心情好,就可能把每位员工的工资加上一个相同的量.反之,如果心情不好,就可能把他们的工资扣除 ...
- Web前端基础怎么学? JavaScript、html、css知识架构图
以前开发者只要掌握 HTML.CSS.JavaScript 三驾马车就能胜任一份前端的工作了.而现在除了普通的编码以外,还要考虑如何性能优化,如何跨端.跨平台实现功能,尤其是 AI.5G 技术的来临, ...
- MyBatis基本运行环境
MyBatis基本运行环境 1. 创建项目 2.拷贝jar加入到项目中build path jar包 3.创建数据库的表及数据添加 USE [mybatis] CREATE TABLE [dbo].[ ...
- Selenium3+python自动化008-操作浏览器基本方法
一.打开网站1.第一步:从selenium里面导入webdriver模块2.打开Firefox浏览器(Ie和Chrome对应下面的)3.打开百度网址二.页面刷新1.有时候页面操作后,数据可能没及时同步 ...