手写链表40 TLE on #3#4#5 HELP!!!
查看原帖
手写链表40 TLE on #3#4#5 HELP!!!
477821
toolong114514楼主2023/6/17 10:20
#include<iostream>
#include<cstdio>
using namespace std;
struct nod{
	int data;
	nod *next;
	nod(){}
	nod(int data,nod *next){
		this->data=data;
		this->next=next;
	}
};
nod *head,*tail;
int n,m;
int main(){
	head=new nod();
	head->data=-1;
	tail=head;
	tail->next=new nod(1,NULL);
	tail=tail->next;
	cin>>n;
	for(int i=2;i<=n;i++){
		int k,p;
		scanf("%d%d",&k,&p);
		nod *tmp;
		tmp=head;
		if(p==0){
			while(tmp!=NULL&&tmp->next->data!=k){
				tmp=tmp->next;
			}
			tmp->next=new nod(i,tmp->next);
		}else{
			while(tmp!=NULL&&tmp->data!=k){
				tmp=tmp->next;
			}
			tmp->next=new nod(i,tmp->next);
		}
	}
	cin>>m;
	while(m--){
		int x;
		scanf("%d",&x);
		nod *tmp1=head->next,*tmp2=head;
		while(tmp1!=NULL&&tmp1->data!=x){
			tmp1=tmp1->next;
			tmp2=tmp2->next;
		}
		if(tmp1!=NULL) tmp2->next=tmp1->next;
	}
	nod *tmp=head->next;
	while(tmp!=NULL){
		printf("%d ",tmp->data);
		tmp=tmp->next;
	}
	return 0;
}
2023/6/17 10:20
加载中...