【搜索】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 ...
随机推荐
- Centos系统安装 phpredis 扩展
Git地址:https://github.com/nicolasff/phpredis 一.安装: phpize ./configure make && make install 其中 ...
- 9.JAVA-抽象类定义
1.抽象类 抽象类,用来表示一个抽象概念. 是一种只能定义类型,而不能产生对象的类,所以定义了抽象类则必须有子类的出现. 抽象类的好处在于能够明确地定义子类需要覆写的方法 抽象类需要使用abstrac ...
- 网页尺寸scrollHeight/offsetHeight
scrollHeight和scrollWidth,获取网页内容高度和宽度. 一.针对IE.Opera: scrollHeight 是网页内容实际高度,可以小于 clientHeight. 二.针对NS ...
- ubuntu下php-fpm多实例运行配置
php-fpm服务一般情况下我们只会配置一个php-fpm了,如果我们碰到要实现多实例php-fpm服务要如何来配置呢,下面一起来看看吧. 这里是在LNMP环境的基础上配置多实例的过程.因为我在使用的 ...
- [整理]ADB命令行学习笔记
global driver# 元素定位driver.find_element_by_id("id") # id定位driver.find_element_by_name(" ...
- 常用的-->查找算法与排序算法
顺序查找 从列表第一个元素开始,顺序进行搜索,直到找到为止. 二分查找 从有序列表的候选区data[0:n]开始,通过对待查找的值与候选区中间值的比较,可以使候选区减少一半. li = [1, 2, ...
- fgetpos, fseek, fsetpos, ftell, rewind - 重定位某个流
总览 (SYNOPSIS) #include <stdio.h> int fseek(FILE *stream, long offset, int whence); long ftell( ...
- docker存储管理
Docker 镜像的元数据 repository元数据 repository在本地的持久化文件存放于/var/lib/docker/image/overlay2/repositories.json中 ...
- stay hungry stay foolish.
I am honored to be with you today at your commencement from one of the finest universities in the wo ...
- Chrome安装助手踩坑
[前言] 最近用之前的方法配置hosts,想浏览下载国外网站的数据和插件,突然发现几乎所有的方法都无效了...... 本文介绍下下载谷歌助手,通过助手访问国外网站 [主体] (1)搜索谷歌助手,点击下 ...