[ACM] sdut 2882 Full Binary Tree (满二叉树的公共祖先)

Full Binary Tree

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

In computer science, a binary tree is a tree data structure in which each node has at most two children. Consider an infinite full binary tree (each node has two children except the leaf nodes) defined as follows. For a
node labelled v its left child will be labelled 2?*?v and its right child will be labelled 2?*?v?+?1. The root is labelled as 1.

You are given n queries of the form i,?j. For each query, you have to print the length of the shortest path between node labelled i and node labelled j.

输入

First line contains n(1?≤?n?≤?10^5), the number of queries. Each query consists of two space separated integers i and j(1?≤?i,?j?≤?10^9) in one line.

输出

For each query, print the required answer in one line.

示例输入

5
1 2
2 3
4 3
1024 2048
3214567 9998877

示例输出

1
2
3
1
44

提示

来源

2014年山东省第五届ACM大学生程序设计竞赛

解题思路:

题意为一棵满二叉树,父节点编号为i,左孩子节点为i*2,右孩子节点编号为i*2+1,根节点编号为1,给定两个节点编号a,b,问a,b之间的最短路径是多少。

也就是求a,b的最近公共祖先。满二叉树每一层节点的编号x满足   2^p<=x<2^(p+1),所以先求出编号较小的数在哪一层,然后大的数跳到那一层,然后两个节点一起跳就可以了。

代码:

#include <iostream>
using namespace std;
int f[10000];//f[i]保存的是2的i次方
int len;

void pre()
{
    f[0]=1;
    for(int i=1;;i++)
    {
        f[i]=f[i-1]*2;
        if(f[i]>1e9)
        {
            len=i-1;
            break;
        }
    }
}

int a,b;
int t;
int main()
{
    pre();
    cin>>t;
    while(t--)
    {
        cin>>a>>b;
        if(a>b)
            swap(a,b);

        int step=0;
        int pos;//找到较小的数a在哪一层上,用pos保存
        for(int i=0;i<len;i++)
        {
            if(a>=f[i]&&a<f[i+1])
            {
                pos=i;
                break;
            }
        }
        while(1)//较大的数跳到与较小的数同一层上去
        {
            if(b>=f[pos]&&b<f[pos+1])
                break;
            b/=2;
            step++;
        }

        while(a!=b)//两个节点一起跳
        {
            a/=2;
            b/=2;
            step+=2;
        }
        cout<<step<<endl;
    }
    return 0;
}
时间: 2024-08-25 06:34:45

[ACM] sdut 2882 Full Binary Tree (满二叉树的公共祖先)的相关文章

[LintCode] Invert Binary Tree 翻转二叉树

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example Given 4 points: (1,2), (3,6), (0,0), (1,3). The maximum number is 3. LeeCode上的原题,可参见我之前的博客Invert Binary Tree 翻转二叉树. 解法一: // Recursion class So

【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题目大意 给定

【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

[111-Minimum Depth of Binary Tree(二叉树的最小深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题目大意 给定

[leetcode]236. Lowest Common Ancestor of a Binary Tree二叉树最近公共祖先

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q

遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R分别表示遍历左子树.访问根节点.遍历右子树 可能的情况6种 排列A3 2 LDR LRD DLR DRL RLD RDL 若限定先左后右 LDR LRD  中根序遍历  后根序遍历 DLR  先根序遍历 先/中/后 序遍历 原文地址:https://www.cnblogs.com/yuanjiangw/p

Leetcode 257 Binary Tree Paths 二叉树 DFS

找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 p

lintcode 中等题:binary tree serialization 二叉树的序列化和反序列化

题目 二叉树的序列化和反序列化 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构. 样例 给出一个测试数据样例, 二叉树{3,9,20,#,#,15,7},表示如下的树结构: 3 / 9 20 / 15 7 我们的数据是进行BFS遍历得到的.当你测试结果wrong answer时,你可以作为输

[LeetCode] Invert Binary Tree 翻转二叉树

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a w

654. Maximum Binary Tree 最大二叉树

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in the array. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number