POJ_214_Boatherds_TLE

本文介绍了一个使用C++实现的二叉树构建方法及其遍历算法,该算法可以计算从根节点到任意叶节点的所有路径上的权重总和,并支持用户查询特定权重路径的存在性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
class Edge
{
public:
	int vertex;
	int weight;
	Edge *child;
	Edge *sibling;
	Edge()
	{
		vertex = weight = 0;
		child = sibling = NULL;
	}
};
class BiTree
{
public:
	int vertex;
	vector<vector<pair<int,int> > >adjList;
	set<int>Set;
	Edge *root;
	set<int>traversal(Edge *root)
	{
		if (!root)
		{
			set<int>vec;
			return vec;
		}
		if (!root->child&&!root->sibling)
		{
			set<int>vec;
			vec.insert(root->weight);
			Set.insert(root->weight);
			return vec;
		}
		set<int>set_child = traversal(root->child);
		set<int>set_sibling = traversal(root->sibling);
		set<int>set_root;
		set_root.insert(root->weight);
		Set.insert(root->weight);
		for (set<int>::iterator iter_child = set_child.begin(); iter_child != set_child.end(); iter_child++)
		{
			if (set_sibling.empty())
			{
				break;
			}
			for (set<int>::iterator iter_sibling = set_sibling.begin(); iter_sibling != set_sibling.end(); iter_sibling++)
			{
				Set.insert(*iter_child + *iter_sibling + root->weight);
			}
		}
		for (set<int>::iterator iter_child = set_child.begin(); iter_child != set_child.end(); iter_child++)
		{
			set_root.insert(*iter_child + root->weight);
			Set.insert(*iter_child + root->weight);
		}
		for (set<int>::iterator iter_sibling = set_sibling.begin(); iter_sibling != set_sibling.end(); iter_sibling++)
		{
			set_root.insert(*iter_sibling);
			Set.insert(*iter_sibling + root->weight);
		}
		return set_root;
	}
	void build(Edge *root)//当前执行操作的结点,当前结点的序号
	{
		//当当前根节点至少有一个叶子结点
		if (adjList[root->vertex].empty())
		{
			return;
		}
		//那么它的child结点就是它的第一个孩子
		root->child = new Edge;
		root->child->vertex = adjList[root->vertex][0].first;
		root->child->weight = adjList[root->vertex][0].second;
		if (adjList[root->child->vertex].size())
		{
			build(root->child);
		}
		int i = root->vertex;
		root = root->child;
		for (size_t j = 1; j < adjList[i].size(); j++)
		{
			root->sibling = new Edge;
			root->sibling->vertex = adjList[i][j].first;
			root->sibling->weight = adjList[i][j].second;
			build(root->sibling);
			root = root->sibling;
		}
	}
	BiTree(const int &v)
	{
		vertex = v;
		adjList.resize(v + 1);
		root = new Edge;
		root->vertex = 1;
		root->weight = 0;
		root->child = root->sibling = NULL;
		for (int i = 1; i <= vertex; i++)
		{
			while (1)
			{
				int j; cin >> j; if (!j)break;
				int weight; cin >> weight;
				adjList[i].push_back({ j,weight });
			}
		}
		build(root);
		traversal(root);
	}
	void check()
	{
		int distance;
		while (cin >> distance&&distance)
		{
			if (Set.find(distance) != Set.end())
			{
				cout << "AYE" << endl;
			}
			else
			{
				cout << "NAY" << endl;
			}
		}
		cout << '.' << endl;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	while (cin >> n&&n)
	{
		BiTree tree(n);
		tree.check();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值