L2-002 链表去重 (25分) #include <bits/stdc++.h> //#define int ll using namespace std; //#pragma GCC optimize(2) typedef long long ll; const ll mod = 1e9 + 7; const int maxn = 1e6 + 10; struct node{ int id,k,next; }a[maxn],c[maxn],d[maxn]; int b[10010]={0}; void solve() { int sdz,n; cin>>sdz>>n; for (int i = 0; i < n; ++i) { int dz,k,next; cin>>dz>>k>>next; a[dz].k=k,a[dz].next=next; } int now=sdz; int top1=0,top2=0; while (now!=-1){ if (!b[abs(a[now].k)]) c[top1].id=now,c[top1].k=a[now].k,c[top1].next=a[now].next,top1++,b[abs(a[now].k)]=1; else d[top2].id=now,d[top2].k=a[now].k,d[top2].next=a[now].next,top2++; now=a[now].next; } for (int i = 0; i < top1-1; ++i) printf("%05d %d %05d\n",c[i].id,c[i].k,c[i+1].id); printf("%05d %d -1\n",c[top1-1].id,c[top1-1].k); if (top2!=0){ for (int i = 0; i < top2-1; ++i) printf("%05d %d %05d\n",d[i].id,d[i].k,d[i+1].id); printf("%05d %d -1\n",d[top2-1].id,d[top2-1].k); } } int main() { //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int _ = 1; //cin >> _; while (_--) { solve(); } return 0; }