题目描述
C++代码
样例
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
const int N = 1e5 + 10;
struct node {
int w;
int ne;
}sq[N];
int main()
{
int head, n;
unordered_map<int, int> mp;
cin >> head >> n;
while (n--)
{
int adress, key, next;
cin >> adress >> key >> next;
sq[adress].ne = next;
sq[adress].w = key;
}
vector<int> ans[2];
while (head != -1)
{
if (mp[abs(sq[head].w)] == 0)
{
ans[0].push_back(head);
mp[abs(sq[head].w)] = 1;
}
else {
ans[1].push_back(head);
}
head = sq[head].ne;
}
int o = 0;
for (auto it : ans[0])
{
if (!o)
{
printf("%05d %d ", it, sq[it].w);
o = 1;
}
else {
printf("%05d\n%05d %d ", it, it, sq[it].w);
}
}
printf("-1\n");
int p = 0;
for (auto it : ans[1])
{
if (!p)
{
printf("%05d %d ", it, sq[it].w);
p = 1;
}
else {
printf("%05d\n%05d %d ", it, it, sq[it].w);
}
}
if (p)
{
printf("-1\n");
}
return 0;
}