80分TLE求助
  • 板块B3631 单向链表
  • 楼主z_y_w_
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/9/25 21:45
  • 上次更新2023/11/2 18:05:31
查看原帖
80分TLE求助
1045286
z_y_w_楼主2023/9/25 21:45
#include<iostream>
using namespace std;
int n;
struct node
{
	int num;
	int nxt;
}a[100005];
int head = 1;
int cnt = 1;
void insert(int x, int y)
{
	for (int i = head; i != NULL; i = a[i].nxt)
	{
		if (a[i].num == x)
		{
			a[++cnt] = { y, a[i].nxt };
			a[i].nxt = cnt;
			return;
		}
	}
}

void out(int x)
{
	for (int i = head; i != NULL; i = a[i].nxt)
	{
		if (a[i].num == x)
		{
			cout << a[a[i].nxt].num << "\n";

			return;

		}
	}
}

void deleten(int x)
{
	for (int i = head; i != NULL; i = a[i].nxt)
	{
		if (a[i].num == x)
		{

			a[i].nxt = a[a[i].nxt].nxt;
			return;

		}
	}
}

int main()
{
	cin >> n;
	a[1].num = 1;
	a[1].nxt = NULL;
	for (int iii = 1; iii <= n; iii++)
	{
		
		int flag, x, y;
		cin >> flag;
		if (flag == 1)
		{
			cin >> x >> y;
			insert(x, y);
		}
		else if (flag == 2)
		{
			cin >> x;
			out(x);
		}
		else
		{
			cin >> x;
			deleten(x);
		}
	}

}
2023/9/25 21:45
加载中...