PAT 1097. 链表重复数据删除
原题链接
中等
作者:
YAX_AC
,
2024-11-26 20:55:24
,
所有人可见
,
阅读 2
//duplicated重复 absolute绝对的 separate分离,单独
//remove the nodes with duplicated absolute values of the keys
//删除键绝对值重复的节点
// only the first node of which the value or absolute value of its key equals K will be kept.
//将仅保留其键的值或绝对值等于K的第一个节点。
//all the removed nodes must be kept in a separate list.
//所有删除的节点必须保存在单独的列表中。
//For each case, output the resulting linked list first, then the removed list.
//首先按顺序输出结果链表,然后按顺序输出删除链表
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
const int N = 100010;
int n;
int h,e[N],ne[N];
int st[N];
int main()
{
cin>>h>>n;
for(int i = 0; i<n; i++)
{
int address,data,next;
cin>>address>>data>>next;
e[address] = data,ne[address] = next;
}
vector<int> a,b;
for(int i = h; i!=-1; i = ne[i])
{
int v = abs(e[i]);
if(st[v]) b.push_back(i);
else
{
st[v] = true;
a.push_back(i);
}
}
for(int i = 0; i<a.size(); i++)
{
printf("%05d %d ",a[i],e[a[i]]);
if(i+1 == a.size()) puts("-1");
else printf("%05d\n",a[i+1]);
}
for(int i = 0; i<b.size(); i++)
{
printf("%05d %d ",b[i],e[b[i]]);
if(i+1 == b.size()) puts("-1");
else printf("%05d\n",b[i+1]);
}
return 0;
}