WA(不是TLE)100pts
查看原帖
WA(不是TLE)100pts
743373
Vitamin_B楼主2024/3/7 22:30

rt,楼上的hack是hack暴力跳的,结果却把我倍增卡WA了:

# include <bits/stdc++.h>

# define ffor(i,name) \
	for (auto i = name.begin (); i != name.end (); ++ i)

using namespace std;

typedef long long ll;

typedef pair <int, int> pii;

int n, q, f[100005][25], m, x, depth[100005], now, t;

int main () {

	ios::sync_with_stdio (0);

	cin.tie (0);

	cout.tie (0);

	cin >> n;

	for (int i = 1; i < n; ++ i)
		cin >> f[i][0], depth[i] = depth[f[i][0]] + 1;

	for (int i = 1; i < 23; ++ i)
		for (int j = 1; j < n; ++ j)
			f[j][i] = f[f[j][i - 1]][i - 1];

	cin >> q;

	while (q --) {

		cin >> m >> now;

		for (int i = 1; i < m; ++ i) {

			cin >> x;

			if (depth[x] < depth[now])
				swap (x, now);

			t = depth[x] - depth[now];
//			cout << t << ' ' << x << "->";
			for (int i = 0; i < 23; ++ i)
				if (t & 1 << i)
					x = f[x][i];
//			cout << x << ' ' << now << '\n';
			if (x == now)
				continue ;

			for (int i = 22; ~i; -- i)
				if (f[x][i] != f[now][i])
					x = f[x][i], now = f[now][i];

			now = f[now][0];

		}

		cout << now << '\n';

	}

	return 0;

}
2024/3/7 22:30
加载中...