L2-002. 链表去重
来源:互联网 发布:松下fpxh编程样列 编辑:程序博客网 时间:2024/05/29 10:33
//使用链表实现//c++链表去重问题,当前元素的前一个元素和尾指针指向当前元素,遍历结束条件为!=-1//本题注意tail=-1 就是设置尾节点的结束#include<iostream>#include<cstdio>#include<cstring> #include<cmath>using namespace std;int S,N;const int maxn=1e6+10;struct T{ int from,val,to;};T np[maxn];int vp[maxn];int star=1e6,head=1e6+1,tail;int main(){ cin>>S>>N; int a,b,c; np[star].to=S; np[head].to=-1; memset(vp,0,sizeof(vp)); for(int i=0;i<N;i++){ cin>>a>>b>>c; np[a].from=a; np[a].val=b; np[a].to=c; } int u=star; while(u!=-1&&np[u].to!=-1){ int u1=np[u].to; if(vp[abs(np[u1].val)]){ np[u].to=np[u1].to; if(np[head].to==-1)np[head].to=tail=u1; else{ np[tail].to=u1; tail=u1; } } else{ vp[abs(np[u1].val)]=1; u=np[u].to; } } np[tail].to=-1; int s1=star,s2=head; while(np[s1].to!=-1){ int temp1=np[s1].to; s1=np[s1].to; printf("%05d %d ",np[temp1].from,np[temp1].val); if(np[temp1].to!=-1)printf("%05d\n",np[temp1].to); else printf("%d\n",np[temp1].to); } while(np[s2].to!=-1){ int temp1=np[s2].to; s2=np[s2].to; printf("%05d %d ",np[temp1].from,np[temp1].val); if(np[temp1].to!=-1)printf("%05d\n",np[temp1].to); else printf("%d\n",np[temp1].to); } return 0;}
0 0
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- L2-002. 链表去重
- python机器学习-聚类KMeans
- Hive 之Table、External Table、Partition(五)
- 七大基本排序算法C/C++(已优化及测试)
- QT--foreach的用法
- Spring中Session过期的问题处理方法
- L2-002. 链表去重
- Android 对拍照、选照和剪切的封装
- 模拟实现strcpy与strcmp函数
- python最优化-梯度下降实现
- 与本地程序相关的一个bug------要配合利用date命令观察才好!
- 中缀表达式转后缀表达式及其计算
- ADB使用详细教程
- Android7.0系统使用Intent跳转到APK安装页
- 一步一步搞清排序之堆排序(JAVA)