PAT 1051两种方法(第三种超时)建树DFS与规律

 DFS

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
vector <int>pre, mid;
struct Node {
	int data;
	Node* left;
	Node* right;

	Node() {
		left = NULL;
		right = NULL;
	}
};
int M, N;
Node * build(int PL, int PR, int ML, int MR)
{
	if (PL > PR)
	{
		return NULL;
	}
	int x = pre[PL];
	int ind = -1;
	for (int i = ML; i <= MR; i++)
	{
		if (mid[i] == x)
		{
			ind = i;
			break;
		}
	}
	int leftnum = ind - ML;
	Node* root = new Node();
	root->data = x;
	root->left = build(PL+1, PL+leftnum, ML, ML+leftnum-1);
	root->right = build(PL + leftnum+1,PR, ML + leftnum+1,MR);
	return root;
}
vector<int>path;
vector <int>temp;
bool DFS(Node *root,int x)
{
	if (root == NULL)
		return false;
	temp.push_back(root->data);
	if (root->data == x)
	{
		path = temp;
		temp.pop_back();
		return true;
	}
	bool f = BFS(root->left, x) || BFS(root->right, x);
	temp.pop_back();
	return(f);
	
}
int main()
{
	
	cin >>M>> N;
	map<int, bool>isin;
	for (int i = 0; i < N; i++)
	{
		int t; cin >> t;
		mid.push_back(t);
	}
	for (int i = 0; i < N; i++)
	{
		int t; cin >> t;
		pre.push_back(t);
	}
	Node *root = build(0, N - 1, 0, N - 1);
	for (int i = 0; i < M; i++)
	{
		if (i != 0)printf("\n");
		int a, b;
		scanf("%d %d", &a, &b);
		vector<int>pa, pb;
		bool fa=BFS(root, a);
		pa = path;
		path.clear();
		bool fb = DFS(root, b);
		pb = path;
		path.clear();
		if (fa&&fb)
		{
			int ind =-1;
			for (int j = 0; j < pa.size()&&j<pb.size(); j++)
			{
				if (pa[j] == pb[j])
				{
					ind = j;
					
				}
				else {
					break;
				}
			}
			if (pa[ind] != a && pa[ind] != b)
			{
				printf("LCA of %d and %d is %d.", a, b, pa[ind]);
			}
			else if (pa[ind] == a)
			{
				printf("%d is an ancestor of %d.", a, b);
			}
			else {
				printf("%d is an ancestor of %d.", b, a);
			}

		}
		else if (!fa && !fb)
		{
			printf("ERROR: %d and %d are not found.", a,b);
		}
		else if (fa && !fb)
		{
			printf("ERROR: %d is not found.", b);
			
		}
		else {
			printf("ERROR: %d is not found.", a);
		}
		
		
	}
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
vector <int>pre, mid;
struct Node {
	int data;
	Node* left;
	Node* right;

	Node() {
		left = NULL;
		right = NULL;
	}
};
int M, N;

map<int, int> hasin;
map<int, int> pos;

int myfind(int PL, int PR, int ML, int MR,int midx,int midy)
{
	int root = pre[PL];
	int index = -1;
	for (int i = ML; i <= MR; i++)
	{
		if (root == mid[i])
		{
			index = i;
			break;
		}
	}
	int leftnum = index - ML;
	if (midx <= index && index <= midy)
	{
		return root;
	}
	else if (midx < index&&midy < index)
	{
		return myfind(PL+1, PL+leftnum, ML, ML+leftnum-1, midx,  midy);
	}
	else if (midx > index&&midy > index){
		return myfind(PL + leftnum+1, PR, ML + leftnum+1, MR, midx, midy);
	}
}

int main()
{
	
	cin >>M>> N;
	map<int, bool>isin;
	for (int i = 0; i < N; i++)
	{
		int t; cin >> t;
		hasin[t]++;
		mid.push_back(t);
		pos[t] = i;
	}
	for (int i = 0; i < N; i++)
	{
		int t; cin >> t;
		pre.push_back(t);
	}
	
	for (int i = 0; i < M; i++)
	{
		if (i != 0)printf("\n");
		int a, b;
		scanf("%d %d", &a, &b);
		if (hasin[a] > 0 && hasin[b] > 0)
		{
			int x = pos[a], y = pos[b];
			int num=myfind(0,N-1,0,N-1,min(x,y),max(x,y));
			if (num == a)
			{
				printf("%d is an ancestor of %d.", a,b);
			}else if(num==b)
				printf("%d is an ancestor of %d.", b, a);
			else
				printf("LCA of %d and %d is %d.", a, b,num);
		}
		else if (hasin[a] == 0 && hasin[b] == 0)
			printf("ERROR: %d and %d are not found.", a, b);
		else if (hasin[a] > 0 && hasin[b] == 0)
			printf("ERROR: %d is not found.", b);
		else 
			printf("ERROR: %d is not found.", a);
		
		
		
	}
	return 0;
}

 还在想是否放弃用中序寻找根与两者关系转而用前序,然后突然想起来超时应该是因为中间对root的的查找使得时间复杂度从log上升到了Nlogn,并且其实我做了hash映射的,直接pos[root]查找就行。果然就成功了。

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
vector <int>pre, mid;
struct Node {
	int data;
	Node* left;
	Node* right;

	Node() {
		left = NULL;
		right = NULL;
	}
};
int M, N;

map<int, int> hasin;
map<int, int> pos;

int myfind(int PL, int PR, int ML, int MR,int midx,int midy)
{
	int root = pre[PL];
	int index = pos[root];
	
	int leftnum = index - ML;
	if (midx <= index && index <= midy)
	{
		return root;
	}
	else if (midx < index&&midy < index)
	{
		return myfind(PL+1, PL+leftnum, ML, ML+leftnum-1, midx,  midy);
	}
	else if (midx > index&&midy > index){
		return myfind(PL + leftnum+1, PR, ML + leftnum+1, MR, midx, midy);
	}
}

int main()
{
	
	cin >>M>> N;
	map<int, bool>isin;
	for (int i = 0; i < N; i++)
	{
		int t; cin >> t;
		hasin[t]++;
		mid.push_back(t);
		pos[t] = i;
	}
	for (int i = 0; i < N; i++)
	{
		int t; cin >> t;
		pre.push_back(t);
	}
	
	for (int i = 0; i < M; i++)
	{
		if (i != 0)printf("\n");
		int a, b;
		scanf("%d %d", &a, &b);
		if (hasin[a] > 0 && hasin[b] > 0)
		{
			int x = pos[a], y = pos[b];
			int num=myfind(0,N-1,0,N-1,min(x,y),max(x,y));
			if (num == a)
			{
				printf("%d is an ancestor of %d.", a,b);
			}else if(num==b)
				printf("%d is an ancestor of %d.", b, a);
			else
				printf("LCA of %d and %d is %d.", a, b,num);
		}
		else if (hasin[a] == 0 && hasin[b] == 0)
			printf("ERROR: %d and %d are not found.", a, b);
		else if (hasin[a] > 0 && hasin[b] == 0)
			printf("ERROR: %d is not found.", b);
		else 
			printf("ERROR: %d is not found.", a);
		
		
		
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值