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. Java迭代器的用法【转】

    迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的I ...

  2. Spring中@Value的使用

  3. 11gR2集群件任务角色分离(Job Role Separation)简介

       从11gR2版本开始,Oracle推荐使用不同的操作系统用户安装GI和数据库软件,例如:使用grid用户安装GI,使用Oracle用户安装数据库软件.当然,用户还是可以使用Oracle用户安装G ...

  4. Pygame - Python游戏编程入门

    >>> import pygame>>> print(pygame.ver)1.9.2a0 如果没有报错,应该是安装好了~ 如果报错找不到模块,很可能是安装版本的问 ...

  5. 什么是WebSocket (经常听别人讲感觉很高大上其实不然)

    WebSocket 协议在2008年诞生,2011年成为国际标准.现在所有浏览器都已经支持了.WebSocket 的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器发送信息,是真 ...

  6. G7或变G6+1?特朗普七国峰会箱友军开炮

    今日导读 G7 峰会刚召开完毕,德国总理默克尔发的一张照片就迅速火遍全球.照片中她身体前倾,像是在质问特朗普,而后者则双手交叉胸前,我自岿然不动,这张火药味十足的照片不禁让人脑补当时剑拔弩张的气氛.到 ...

  7. GCC、g++编译器和gcc编译器的区别

    GCC:(GNU Compiler Collection,GNU编译器套件),是由 GNU 开发的编程语言编译器.它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分. gcc:GNU的C语 ...

  8. 【搜索】P1468 派对灯 Party Lamps

    P1468 派对灯 Party Lamps 我们来分析一下对灯的操作 1.对所有灯的,这时吧所有灯看成一个整体 2.奇偶数的操作,这时可以把每两个数看成一个循环节 3.对3X+ 1的操作,这时可以把每 ...

  9. 报错:org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement

    org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n ...

  10. Python List extend()方法

    Python List extend()方法  Python 列表 描述 extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表). 语法 extend()方法语法 ...