2015北京网络赛 F Couple Trees 暴力倍增
Couple Trees
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://hihocoder.com/problemset/problem/1232
Description
"Couple Trees" are two trees, a husband tree and a wife tree. They are named because they look like a couple leaning on each other. They share a same root, and their branches are intertwined. In China, many lovers go to the couple trees. Under the trees, lovers wish to be accompanied by a lifetime.
Ada and her boyfriend Asa came to the couple trees as well. They were very interested in the trees. They were all ACMers, so after careful observation, they found out that these two trees could be considered as two "trees" in graph theory. These two trees shared N vertices which were labeled 1 to N, and they all had exactly N vertices. Vertices 1 was the root of both trees.
Ada and Asa wanted to know more about the trees' rough bark, so each of them put one thumb at a vertices. Then they moved their thumbs towards the root. Ada moved along the wife tree, and Asa moved along the husband tree. Of course, they could moved at different speed.
At that moment, a thought suddenly came to Ada's mind: their thumbs may meet before the root. Which one was the earliest possible meeting vertex? And how many vertices would Ada and Asa encounter on the way to the meeting vertex?
Input
The input consists of no more than 8 test cases.
For each test case:
The first line contains two integers, N and M, indicating the number of vertices and the number of queries.(1≤N,M≤100,000)
The next line contains N−1 integers. It describes the structure of wife tree in this way: If the ith integer is k, it means that the vertex labeled k is the father vertex of the vertex labeled (i+1) . It's guaranteed that a vertex X's father vertex can't have a larger label than X does.
The next line describes the husband tree in the same way.
Then next M lines describe the queries. Each line contains two integers Xi and Yi. Let Ki be the earliest possible meeting vertex of the ith query (K0 is defined as 0). In the ith query, Ada's thumb was put at the vertex labeled (Xi+Ki−1) mod N + 1 and Asa's thumb was put at the vertex labeled (Yi+Ki−1) mod N + 1.(1≤Xi,Yi≤N) at the beginning.
Output
For each test case:
Output the answer for each query in a single line. The answer contains three integers: the earliest possible meeting vertex, the number of the vertices Ada will encounter and the number of the vertices Asa will encounter (including the starting vertex and the ending vertex). In particular, if they put their thumb at the same vertex at first, the earliest possible meeting vertex should be the starting vertex.
Sample Input
5 1
1 2 3 3
1 1 3 2
4 3
5 3
1 1 2 2
1 2 2 1
5 3
5 4
3 5
5 3
1 1 2 2
1 2 3 1
1 4
1 1
3 4
Sample Output
3 2 2
1 1 3
1 2 1
2 2 1
1 2 2
3 1 1
2 1 2
HINT
题意
给你两棵树,都同时往上爬,问你这两个人都能够经过的点中,最大的点是什么,并且都各走了多少步
题解:
倍增就好了,直接暴力往上爬
然而并没有什么算法难度= =
当然这个做法是水过去的,并不是正解
@)1%KBO0HM418$J94$1R.jpg)
代码:
//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100006
#define mod 1000000007
#define eps 1e-9
#define PI acos(-1)
const double EP = 1E- ;
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//*************************************************************************************
int fa[maxn][],fb[maxn][],deepa[maxn],deepb[maxn];
int n,m;
int x,y;
int stepx,stepy,lastans;
void solve(int x,int y)
{
while(x!=y)
{
if(x<y)
{
for(int i=;i>=;i--)
if(fb[y][i]>x)y=fb[y][i],stepy+=<<i;
y=fb[y][];stepy++;
}
else
{
for(int i=;i>=;i--)
if(fa[x][i]>y)x=fa[x][i],stepx+=<<i;
x=fa[x][];stepx++;
}
}
lastans = x;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=;i++)
fa[][i]=fb[][i]=;
deepa[]=deepb[]=;
for(int i=;i<=n;i++)
{
fa[i][]=read();
deepa[i]=deepa[fa[i][]]+;
for(int j=;j<=;j++)
fa[i][j]=fa[fa[i][j-]][j-];
}
for(int i=;i<=n;i++)
{
fb[i][]=read();
deepb[i]=deepb[fb[i][]]+;
for(int j=;j<=;j++)
fb[i][j]=fb[fb[i][j-]][j-];
}
lastans = ;
while(m--)
{
x=read(),y=read();
x = (x+lastans)%n+;
y = (y+lastans)%n+;
stepx=stepy=;
solve(x,y);
printf("%d %d %d\n",lastans,stepx,stepy);
}
}
}
2015北京网络赛 F Couple Trees 暴力倍增的更多相关文章
- acm 2015北京网络赛 F Couple Trees 树链剖分+主席树
Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...
- acm 2015北京网络赛 F Couple Trees 主席树+树链剖分
提交 题意:给了两棵树,他们的跟都是1,然后询问,u,v 表 示在第一棵树上在u点往根节点走 , 第二棵树在v点往根节点走,然后求他们能到达的最早的那个共同的点 解: 我们将第一棵树进行书链剖,然后第 ...
- Hiho 1232 北京网络赛 F Couple Trees
给两颗标号从1...n的树,保证标号小的点一定在上面.每次询问A树上的x点,和B树上的y点同时向上走,最近的相遇点和x,y到这个点的距离. 比赛的时候想用倍增LCA做,但写渣了....后来看到题解是主 ...
- 2015北京网络赛 Couple Trees 倍增算法
2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道. 解法来自 q ...
- 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT
2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
- (中等) Hiho 1232 Couple Trees(15年北京网络赛F题),主席树+树链剖分。
"Couple Trees" are two trees, a husband tree and a wife tree. They are named because they ...
- 2015北京网络赛 A题 The Cats' Feeding Spots 暴力
The Cats' Feeding Spots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acm ...
- 2015北京网络赛A题The Cats' Feeding Spots
题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. #include ...
随机推荐
- C#调用Geocoding API进行地理编码与逆编码
使用C#调用Geocoding API来将地址转为经纬度,或者将经纬度转变为具体的地址. Geocoding API的详细介绍参见:http://developer.baidu.com/map/web ...
- MySQL索引的查看创建和删除
1.索引作用 在索引列上,除了上面提到的有序查找之外,数据库利用各种各样的快速定位技术,能够大大提高查询效率.特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万倍. 例如,有 ...
- Android 自动换行流式布局的RadioGroup
效果图 用法 使用FlowRadioGroup代替RadioGroup 代码 import android.content.Context; import android.util.Attribute ...
- 【转】eclipse新建项目,报错“Error: workspace\appcompat_v7\res\values-v21\styles_base.xml No resource found that matches the given name”
原文网址:http://www.cnblogs.com/mbp-study/p/5268478.html 新建项目报错,不知道为什么,以前从未出现过的错误,把sdk更新之后,出现莫名错误,自己也是一知 ...
- PostgreSql与sqlserver对比杂记
PostgreSql与MSSqlServer区别 增删查改没有语法一样. 排序Group Having 聚集函数使用一样 联结查询 ON 子句是最常见的连接条件的类型:它接收一个和 WHERE 子句相 ...
- 如何卸载eclipse中的ADT
卸载ADT的方法,方法如下: 1.选择 Help > Install New Software: 2.在"Details" 面板中, 点击"What is alre ...
- GDI+ 学习记录(26): 显示图像 - Image
//显示图像 var g: TGPGraphics; img: TGPImage; begin g := TGPGraphics.Create(Self.Canvas.Handle); ...
- js基础第一天
js作用:网页特效(电梯导航).交互.表单特效.就是可以用来控制结构和样式. 常用的三个输出语句都属于js的内置对象,提供我们直接使用的功能就是内置对象功能. web三标准:结构.样式.行为.而js主 ...
- 派遣例程与IRP结构
提到派遣例程,必须理解IRP(I/O Request Package),即"输入/输出请求包"这个重要数据结构的概念.Ring3通过DeviceIoControl等函数向驱动发出I ...
- 【推荐】JavaScript的那些书
又好久没写东西了,写上一篇的时候还以为接下来的工作会轻松一些,结果未从我所愿呐,又是一阵忙碌.而这段时间穿插着做了很多12年淘宝校园招聘的前端面试,很多同学都有问到,学校里没有前端的课程,那如何学习J ...