求助maxn开1e6所有点都mle
查看原帖
求助maxn开1e6所有点都mle
982518
sjwhsss楼主2024/7/17 18:48
#include <bits/stdc++.h>
using namespace std;
// const int maxn = 1e6+5;
const int maxn = 1e5+5;
struct tree
{
	int root , l , r , w;
}t[maxn];
int sum[maxn];
int Find(int x)
{
	if (x == -1 || x == 0) return 0;
	int a = Find(t[x].l);
	int b = Find(t[x].r);
	return sum[x] = a + b + 1;
}
struct qq
{
	int root;
	queue <int>q1 , q2;
}qx[maxn];
void Dfs(int x , queue <int>&q , bool flag)
{
	if (x == -1)return;
	Dfs(t[x].l , qx[x].q1 , 0);
	Dfs(t[x].r , qx[x].q2 , 1);
	//cout << qx[x].q1.size() << " ";
	q.push(t[x].w);
	queue <int>tmp;
	if (!flag)
	{
		tmp = qx[x].q1;
		while(tmp.size())
		{
			q.push(tmp.front());
			tmp.pop();
		}
		tmp = qx[x].q2;
		while(tmp.size())
		{
			q.push(tmp.front());
			tmp.pop();
		}
	}
	else
	{
		tmp = qx[x].q2;
		while(tmp.size())
		{
			q.push(tmp.front());
			tmp.pop();
		}
		tmp = qx[x].q1;
		while(tmp.size())
		{
			q.push(tmp.front());
			tmp.pop();
		}
	}
	return;
}
int main ()
{
	int n;
	cin >> n;
	bool flag = 1;
	t[0].w=0 , t[0].l=-1 , t[0].r=-1;
	for (int i = 1; i <= n; i++)
	{
		cin >> t[i].w;
		t[i].root=i;
	}
	if (flag == 1)
	for (int i = 1; i <= n; i++)
	{
		cin >> t[i].l >> t[i].r;
		if (t[i].l == -1)t[i].l=0;
		if (t[i].r == -1)t[i].r=0;
	}
	Find(1);
	queue <int>q;
	Dfs(1 , q , 0);
	int ans = 1;
	for (int i = 1; i <= n; i++)
	{
		if(qx[i].q1 == qx[i].q2) ans = max(ans , sum[i]);
	}
	cout << ans;
	return 0;
}

rt 开1e5第21-25个点re,1e6全部mle

2024/7/17 18:48
加载中...