因为数值比较小,所以直接用数组来模拟
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<cmath> 5 using namespace std; 6 7 #define maxn 100005 8 9 struct node 10 { 11 int key; 12 int next; 13 }a[maxn]; 14 15 int main() 16 { 17 int first,n,add; 18 scanf("%d%d",&first,&n); 19 for(int i=0;i<n;i++) 20 { 21 scanf("%d",&add); 22 scanf("%d%d",&a[add].key,&a[add].next); 23 } 24 25 //去重:直接再开两个数组记录去重后两个链表的地址 26 int num,vis[10005]={0},t1=0,t2=0; 27 int a1[maxn],a2[maxn]; 28 for(int i=first;i!=-1;i=a[i].next) 29 { 30 num=abs(a[i].key); 31 if(!vis[num]) 32 { 33 vis[num]=1; 34 a1[t1++]=i; 35 } 36 else 37 a2[t2++]=i; 38 39 } 40 41 printf("%05d %d ",a1[0],a[a1[0]].key); 42 for(int i=1;i<t1;i++) 43 { 44 printf("%05d ",a1[i]); 45 printf("%05d %d ",a1[i],a[a1[i]].key); 46 } 47 printf("-1 "); 48 49 if(t2) 50 { 51 printf("%05d %d ",a2[0],a[a2[0]].key); 52 for(int i=1;i<t2;i++) 53 { 54 printf("%05d ",a2[i]); 55 printf("%05d %d ",a2[i],a[a2[i]].key); 56 } 57 printf("-1 "); 58 } 59 60 return 0; 61 }