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 图的概念 有向边 有向图 无向边 无向图 点的次数: 点连接的边的数量 闭路: 起点和重点一样 连接图: 任意两点之间都可到达 无闭路有向图: 没有闭路的有向图 森林: ...
随机推荐
- ETCD数据空间压缩清理
场景:做etcd数据镜像的时候出现如下错误 Error: etcdserver: mvcc: database space exceeded 通过查找官方文档https://coreos.com/e ...
- delphi xe-system.json
Delphi XE10有一个对JSON处理的单元,在你需要使用JSON的单元里面引入"System.json",随后你就可以用Delphi自己的json处理类了. 普通解析 实例1 ...
- 巨蟒python全栈开发-第14天 内置函数2 递归 二分查找
一.今日内容总览 1.内置函数补充 repr() 显示出字符串的官方表示形式 chr() arscii码中的字,转换成位置 ord() arscii码中的位置,转换成字2.递归 自己调用自己 两个口: ...
- JD-GUI
JD-GUI http://jd.benow.ca/ JD-GUI可到官網直接下載.官網除了JD-GUI之外,另提供了Eclipse(JD-Eclipse)和IntelliJ(JD-IntelliJ) ...
- LinkedList基本用法
https://blog.csdn.net/i_lovefish/article/details/8042883
- 转!java产生不重复随机数
private static void testC(int sz) { long startTime = System.currentTimeMillis(); //开始测试时间 Random rd ...
- Python面向对象中的“私有化”
Python面向对象中的“私有化” Python并不直接支持私有方式,而要靠程序员自己把握在外部进行特性修改的时机. 为了让方法或者特性变为私有(从外部无法访问),只要在它的名字前面加上双下划线即可. ...
- mysql模糊查询实践总结
%代表任意多个字符 _代表一个字符 在 MySQL中,SQL的模式缺省是忽略大小写的 正则模式使用REGEXP和NOT REGEXP操作符. “.”匹配任何单个的字符.一个字符类 “[...]”匹配在 ...
- 七牛云 如何配置域名的 CNAME
CNAME 简介 CNAME 即指别名记录,也被称为规范名字.这种记录允你将多个名字映射到同一台计算机. 当需要将域名指向另一个域名,再由另一个域名提供 ip地址,就需要添加 CNAME 记录. 为什 ...
- CG group
Linux CGroup全称Linux Control Group, 是Linux内核的一个功能,用来限制,控制与分离一个进程组群的资源(如CPU.内存.磁盘输入输出等).这个项目最早是由Google ...