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. mvc不登录的情况下无法跳转至其他页面--解决方法之一

    在每个控制器里,加以下方法 /// <summary> /// 在调用视图之前拦截非法用户 /// </summary> /// <param name="fi ...

  2. AJPFX简述Java中this关键字的使用

    Java中this关键字的使用主要有两处: 1.构造方法 this指的是调用构造方法进行初始化的对象. //有参构造public Human(String name, int age) { this( ...

  3. 使用python编写的简单远程管理软件

    因为用户可以选择是否同意被控制,所以并不算是木马. 使用python3.7,spyder,在windows 10 开发. client为控制端,server为被控端. 参考 mygithub http ...

  4. python基础一 day8 函数

    函数的定义与函数的调用是两个部分 定义函数的时候里面的代码不执行,等到调用函数的时候再执行 只写return和不写return返回None 函数遇到return,这个函数就被结束            ...

  5. css--字体和文本样式

    文字样式属性 字体:font-family 文字大小:font-size 文字颜色:font-color 文字粗细:font-weight 文字样式:font-style font-family字体属 ...

  6. JavaScript 非常重要的几个概念

    JavaScript是一门比较复杂的语言.如果你是一名JavaScript开发人员,不管处于什么样的水平,都有必要了解JavaScript的基本概念.小编最近的工作涉及到JavaScript,于是本文 ...

  7. 2018 CCPC 女生赛 hdoj6288 缺失的数据范围

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6288 Summarize:1.二分查找答案: 2.自带log函数精度不够,需自己写: 3.注意二分递归 ...

  8. luogu P1407 稳定婚姻-tarjan

    题目背景 原<工资>重题请做2397 题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有 ...

  9. uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 无效的列类型: 1111; nested exception is java.sql.SQLException: 无效的列类型: 1111

    WHERE 的条件取值时添加上jdbcType=NUMBER这样的配置 参考[1]:https://blog.csdn.net/fyhjuyol/article/details/45483167

  10. 关于U盘安装ubuntu-18.04安装时候出现的grub-efi-amd64-signed的问题。

    关于这个问题,首先我们要查看一下我们电脑的主板设置中U盘启动的类型是什么,是UEFI还是legacy? 对于如果是UEFI那么给ubuntu分区的时候不用设置/boot分区,设置efi系统分区:如果是 ...