111
查看原帖
111
754171
midelashen_official楼主2023/12/2 09:30
#include<bits/stdc++.h>
using namespace std;
struct node{
	string lt;
	string rt;
};
int n;
int longest=0,shortest=n+1;
map<string,node> mp;
void dfs(string s,int now)
{
//	cout<<s<<" "<<now<<endl;
	longest=max(longest,now);
	if(mp[s].lt=="0"||mp[s].rt=="0"){
		shortest=min(shortest,now);
	//	cout<<shortest<<endl;
}
	if(mp[s].lt!="0")
	dfs(mp[s].lt,now+1);
	if(mp[s].rt!="0")
	dfs(mp[s].rt,now+1);

	return ;
}
int main()
{
	
	cin>>n;
	shortest=n+1;
	for(int i=1;i<=n;i++)
	{
		string data,lt,rt;
		cin>>data>>lt>>rt;
		mp[data].lt=lt;
		mp[data].rt=rt;
		
	}
	dfs("root",1);
	cout<<longest-shortest;
	return 0;
}
2023/12/2 09:30
加载中...