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 ...
随机推荐
- 如何在 OS X Yosemite 中安装 Java
如果你的 Mac 纯净的安装了 OS X Yosemite 的话,其中是不会包含 Java 的,如果你的 Mac 需要安装 Java 环境的话,可以通过下面介绍的两种方法来实现.通过手动安装最新版 J ...
- poj 1364 King(差分约束)
题目:http://poj.org/problem?id=1364 #include <iostream> #include <cstdio> #include <cst ...
- poj 1442 Black Box(堆 优先队列)
题目:http://poj.org/problem?id=1442 题意:n,m,分别是a数组,u数组的个数,u[i]w为几,就加到a几,然后输出第i 小的 刚开始用了一个小顶堆,超时,后来看了看别人 ...
- poj1062
经典的图论建模题: 先拿开的等级问题不看: 每个物品本身的价格就是有一个自定义源点到这个点距离: 有了A物品B物品优惠为W就代表由B到A的有向路权值为W: 最后的最小花费就是源点的点1的最短路径(酋长 ...
- redis作为mysql的缓存服务器(读写分离,通过mysql触发器实现数据同步)
一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...
- HDU 5289 Assignment
题意:求一段长度为n的序列里有多少个子区间内的最大值减最小值小于k. 解法:RMQ+尺取法或单调队列.RMQ可以用st或者线段树,尺取法以前貌似YY出来过……只是不知道是这个东西…… 设两个标记l和r ...
- 关于word2010中完美解决数学公式(正斜体)输入的解决方案
测试环境 win10(64位) office2010(32位)——64位的没有测试,估计应该也可以. 需要软件(包)(请按照下面顺序安装) ①VC运行库(自行百度下载即可) ②北大方正word公式数学 ...
- Linux Add a Swap File
http://www.cyberciti.biz/faq/linux-add-a-swap-file-howto/ Procedure To Add a Swap File Under Linux Y ...
- c#操作XML文件的通用方法
转载地址:http://www.studyofnet.com/news/36.html 原址没找到 sing System; using System.Data; using System.Confi ...
- 【译】 AWK教程指南 附录C-AWK的内建函数
C.1 字串函数 index( 原字串, 查找的子字串 ) 若原字串中含有欲寻找的子字串,则返回该子字串在原字串中第一次出现的位置,若未曾出现该子字串则返回0. 例如: $ awk 'BEGIN{ p ...