2015 Beijing Network race Couple Trees multiplication algorithm

Source: Internet
Author: User

2015 Beijing online race Couple Trees

Test instructions: Two trees, finding the nearest public ancestor of two nodes on different trees

Train of thought: The team that I saw in the game is not many, did not think carefully. Today, the problem only found that there is a multiplication algorithm, I actually do not know.

The solution comes from Qscqesze, this actually before if understand multiplication words or is not very difficult, but this question data also is not very to force, limit data theoretically can not pass.

Other solutions are tree-chain split? is not very clear. So the water is over ...

1#include <iostream>2#include <cstdio>3#include <fstream>4#include <algorithm>5#include <cmath>6#include <deque>7#include <vector>8#include <queue>9#include <string>Ten#include <cstring> One#include <map> A#include <stack> -#include <Set> - #defineLL Long Long the #defineEPS 1e-8 - #defineINF 0x3f3f3f3f - #defineMAXN 100005 - using namespacestd; +  - intf1[maxn][ -], f2[maxn][ -]; + intDEEP1[MAXN], DEEP2[MAXN]; A intStep1, Step2, ans; at voidWorkintXinty) { -Step1 = Step2 =1; -      while(X! =y) { -         if(X <y) { -             //x < y means y is not X ' s ancestor -              for(inti = the; I >=0; i--){ in                 if(F2[y][i] >x) { -y =F2[y][i]; toStep2 + =1<<i; +                      Break; -                 } the             } *y = f2[y][0]; $step2++;Panax Notoginseng         } -         Else{ the              for(inti = the; I >=0; i--){ +                 if(F1[x][i] >y) { Ax =F1[x][i]; theStep1 + =1<<i; +                      Break; -                 } $             } $x = f1[x][0]; -step1++; -         } the     } -Ans =x;Wuyi } the intMain () - { Wu #ifndef Online_judge -Freopen ("In.txt","R", stdin); About     //freopen ("OUT.txt", "w", stdout); $ #endif //Open_file -     intN, M; -      while(~SCANF ("%d%d", &n, &m)) { -         intx, y; Adeep1[1] = deep2[1] =1; +          for(inti =0; I <= the; i++){ thef1[1][i] = f2[1][i] =1; -         } $          for(inti =2; I <= N; i++){ thescanf"%d", &x); thef1[i][0] =x; theDeep1[i] = Deep1[x] +1; the              for(intj =1; J <= the; J + +){ -F1[I][J] = f1[f1[i][j-1]][j-1]; in             } the         } the          for(inti =2; I <= N; i++){ Aboutscanf"%d", &x); thef2[i][0] =x; theDeep2[i] = Deep2[x] +1; the              for(intj =1; J <= the; J + +){ +F2[I][J] = f2[f2[i][j-1]][j-1]; -             } the         }BayiAns =0; the          for(inti =1; I <= m; i++){ thescanf"%d%d", &x, &y); -x = (x + ans)% n +1; -y = (y + ans)% n +1; the Work (x, y); theprintf"%d%d%d\n", ans, step1, step2); the         } the     } -}

2015 Beijing Network race Couple Trees multiplication algorithm

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.