题干:https://pintia.cn/problem-sets/994805342720868352/problems/994805369774129152
题解:做了40分钟,拿了15分
// A1097.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <bits/stdc++.h>
using namespace std;
struct node
{
int address;
int key;
int next;
};
unordered_map<int, node> link;
vector<node> an;
vector<node> bn;
int main()
{
#ifndef ONLINE_JUDGE
FILE* s;
freopen_s(&s, "in.txt", "r", stdin);
#endif // !ONLINE_JUDGE
int head, n;
cin >> head >> n;
int address, key, next;
for (int i = 0; i < n; i++) {
cin >> address >> key >> next;
link[address].address = address;
link[address].key = key;
link[address].next = next;
}
while (head != -1) {
an.push_back({ link[head].address, link[head].key, link[head].next });
//printf("%04d %d %04d\n", link[head].address, link[head].key, link[head].next);
head = link[head].next;
}
for (int i = 0; i < an.size(); i++) {
for (int j = i + 1; j < an.size(); j++) {
if (abs(an[j].key) == abs(an[i].key)) {
bn.push_back(an[j]);
an.erase(an.begin()+j);
j--;
}
}
}
for (int i = 0; i < an.size(); i++) {
if (i == an.size() - 1) {
printf("%05d %d %d\n", an[i].address, an[i].key, -1);
}
else {
printf("%05d %d %05d\n", an[i].address, an[i].key, an[i + 1].address);
}
}
for (int i = 0; i < bn.size(); i++) {
if (i == bn.size() - 1) {
printf("%05d %d %d\n", bn[i].address, bn[i].key, -1);
}
else {
printf("%05d %d %05d\n", bn[i].address, bn[i].key, bn[i + 1].address);
}
}
return 0;
}