2015 Beijing online game F Couple Trees violence doubled

Source: Internet
Author: User

Couple Trees

Time Limit:1 Sec

Memory limit:256 MB

Topic Connection http://hihocoder.com/problemset/problem/1232
Description

"Couple Trees" is the Trees, a husband tree and a wife tree. They is named because they look like a couple leaning on each other. They share a same root, and their branches is intertwined. In China, many lovers go to the couple trees. Under the trees, lovers wish to being 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 the trees could be considered as both "trees" In graph theory. These 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 on the trees ' rough bark, so each of the 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 this moment, a thought suddenly came to ADA's Mind:their thumbs may meet before the root. Which one is the earliest possible meeting vertex? And how many vertices would Ada and Asa encounter on the "to the" meeting vertex?

Input

The input consists of no more than 8 test cases.

For each test case:

The first line contains the 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 an integer is k, it means so the vertex labeled K is the FA ther vertex of the vertex labeled (i+1). It's guaranteed that a vertex x ' s father Vertex can ' t has a larger label than X does.

The next line describes the husband tree in the same.

Then next M lines describe the queries. Each line contains integers Xi and Yi. Let Ki was the earliest possible meeting vertex of the ith in query (K0 is defined as 0). In the ith query, Ada's thumb is put at the vertex labeled (xi+ki−1) mod N + 1 and Asa's thumb is put at the vertex labe LED (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 would encounter an d The number of the vertices Asa would encounter (including the starting vertex and the ending vertex). In particular, if they put their thumb on the same vertex at first, the earliest possible meeting vertex should be the STA rting Vertex.

Sample Input
5 11 2 3 31 1 3 24 35 31 1 2 21 2 2 15 35 43 55 31 1 2 21 2 3 11 41 13 4

Sample Output

3 2 21 1 31 2 12 2 11 2 23 1 12 1 2
HINT

Test instructions

Give you two trees, all at the same time climb up, ask you all two people can pass the point, the biggest point is what, and each walk how many steps

Exercises

Just multiply it, direct violence, climb up.

However, there is no algorithmic difficulty = =

Of course, this is water past, not the positive solution

Code:

//Qscqesze#pragmaComment (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>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 100006#defineMoD 1000000007#defineEPS 1e-9#definePI ACOs (-1)Const DoubleEP = 1e-Ten ;intNum;//const int INF=0X7FFFFFFF;Constll inf=999999999; inline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//*************************************************************************************intfa[maxn][ -],fb[maxn][ -],DEEPA[MAXN],DEEPB[MAXN];intn,m;intx, y;intStepx,stepy,lastans;voidSolveintXinty) {     while(x!=y) {if(x<y) { for(intI= the; i>=0; i--)                if(fb[y][i]>x) y=fb[y][i],stepy+=1<<i; Y=fb[y][0];stepy++; }        Else        {             for(intI= the; i>=0; i--)                if(fa[x][i]>y) x=fa[x][i],stepx+=1<<i; X=fa[x][0];stepx++; }} Lastans=x;}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {         for(intI=0; i<= the; i++) fa[1][i]=fb[1][i]=1; deepa[1]=deepb[1]=1;  for(intI=2; i<=n;i++) {fa[i][0]=read (); Deepa[i]=deepa[fa[i][0]]+1;  for(intj=1; j<= the; j + +) Fa[i][j]=fa[fa[i][j-1]][j-1]; }         for(intI=2; i<=n;i++) {fb[i][0]=read (); Deepb[i]=deepb[fb[i][0]]+1;  for(intj=1; j<= the; j + +) Fb[i][j]=fb[fb[i][j-1]][j-1]; } Lastans=0;  while(m--) {x=read (), y=read (); X= (X+lastans)%n+1; Y= (Y+lastans)%n+1; STEPX=stepy=1;            Solve (x, y); printf ("%d%d%d\n", lastans,stepx,stepy); }    }}

2015 Beijing online race F Couple Trees violence multiplier

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.