40pts求调后三个TLE
查看原帖
40pts求调后三个TLE
1069747
matthewYuan楼主2023/11/14 18:11
#include<iostream>
#include<string>
using namespace std;
struct person
{
	int num;
	person* next;
};
int n;
int m;
void insertlink(person**a, person*b,int c,int t)//b代表插入的,c是插入到几号,t是左或者右
{
	person* pb, * pf;
	pb = *a;
	pf = *a;
	if (t== 0)
	{
		while (pb->num!=c&&pb->next!=NULL)
		{
			pf = pb;
			pb = pb->next;
		}
		if (pb == *a)
		{
			b->next = *a;
			*a = b;
		}
		else
		{
			pf->next = b;
			b->next = pb;
		}
	}
	else if (t == 1)
	{
		while (pb->num != c&&pb->next!=NULL)
		{
			pf = pb;
			pb = pb->next;
		}
			if (pb == *a)
			{
				b->next = pb->next;
				(*a)->next = b;
			}
			else
			{
				b->next = pb->next;
				pb->next = b;
			}
		
	}
}
void eraselink(person** a,int b)
{
	person* pf, * pb;
	pb = *a;
	pf = *a;
	while (pb->num != b&&pb->next!=NULL)
	{
		pf = pb;
		pb = pb->next;
	}
	if (pb->num == b)
	{
		if (pb == *a)
		{
			*a = pb->next;
		}
		else
		{
			pf->next = pb->next;
		}
		free(pb);
		pb = NULL;
	}
}
void printlink(person* a)
{
	person* mov;
	mov = a;
	while (mov != NULL)
	{
		cout << mov->num << " ";
		mov = mov->next;
	}
	cout << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	person* head = new person;
	head->num = 1;
	head->next = NULL;
	for (int i = 2; i <= n; i++)
	{
		
		int x, y;
		cin >> x >> y;
		person* q = new person;
		q->num = i;
		q->next = NULL;
		insertlink(&head, q,x, y);
	}
	cin >> m;
	for (int i = 1; i <= m; i++)
	{
		int z;
		cin >> z;
		eraselink(&head, z);
	}
	printlink(head);
	return 0;
}
2023/11/14 18:11
加载中...