84分 WA 4个点求助
查看原帖
84分 WA 4个点求助
502658
Ray662楼主2023/1/17 16:24
#include <bits/stdc++.h>
#define ull unsigned long long
#define _for(i, a, b)  for (int i = (a); i <= (b); i ++ )
#define _all(i, a, b)  for (int i = (a); i >= (b); i -- )
using namespace std;
const int N = 1e6 + 5, P = 999999751;
int n, ans, v[N], lc[N], rc[N], sz[N];
ull p[N], h[N], g[N], b[N], t[N];
void dfs(int x) {
	if (lc[x] > 0)  dfs(lc[x]);
	if (rc[x] > 0)  dfs(rc[x]);
	sz[x] = sz[lc[x]] + sz[rc[x]] + 1;
	if (sz[x] == 1)  h[x] = g[x] = b[x] = t[x] = v[x];
	else  h[x] = h[lc[x]] * p[sz[rc[x]] + 1] + v[x] * p[sz[rc[x]]] + h[rc[x]],
		  g[x] = g[lc[x]] * p[sz[rc[x]] + 1] + g[rc[x]] * p[1] + v[x],
		  b[x] = b[rc[x]] * p[sz[lc[x]] + 1] + v[x] * p[sz[lc[x]]] + b[lc[x]],
		  t[x] = t[rc[x]] * p[sz[lc[x]] + 1] + t[lc[x]] * p[1] + v[x];
}
int main() {
	ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
	h[0] = b[0] = 137;
	g[0] = t[0] = 199;
	cin >> n;
	_for (i, 1, n)  cin >> v[i];
	_for (i, 1, n) {
		cin >> lc[i] >> rc[i];
		if (lc[i] < 0)  lc[i] = 0;
		if (rc[i] < 0)  rc[i] = 0;
	}
	p[0] = 1;
	_for (i, 1, n + 1)  p[i] = p[i - 1] * P;
	dfs(1);
	_for (i, 1, n)  if (sz[lc[i]] == sz[rc[i]] && h[lc[i]] == b[rc[i]] && g[lc[i]] == t[rc[i]])  ans = max(ans, sz[i]);
	cout << ans << endl;
	return 0;
}
2023/1/17 16:24
加载中...