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的更多相关文章

  1. CodeForces - 813C The Tag Game (树的dfs遍历)

    [传送门]http://codeforces.com/problemset/problem/813/C [题目大意]两个人玩游戏,一个人跑一个人追,轮流决策,可以走也可以不走.给你一棵树,想要从某个结 ...

  2. node搜索codeforces 3A - Shortest path of the king

    发一下牢骚和主题无关: 搜索,最短路都可以     每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...

  3. Harbor 搜索镜像及查看 tag

    在我们搭建完 Harbor 后: https://www.cnblogs.com/klvchen/p/9482153.html 如果想要通过 API 获取 Harbor 上面的镜像及 tag 可以使用 ...

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

    C. The Tag Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutpu ...

  5. CodeForces - 813C The Tag Game(拉格朗日乘数法,限制条件求最值)

    [传送门]http://codeforces.com/problemset/problem/813/C [题意]给定整数a,b,c,s,求使得  xa yb zc值最大的实数 x,y,z , 其中x ...

  6. Codeforces 813C The Tag Game (BFS最短路)

    <题目链接> 题目大意:A.B两人在一颗树上,A在根节点1上,B在节点x上,现在他们轮流走,每次只能走一步,或者不走.A以尽可能靠近B的方式行走,B以尽可能远离A的方式走,B先开始走.问你 ...

  7. Git tag 给当前分支打标签

    原文已经找不到出处,重新整理格式,仅作个人收藏! 标签(Tag)可以针对某一时间点的版本做标记,常用于版本发布. 列出tag $ git tag # 在控制台打印出当前仓库的所有tag $ git t ...

  8. bs4--官文--搜索文档树

    搜索文档树 Beautiful Soup定义了很多搜索方法,这里着重介绍2个: find() 和 find_all() .其它方法的参数和用法类似,请读者举一反三. 再以“爱丽丝”文档作为例子: ht ...

  9. DT二次开发之-资讯列表中调用 TAG 关键词

    资讯列表加文章关键词:(列表或搜索主字段加上 ,tag) {if $t[tag]} <p class="key"> 关键词: {php $tag = str_repla ...

随机推荐

  1. ubuntu下安装memcache及memcached

    memcache 和 memcached 有什么区别呢? memcache最早是在2004年2月开发的,而memcached最早是在2009年1月开发的.所以memcache的历史比memcached ...

  2. How `new’ operator works ?

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=15 February 15, 2013 How `new’ operator ...

  3. SQLServer · 最佳实践 · SQL Server 2012 使用OFFSET分页遇到的问题

    1. 背景 最近有一个客户遇到一个奇怪的问题,以前使用ROW_NUMBER来分页结果是正确的,但是替换为SQL SERVER 2012的OFFSET...FETCH NEXT来分页出现了问题,因此,这 ...

  4. whatis命令

    whatis——于查询一个命令执行什么功能 示例1: # whatis ls 显示ls命令的功能,和执行man命令时NAME信息差不多

  5. docker 新手入门 (阿里镜像仓库的使用)

    创建镜像仓库后的步骤是:   https://help.aliyun.com/document_detail/60743.html?spm=a2c4g.11186623.6.546.79be52f3y ...

  6. VsCode使用之HTML 中 CSS Class 智能提示

    HTML 中 CSS Class 智能提示 安装插件:HTML CSS Support 设置中添加以下代码: "editor.parameterHints": true, &quo ...

  7. CSS中列表项list样式

    CSS列表属性 属性 描述 list-style-属性 用于把所有用于列表的属性设置于一个声明中. list-style-image 将图象设置为列表项标志. list-style-position ...

  8. Web前端技术体系大全搜索

    一.前端技术框架 1.Vue.js 官网:https://cn.vuejs.org/ Vue CLI:https://cli.vuejs.org/ 菜鸟教程:http://www.runoob.com ...

  9. Maven实战读书笔记(二):Maven坐标与仓库

    2.1 Maven坐标 Maven坐标是Maven用来表示一个组件依赖的标示. Maven通过下面几个元素定义坐标:groupId.artifactId.version.packaging.class ...

  10. js 将页面保存为图片

    <!DOCTYPE html><html><head><title>保存为images</title><meta charset=&q ...