【思路】:模拟的链表,果断TLE,先贴上吧,有机会再改。一年以后绝对远超这个水平。
【TLE代码】:
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAX 100000+10
struct Node
{
int address;
int data;
int next;
Node *nextNode;
};
Node node[MAX];
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n = 0, start = 0;
int i = 0, j = 0;
Node *startNode = NULL;
//input
cin >> n >> start;
for (i = 0; i < n; i++)
{
cin >> node[i].address >> node[i].data >> node[i].next;
}
//link
for (i = 0; i < n; i++)
{
if (node[i].address == start)
startNode = &node[i];
if (node[i].next == -1)
node[i].nextNode = NULL;
for (j = 0; j < n; j++)
{
if (node[i].next == node[j].address)
node[i].nextNode = &node[j];
}
}
//delete
Node *p, *q, *pre;
for (p = startNode; p != NULL; p=(*p).nextNode)
{
pre = p;
for (q = (*p).nextNode; q != NULL; q=(*q).nextNode)
{
if (abs((*p).data) == abs((*q).data))
{
//delete
(*pre).nextNode = (*q).nextNode;
(*pre).next = (*q).next;
n--;
}
else
pre=(*pre).nextNode;
}
}
//output
cout << n << " " << start << endl;
for (p = startNode; p != NULL; p=(*p).nextNode)
{
cout << setprecision(5) << setfill('0') << (*p).address << " " << (*p).data << " " << setprecision(5) << setfill('0') << (*p).next << endl;
}
}