88的雷达回归!!!!
yang_zhiyu
2024-01-24 20:55:59
#include <iostream>
#include <vector>
using namespace std;
struct Node
{
int lc,rc;
};
const int N=100050;
Node tree[N];
vector<int> kuan[N];
int maxdep;
void preorder(int cur,int dep)
{
if (cur == 0) return ;
dep++;
maxdep=max(maxdep,dep);
kuan[dep].push_back(cur);
preorder(tree[cur].lc,dep);
preorder(tree[cur].rc,dep);
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>tree[i].lc>>tree[i].rc;
}
preorder(1,0);
int maxkuan=-114514;
for(int i=1;i<=maxdep;i++)
{
maxkuan=max(int(kuan[i].size()),maxkuan);
}
cout<<maxkuan<<endl;
return 0;
}
共 1 条回复
#include <bits/stdc++.h> #define endl "\n" using namespace std; int n; const int N=1e5+1; struct sd{ int l,r; }tree[N]; int in[N],pre[N],idx[N]; int dfs(int iL,int pL,int len) { if (len<= 0) return 0; int root=pre[pL+len-1]; int pos=idx[root]; int len1=pos-iL,len2=len-len1-1; tree[root].l=dfs(iL,pL,len1); tree[root].r=dfs(pos+1,pL+len1,len2); return root; } void preorder(int cur) { if (cur == 0) return ; cout << cur << ' '; preorder(tree[cur].l); preorder(tree[cur].r); } int main(){ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freeopen(".in","r",stdin); //freeopen(".out","w",stdout); cin>>n; for (int i = 1; i <= n; i++){ cin >>in[i]; idx[in[i]]=i; } for (int i = 1; i <= n; i++) cin >>pre[i]; preorder(dfs(1,1,n)); return 0; }