@[sjwhsss](/user/982518) `queue` 的默认实现容器是 `deque`,空间常数 $200$。
这边建议换一下。
by Wilting @ 2024-07-27 14:56:36
@[sjwhsss](/user/982518) 换成 `list` 就好。
```cpp
#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, list<int>>q1 , q2;
}qx[maxn];
void Dfs(int x , queue <int, list<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, list<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, list<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;
}
```
by Wilting @ 2024-07-27 14:58:16
求关
by Wilting @ 2024-07-27 14:59:10
感谢解答,虽然最后五个点会MLE
by sjwhsss @ 2024-07-27 16:22:44