#7 div1 A Breadth-First Search by Foxpower 在线LCA(倍增),模拟

A - Breadth-First Search by Foxpower

Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

Submit Status

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

6

Sample Input 2

4
1 1 3

Output for the Sample Input 2

4

Sample Input 3

11
1 1 3 3 2 4 1 3 2 9

Output for the Sample Input 25

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来求

 
时间: 2024-10-18 10:46:13

#7 div1 A Breadth-First Search by Foxpower 在线LCA(倍增),模拟的相关文章

图的遍历之广度优先搜索(Breadth First Search)

描述 广度优先搜索算法(Breadth First Search)与树的层序遍历(level-order traversal)类似,基本思想是思想是: 从图中某顶点v出发,访问v之后,并将其访问标志置为已被访问,即visited[i]=1: 依次访问v的各个未曾访问过的邻接点: 分别从这些邻接点出发依次访问它们的邻接点,并使得"先被访问的顶点的邻接点先于后被访问的顶点的邻接点被访问,直至图中所有已被访问的顶点的邻接点都被访问到: 如果此时图中尚有顶点未被访问,则需要另选一个未曾被访问过的顶点作为

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 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 form

BFS—— Breadth First Search 广度优先算法

Breadth First Search BFS家伙还是很有用的,特地从wiki扒了一个动态的图,来帮助感性的理解这个动态搜索的过程. 对于如下一个无权值的有向图,在进行广度优先搜索呢?这里我们的代码实现是,以节点3为入口 对于BFS的理论基础介绍,个人觉得还是看<DSAA>比较好.这里不做介绍,注重分析该搜索算法的实现. 如果对于图这种数据结构不熟悉,这个BFS一般是搞不定的... 下面分别是无向图的邻接表实现和邻接矩阵实现 http://blog.csdn.net/cinmyheart/a

Breadth First Search VS Depth First Search (Algorithms)

First lets recall the concept for BFS and DFS. I will use below Binary Tree as an example. Before that, lets go through some of the concepts of Trees and Binary Trees quickly. Concept of Binary Trees A binary tree is made of nodes, where each node co

(总结)宽度优先搜索(Breadth First Search)

ACM入门最经典的开局一般都是宽搜. 宽度优先搜索(以下均简称bfs)一般用于树和图的搜索,在ACM中属于比较基础的技巧,因此需要非常熟练的掌握. 那么从最基础的bfs开始讲起.在一个迷宫中,有一个起点和一个终点(出口),和一些障碍物(无法通过). 比如下图

Chapter four Breadth First Search(宽度优先搜索)

BFS最主要的数据结构是Queue,由LinkedList实现. 1.binary-tree-level-order-traversal(二叉树的层次遍历) 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) BFS解法[基本模板]: public class Solution { /** * @param root: The root of binary tree. * @return: Level order a list of lists of integer */ public

图论算法(5) --- 双向广搜求最短路(Bidirectional Breadth First Search)

我们知道,在图论算法中,求最短路是最基本的问题.在求最短路的问题中,应用双向广度优先搜索算法,又是一个较为高效而又简单的算法.所谓双向广度优先搜索,其实根本的核心还是BFS,只不过它是从起点和终点两头同时搜索,大大提高了搜索效率,又节省了搜索空间.广搜大家知道当然是用队列来实现了,在这里,要注意的问题就是,我们必须按层搜索,正向队列处理一层,接着去处理反向队列的一层,按层交替进行,而不是按节点交替进行,这点需要注意,其他的也就很简单了,代码中附有注释,如有问题请留言. package simil

无向图的深度优先与广度优先搜索代码实现

图采用了邻接表的形式储存. 带不带权都无所谓的 深度优先搜索 Depth First Search 道理和树的先序遍历差不多,把将要访问的点入栈,然后从栈里取点进行访问. 由于这只是类中的一个成员函数,有些被调用的函数的具体代码将会在文章最后补上 ,但是函数功能看注释就好了 1 //深度优先 2 void GraphAdjacencyListWeight::DFSAdvanced(int StartVertex) { 3 int *visited = new int[VertexNumber];

无权最短路 - 宽度优先搜索

2017-09-13 21:54:52 writer:pprp 图论全部都忘记了,重新学一下吧,之前学的实在是太烂了 测试数据如下: 7 12//顶点个数, 路径个数3 11 41 22 42 54 34 54 64 73 65 76 73//起始点 代码如下: /* @theme:无权最短路径问题 @complexity:O(|E| + |V|) @writer:pprp @begin:21:10 @end:21:53 @error: @declare: breadth first searc