POJ_214_Boatherds_TLE

#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;
}

你可能感兴趣的:(poj)