TTTTTTTTTTTTTTTT #7 div1 A Breadth-First Search by Foxpower 在线LCA(倍增),模拟
Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu
Description
A - Breadth-First Search by Foxpower
Problem Statement
Fox Ciel went to JAG Kingdom by bicycle, but she forgot a place where she parked her bicycle. So she needs to search it from a bicycle-parking area before returning home.
The parking area is formed as a unweighted rooted tree TT with nn vertices, numbered 11 through nn. Each vertex has a space for parking one or more bicycles. Ciel thought that she parked her bicycle near the vertex 11, so she decided to search it from there by the breadth-first search. That is, she searches it at the vertices in the increasing order of their distances from the vertex 11. If multiple vertices have the same distance, she gives priority to the vertices in the order of searching at their parents. If multiple vertices have the same parent, she searches at the vertex with minimum number at first.
Unlike a computer, she can't go to a next vertex by random access. Thus, if she goes to the vertex jj after the vertex ii, she needs to walk the distance between the vertices ii and jj. BFS by fox power perhaps takes a long time, so she asks you to calculate the total moving distance in the worst case starting from the vertex 11.
Input
The input is formatted as follows.
nn
p2p2 p3p3 p4p4 ⋯⋯ pnpn
The first line contains an integer nn (1≤n≤1051≤n≤105), which is the number of vertices on the unweighted rooted tree TT. The second line contains n−1n−1 integers pipi (1≤pi<i1≤pi<i), which are the parent of the vertex ii. The vertex 11 is a root node, so p1p1 does not exist.
Output
Print the total moving distance in the worst case in one line.
Sample Input 1
4
1 1 2
Output for the Sample Input 1
Sample Input 2
4
1 1 3
Output for the Sample Input 2
Sample Input 3
11
1 1 3 3 2 4 1 3 2 9
Output for the Sample Input
25 题意:一有一棵树,直接相连的节点之间的距离均为1,一个人站在根节点,从深度从低到高搜索,从每次只能把同一深度的搜索完了才能向下一层搜索,问至少需要经过多少距离才能把所有节点都访问一遍
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=100000+10;
int c[maxn][22],deep[maxn],vis[maxn];
vector<vector<int> > G(maxn); void dfs(int u,int par)
{
c[u][0]=par;
for(int i=1;i<=20;i++) c[u][i]=c[c[u][i-1]][i-1];
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(v==par) continue;
deep[v]=deep[u]+1;
dfs(v,u);
}
} int lca(int u,int v)
{
if(deep[u]<deep[v]) swap(u,v);
for(int i=20;i>=0;i--)
if(deep[c[u][i]]>=deep[v])
u=c[u][i];
if(u==v) return u;
for(int i=20;i>=0;i--)
if(c[u][i]^c[v][i])
{
u=c[u][i];
v=c[v][i];
}
return c[u][0];
} int dis(int u,int v)
{
return deep[u]+deep[v]-2*deep[lca(u,v)];
} int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++) G[i].clear();
deep[1]=0;
for(int i=2;i<=n;i++)
{
int x;scanf("%d",&x);
G[x].push_back(i);
}
dfs(1,1);
int pre=1;ll ans=0;
queue<int> q;
q.push(1);
while(q.size())
{
int u=q.front();q.pop();
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
ans+=dis(pre,v);
pre=v;
q.push(v);
}
}
printf("%lld\n",ans);
}
return 0;
}
分析:LCA倍增法的第一题,需要设置一个pre代表先前处于哪个位置,然后对于当前需要访问的节点u,求pre和u之间的距离就好,通过lca来求
TTTTTTTTTTTTTTTT #7 div1 A Breadth-First Search by Foxpower 在线LCA(倍增),模拟的更多相关文章
- 广度优先搜索(Breadth First Search, BFS)
广度优先搜索(Breadth First Search, BFS) BFS算法实现的一般思路为: // BFS void BFS(int s){ queue<int> q; // 定义一个 ...
- Aizu 2677 Breadth-First Search by Foxpower LCA+bfs
A - Breadth-First Search by Foxpower Problem Statement Fox Ciel went to JAG Kingdom by bicycle, but ...
- 广度优先搜索(Breadth First Search)
Date:2019-07-03 14:29:02 走完一层的所有房间,再走下一层,用队列实现 算法实现 /*--------------------------模版------------------ ...
- [Algorithm] Breadth First JavaScript Search Algorithm for Graphs
Breadth first search is a graph search algorithm that starts at one node and visits neighboring node ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript
Depth first search is a graph search algorithm that starts at one node and uses recursion to travel ...
- C#算法知识点记录
针对算法的知识点进行记录 简易桶排序 首先看一个简易桶排序,有一串数字,进行从大到小排列.数字间隔不大,使用一维数组来当作桶,进行插入排序. static void Main(string[] arg ...
- 译:Boost Property Maps
传送门:Boost Graph Library 快速入门 原文:Boost Property Map 图的抽象数学性质与它们被用来解决具体问题之间的主要联系就是被附加在图的顶点和边上的属性(prope ...
- 分支界定法 branch-and-bound 分析与实现)(转载)
1. 介绍分支界定法之前需要了解一下广度优先搜索breadth-First-search(BFS) 1.从图中某个顶点V0出发,并访问此顶点:以层为顺序,一层一层往下遍历 2.从V0出发,访问V0的各 ...
随机推荐
- python 基础(十九)--re正则表达式模块
正则表达式模式 模式 描述 ^ 匹配字符串的开头 $ 匹配字符串的末尾. . 匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符. [...] 用来表示一组字符 ...
- JavaScript的几种循环方式
JavaScript提供了许多通过LOOPS迭代的方法.本教程解释了现代JAVASCRIPT中各种各样的循环可能性 目录: for forEach do...while while for...in ...
- T100——不绑定数据表字段,做查询条件(待改进)
此例子使用的方法有待改善,不是很好的方法. 范例:aimm200: 作用:查询时默认不显示无效料件:新增参数控制查询是否显示无效料件.(只作用查询,不影响新增.修改等)
- BufferedImage类、Image类、Graphics类
BufferedImage Image是一个抽象类,BufferedImage是其实现类,是一个带缓冲区图像类,主要作用是将一幅图片加载到内存中(BufferedImage生成的图片在内存里有一个图像 ...
- whistle学习(二)之启动、停止、重启、更新whistle等命令
新版本的whistle支持三种等价命令whistle,w2,wproxy 启动whistle w2 start 启动时指定端口 w2 start -p (// 不设置端口默认使用8899) 默认端口为 ...
- Spark 源码和应用开发环境的构建
引言 Spark 现在无疑是大数据领域最热门的技术之一,读者很容易搜索到介绍如何应用 Spark 技术的文章,但是作为开发人员,在了解了应用的概念之后,更习惯的是打开开发环境,开发一些应用来更深入的学 ...
- 微信小程序富文本
<div class="weui-panel__bd pad-all fs13 " > <rich-text nodes="{{detail.conte ...
- kbmMW 5.09.00是个必须升级的版本!
这几天遇到的几个问题,从5.08.10升级到5.09.00,自然解决了! 所以建议大家都升级到这个版本. 例如我遇到的问题: 1.在线程中使用ClientQuery注意的问题 2.Invalid pr ...
- php三种排序算法
1. <?php /** * 快速排序不费空间也节省时间 */ $arr=array(5,1,8,2,6,4,9,3,7); // $arr=array(1,2,3,4,5,6,7,8,9); ...
- 常见排序&查询算法Java代码实现
1. 排序算法代码实现 /** * ascending sort * 外层循环边界条件:总共需要冒泡的轮数--每一轮都将最大或最小的数冒泡到最后 * 内层循环边界条件:冒泡数字移动的边界--最终数字需 ...