zoj2615-Cells

超时= =,可以用数组的存储结构来计算出子父节点,应该不会超时,懒得写了...

#include<iostream>
#include<queue>
#include<stack>
#include<stdio.h>
using namespace std;

const int chnum=101;

struct node
{
	int data;
	node *ch[chnum];
	node()
	{
		for(int i=0;i<chnum;i++)
			ch[i]=NULL;
	}
};

void del(node *&head)
{
	stack<node *> sta;
	node *p;
	sta.push(head);
	while(!sta.empty())
	{
		p=sta.top();
		sta.pop();
		for(int i=0;i<chnum;i++)
		{
			if(p->ch[i])
			{
				sta.push(p->ch[i]);
			}
		}
		delete p;
	}
}
void answer(node *,int,int);
int main()
{
	queue<node *> que;
	int icase,o=1;
	bool fl=false;
	scanf("%d",&icase);
	while(icase--)
	{
		if(fl)
			printf("\n");
		fl=true;
		while(!que.empty())
			que.pop();
		int num,k=0;
		scanf("%d",&num);
		node *p=NULL,*now=NULL,*head=NULL;
		p=new node;
		head=p;//头结点
		p->data=k++;
		que.push(p);
		while(num--)
		{
			int nonum;
			scanf("%d",&nonum);
			now=que.front();
			que.pop();
			for(int i=0;i<nonum;i++)
			{
				p=new node;
				now->ch[i]=p;
				p->data=k++;
				que.push(p);
			}
		}
		int testnum;
		scanf("%d",&testnum);
		bool flag= true;
		while(testnum--)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			if(flag)
				printf("Case %d:\n",o++);
			flag=false;
			answer(head,a,b);
		}
		del(head);
	}
	return 0;
}
bool fine(node *&aima,node *anode,int num)//爆栈了= =,被迫改,还是超时..
{
	stack<node *> sta;
	node *p;
	sta.push(anode);
	while(!sta.empty())
	{
		p=sta.top();
		sta.pop();
		if(p->data==num)
		{
			aima=p;
			return true;
		}
		for(int i=0;i<chnum;i++)
		{
			if(p->ch[i])
			{
				sta.push(p->ch[i]);
			}
		}
	}
}
void answer(node *head,int a,int b)
{
	node *aim=NULL,*temp=NULL;
	fine(aim,head,a);
	fine(temp,aim,b);
	if(temp!=NULL)
		printf("Yes\n");
	else
		printf("No\n");
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值