用链表做的,看了看题解也是链表。但是我没太搞懂我这样做为何也是TLE。
#include <iostream>
using namespace std;
struct Node {
private:
static int ID;
public:
int data;
Node *next = NULL;
Node *last = NULL;
Node() {
data = ++ID;
}
};
int Node::ID = 0;
void printN(Node *h)
{
while (h != NULL) {
cout << h->data << " ";
h = h->next;
}
cout << endl;
}
int main()
{
int n, stu;
bool dir;
cin >> n;
Node *head = new Node;
Node *temp;
for (int i = 1; i < n; i++) {
cin >> stu >> dir; // false->left; true->right
Node *temp2 = new Node;
temp = head;
while (temp->next != NULL && temp->data != stu) {
temp = temp->next;
}
if (dir) {
if (temp->next != NULL) {
temp2->next = temp->next;
temp->next->last = temp2;
temp2->last = temp;
temp->next = temp2;
} else {
temp->next = temp2;
temp2->last = temp;
}
} else {
if (temp->last == NULL) {
temp2->next = head;
head->last = temp2;
head = temp2;
} else {
temp2->next = temp;
temp2->last = temp->last;
temp->last->next = temp2;
temp->last = temp2;
}
}
// printN(head);
}
cin >> n;
for (int i = 0; i < n; i++) {
cin >> stu;
temp = head;
while (temp != NULL) {
if (temp->data == stu) {
if (temp->last == NULL) {
temp->next->last = NULL;
head = temp->next;
} else if (temp->next == NULL) {
temp->last->next = NULL;
} else {
temp->last->next = temp->next;
temp->next->last = temp->last;
}
delete temp;
break;
}
temp = temp->next;
}
// printN(head);
}
printN(head);
return 0;
}