AcWing 2022.7.19-2

类LCA问题

采用LCA思想,因为满二叉树的父子之间的大小关系确定了,所以不需要建图,直接用 /2 模拟追溯祖先的过程即可。

#include <bits/stdc++.h>
using namespace std;

int a, b;

int main() {
	//cout << "pause" << endl;
	scanf("%d %d", &a, &b);
	while (a != b) {

		while (a > b)
			a /= 2;

		while (a < b)
			b /= 2;
	}

	printf("%d\n", a);

	return 0;
}

附LCA正解:
https://www.acwing.com/activity/content/code/content/154772/

posted @ 2022-07-19 15:28  superPG  阅读(20)  评论(0编辑  收藏  举报