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 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 3
25
思路:求出出队序列,两两求树内最近距离;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=2e5+,M=1e6+,inf=1e9;
#define mem(s) memset(s,0,sizeof(s))
int n,m,head[N],t,vis[N],deep[N],fa[N][];
int a[N];
int flag[N];
struct ss {
int to,next;
}e[N*];
void add(int u,int v) {
e[t].next=head[u];e[t].to=v;head[u]=t++;
}
void init() {
t=;mem(head);mem(vis);mem(fa);mem(deep);mem(flag);
}
void dfs(int x) {
vis[x]=;
for (int i=; i<= ;i++) {
if(deep[x]<(<<i)) break;
fa[x][i] = fa[fa[x][i-]][i-];
}
for (int i=head[x];i;i=e[i].next) {
if(vis[e[i].to]) continue;
deep[e[i].to]=deep[x]+;
fa[e[i].to][]=x;
dfs(e[i].to);
}
}
int RMQ_LCA(int x,int y) {
if(deep[x]<deep[y]) swap(x,y);
int d=deep[x]-deep[y];
for (int i=; i<= ;i++)
if((<<i)&d) x=fa[x][i];
for (int i=; i>= ;i--) {
if(fa[x][i]!=fa[y][i]) {
x=fa[x][i];y=fa[y][i];
}
}
if(x==y) return x;
else return fa[x][];
}
int Dis_LCA(int x,int y) {
int LCA= RMQ_LCA(x,y);
return (deep[x]+deep[y]-*deep[LCA]);
}
struct is
{
int pos,step,pre;
};
int main()
{
int x,y,z,i,t;
while(~scanf("%d",&x))
{
queue<is>q;
init();
for(i=;i<=x;i++)
scanf("%d",&a[i]);
for(i=x;i>=;i--)
{
add(a[i],i);
add(i,a[i]);
}
dfs();
int maxdeep=;
int pre=;
is st;
st.pos=;
st.step=;
st.pre=;
q.push(st);
flag[]=;
ll ans=;
while(!q.empty())
{
is vv=q.front();
q.pop();
if(vv.pos!=)
{
ans+=Dis_LCA(vv.pos,pre);
}
pre=vv.pos;
maxdeep=vv.step;
int pos=vv.pos;
for(i=head[vv.pos];i;i=e[i].next)
{
if(flag[e[i].to])
continue;
is en;
en.pos=e[i].to;
en.step=vv.step+;
en.pre=vv.pos;
flag[e[i].to]=;
q.push(en);
}
}
printf("%lld\n",ans);
}
return ;
}
Aizu 2677 Breadth-First Search by Foxpower LCA+bfs的更多相关文章
- TTTTTTTTTTTTTTTT #7 div1 A Breadth-First Search by Foxpower 在线LCA(倍增),模拟
A - Breadth-First Search by Foxpower Time Limit:2000MS Memory Limit:131072KB 64bit IO Format ...
- 广度优先搜索(Breadth First Search, BFS)
广度优先搜索(Breadth First Search, BFS) BFS算法实现的一般思路为: // BFS void BFS(int s){ queue<int> q; // 定义一个 ...
- LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)
题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...
- 广度优先搜索(Breadth First Search)
Date:2019-07-03 14:29:02 走完一层的所有房间,再走下一层,用队列实现 算法实现 /*--------------------------模版------------------ ...
- BNUOJ 52303 Floyd-Warshall Lca+bfs最短路
题目链接: https://www.bnuoj.com/v3/problem_show.php?pid=52303 Floyd-Warshall Time Limit: 60000msMemory L ...
- 最小生成树Kruskal+LCA+bfs【bzoj4242】水壶
Description JOI 君所居住的 IOI 市以一年四季都十分炎热著称. IOI 市被分成 \(H\) 行,每行包含 \(W\) 块区域.每个区域都是建筑物.原野.墙壁之一. IOI 市有 \ ...
- Codevs1026 SEARCH(逃跑的拉尔夫 )(BFS)
SEARCH 时间限制: 1 Sec 内存限制: 128 MB提交: 11 解决: 4[提交][状态][讨论版] 题目描述 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警 ...
- hdu-2586 How far away ?(lca+bfs+dfs+线段树)
题目链接: How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 【WIP_S9】图论算法
创建: 2018/06/01 图的概念 有向边 有向图 无向边 无向图 点的次数: 点连接的边的数量 闭路: 起点和重点一样 连接图: 任意两点之间都可到达 无闭路有向图: 没有闭路的有向图 森林: ...
随机推荐
- Linux下修改Mysql的用(root的密码及修改root登录权限
修改的用户都以root为列. 一.知道原来的myql数据库的root密码: ①: 在终端命令行输入 mysqladmin -u root -p password "新密码" 回车 ...
- JavaScript实践-简单的贪吃蛇小游戏
实现逻辑: //获取Html中的格子(行,列) //建立数组存储所有格子(x,y) //建立数组用于存储蛇身(x,y) //生成随机坐标(x,y)的函数 //随机创建蛇身并存储到蛇身数组 //创建食物 ...
- orchestrator-Raft集群部署
本文简要说明下orchestrator的Raft集群部署,其实部署很简单主要是好好研究下配置文件的配置,这里我的样例配置文件暂时只适用于我们这块业务 如果您自己使用请根据情况自行修改. 主要通过配置文 ...
- Centos7 下谷歌日志库GLog配置
1 glog下载地址 https://code.google.com/archive/p/google-glog/downloads glog-0.3.3.tar.gz 需要FQ,直接打不开 2 解压 ...
- jq封装选项卡写法
jq普通选项卡写法: var tabTag=$('#tabon'); var tabon=tabTag.find('li');//菜单栏 var tabCon=$(".hidden" ...
- java中的System.copyof()与Array.copyof()区别
java中的System.copyof()与Array.copyof()区别 在复制数组时我们可以使用System.copyof(),也可以使用Array.copyof(),但是它们之间是有区别的.以 ...
- 转!!Tomcat网站上的core和deployer的区别
转自:https://www.cnblogs.com/guxia/p/6678184.html 8.5.13 Please see the README file for packaging info ...
- Django框架错误处理
错误处理 在一些网站开发中.经常会需要捕获一些错误,然后将这些错误返回比较优美的界面,或者是将这个错误的请求做一些日志保存.那么我们本节就来讲讲如何实现. 常用的错误码: 404:服务器没有指定的ur ...
- 系列文章(二):从WLAN的安全威胁,解析电信诈骗的技术症结——By Me
导读:互联网的无线接入已经成为大趋势,其中无线局域网(又称为WLAN,Wireless Local AreaNetwork)以其使用方便.组网灵活.可扩展性好.成本低等优点,成为互联网特别是移动互联网 ...
- 收藏:几种开源许可证的区别!——By 阮一峰制作
乌克兰程序员Paul Bagwell,画了一张分析图,下面是阮一峰制作的中文版,非常棒,绝对的好东西,收藏这张图供日后查看: