LCA倍增板子

auto LCA = [&](int a, int b)->pair
	{
		ll ans = 0;
		if (dep[a] < dep[b])swap(a, b);

		while (dep[a] > dep[b])
		{
			int dis = (int)log2(dep[a] - dep[b]);
			a = my_father[dis][a];
			ans += pow(2, dis);
		}

		for (int i = log2(dep[a]); i >= 0; i--)
		{
			if (my_father[i][a] != my_father[i][b])	//向上结果不同才跳
			{
				a = my_father[i][a];
				b = my_father[i][b];
				ans += pow(2, i) * 2;
			}
		}
		if (a != b)
		{
			a = b = my_father[0][a];
			ans += 2;
		}
		return { a ,ans };
	};

你可能感兴趣的:(板子,算法,LCA)